StringTokenizer error: incompatible types: Object cannot be converted to String
StringTokenizer error: incompatible types: Object cannot be converted to String
我的代码:
class Customer
{
String name_dob,name,dob,dd,mm,yyyy;
void stringTokenize()
{
StringTokenizer st1 = new StringTokenizer(name_dob,",");
name = st1.nextElement();
dob = st1.nextElement();
StringTokenizer st2 = new StringTokenizer(dob,"/");
dd = st2.nextElement();
mm = st2.nextElement();
yyyy = st2.nextElement();
}
void display()
{
System.out.println(name + "," + dd + "," + mm + "," + yyyy);
}
}
public class Main
{
public static void main(String[] args) {
int i,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of data");
n = sc.nextInt();
Customer[] obj = new Customer[n];
for(i=0;i<n;i++)
{
obj[i] = new Customer();
System.out.println("Enter the data");
obj[i].name_dob = sc.nextLine();
obj[i].stringTokenize();
}
System.out.println("After tokenizing........");
for(i=0;i<n;i++)
{
System.out.println("The data is");
obj[i].display();
}
}
}
我得到的错误
Main.java:23: error: incompatible types: Object cannot be converted to String
name = st1.nextElement();
Main.java:24: error: incompatible types: Object cannot be converted to String
dob = st1.nextElement();
Main.java:26: error: incompatible types: Object cannot be converted to String
dd = st2.nextElement();
Main.java:27: error: incompatible types: Object cannot be converted to String
mm = st2.nextElement();
Main.java:28: error: incompatible types: Object cannot be converted to String
yyyy = st2.nextElement();
使用 nextToken
而不是 nextElement
。
nextElement
的文档说:
Returns the same value as the nextToken method, except that its declared return value is Object rather than String.
您可能想使用 nextToken
而不是 nextObject
。
或者使用 nextObject
并将其转换为 String
但这似乎是一个开销。
看看Javadoc
nextElement()
Returns the same value as the nextToken method, except that its declared return value is Object rather than String.
我的代码:
class Customer
{
String name_dob,name,dob,dd,mm,yyyy;
void stringTokenize()
{
StringTokenizer st1 = new StringTokenizer(name_dob,",");
name = st1.nextElement();
dob = st1.nextElement();
StringTokenizer st2 = new StringTokenizer(dob,"/");
dd = st2.nextElement();
mm = st2.nextElement();
yyyy = st2.nextElement();
}
void display()
{
System.out.println(name + "," + dd + "," + mm + "," + yyyy);
}
}
public class Main
{
public static void main(String[] args) {
int i,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of data");
n = sc.nextInt();
Customer[] obj = new Customer[n];
for(i=0;i<n;i++)
{
obj[i] = new Customer();
System.out.println("Enter the data");
obj[i].name_dob = sc.nextLine();
obj[i].stringTokenize();
}
System.out.println("After tokenizing........");
for(i=0;i<n;i++)
{
System.out.println("The data is");
obj[i].display();
}
}
}
我得到的错误
Main.java:23: error: incompatible types: Object cannot be converted to String name = st1.nextElement();
Main.java:24: error: incompatible types: Object cannot be converted to String dob = st1.nextElement();
Main.java:26: error: incompatible types: Object cannot be converted to String dd = st2.nextElement();
Main.java:27: error: incompatible types: Object cannot be converted to String mm = st2.nextElement();
Main.java:28: error: incompatible types: Object cannot be converted to String yyyy = st2.nextElement();
使用 nextToken
而不是 nextElement
。
nextElement
的文档说:
Returns the same value as the nextToken method, except that its declared return value is Object rather than String.
您可能想使用 nextToken
而不是 nextObject
。
或者使用 nextObject
并将其转换为 String
但这似乎是一个开销。
看看Javadoc
nextElement() Returns the same value as the nextToken method, except that its declared return value is Object rather than String.