Sac*_*ing 3 java stringtokenizer
我想我把它打破了最基本的形式.如果没有,那么我道歉并将尝试编辑它.为什么在我的while循环中,如果我已经将变量设置为String,我需要为FirstName强制转换String?难道我做错了什么?我尝试将FirstName变量设置为等于第一个标记,该标记应该是文本文件的第一个单词等.
try
{
BufferedReader infileCust =
new BufferedReader(new FileReader("C:\\custDat.txt"));
}
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.toString());
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
}
public static void createCustomerList(BufferedReader infileCust,
CustomerList custList) throws IOException
{
String FirstName;
String LastName;
int CustId;
//take first line of strings before breaking them up to first last and cust ID
String StringToBreak = infileCust.readLine();
//split up the string with string tokenizer
StringTokenizer st = new StringTokenizer(StringToBreak);
while(st.hasMoreElements()){
FirstName = (String) st.nextElement();
LastName = (String) st.nextElement();
CustId = Integer.parseInt((String) st.nextElement());
}
Run Code Online (Sandbox Code Playgroud)
编辑:显然我可以使用nextToken()代替.但是为什么我的变量显示为未使用?它们不在while循环的范围内吗?
这是因为nextElement返回Object.如果你打电话nextToken,你不需要演员.
从文档:
public Object nextElement()
返回与nextToken方法相同的值,除了它声明的返回值是Object而不是String.它的存在使得该类可以实现Enumeration接口.
编辑关于未使用的变量:您收到警告的原因是变量已分配,但未以某种方式打印,保存或分析.如果您writeln使用名字和姓氏添加呼叫,警告就会消失.
| 归档时间: |
|
| 查看次数: |
2465 次 |
| 最近记录: |