我的代码:
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:错误:不兼容的类型:对象无法转换为String name = st1.nextElement();
Main.java:24:错误:不兼容的类型:对象无法转换为String dob = st1.nextElement();
Main.java:26:错误:不兼容的类型:对象无法转换为String dd = st2.nextElement();
Main.java:27:错误:不兼容的类型:对象无法转换为String mm = st2.nextElement();
Main.java:28:错误:不兼容的类型:对象无法转换为String yyyy = st2.nextElement();
答案 0 :(得分:1)
答案 1 :(得分:0)
您可能希望使用nextToken
代替nextObject
。
或者使用nextObject
并将其转换为String
,但这似乎是一种开销。
查看 Javadoc
<强> nextElement()强> 返回与 nextToken 方法相同的值,除了它声明的返回值是Object而不是String。