String数组不存储在文本文件中的数据 [英] String array not storing data from text file
问题描述
所以我有一些code是应该使用一个JFileChooser得到一个文本文件,使用拆分和扫描仪使用冒号作为分隔符,并存储数据到一个数组。在code和文字,如下图所示。我已经加入code的一个片段来测试,如果数组长度小于1,它总是即使我已经将其设置为在一个字符串数组存储。为什么这样做的,我怎么能得到它从code的每行文字存储到6个区间数组?
否则如果(e.getSource()== readButton){
文件选择器的JFileChooser =新的JFileChooser(本地磁盘(C :));
如果(fileChooser.showOpenDialog(空)== JFileChooser.APPROVE_OPTION)
{
empFile = fileChooser.getSelectedFile();
}
扫描程序扫描=新的扫描仪(empFile);
而(scan.hasNext()){
的String [] rowData = scan.nextLine()拆分(:);
如果(rowData.length&所述; 1){
的System.out.println(错误);
}
否则,如果(rowData.length == 5){
rowData [4] =0;
FNAME = rowData [0];
LNAME = rowData [1];
情况2 = rowData [2];
firstParam = Double.parseDouble(rowData [3]);
empNum =的Integer.parseInt(rowData [4]); c.addEmployee(FNAME,LNAME,情况2,firstParam,0,empNum); }
其他{
FNAME = rowData [0];
LNAME = rowData [1];
情况2 = rowData [2];
firstParam = Double.parseDouble(rowData [3]);
secondParam =的Integer.parseInt(rowData [4]);
empNum =的Integer.parseInt(rowData [5]); c.addEmployee(FNAME,LNAME,情况2,firstParam,secondParam,empNum); } }
}
约翰:史密斯:制造:6.75:120:444
贝蒂:白色:经理:1200.00:111
斯坦:粘糊糊:销售:10000.00:332
贝蒂:布普:设计:12.50:50:244
块引用>解决方案您扫描仪正在一个字符串,而不是变量。而不是
扫描程序扫描=新的扫描仪(empFile);
尝试
扫描程序扫描=新的扫描仪(empFile);
从文档
扫描仪(字符串源)
构造产生从指定字符串扫描值一个新的扫描仪。
块引用>您目前正在扫描字符串empFile,而不是目前的实际文件。
So I have some code that is supposed to use a jfilechooser get a text file use a split and scanner with a colon as a delimeter and store the data into an array. The code and text is as seen below. I have added a segment of code to test if the array length is less than 1 and it always is even though I have set it to to store in a String array. Why is it doing this and how can I get it to store the text from each line of the code into an array of 6 intervals?
else if (e.getSource()==readButton) { JFileChooser fileChooser = new JFileChooser("Local Disk (C:)"); if (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) { empFile=fileChooser.getSelectedFile(); } Scanner scan = new Scanner("empFile"); while(scan.hasNext()) { String[] rowData = scan.nextLine().split(":"); if (rowData.length < 1){ System.out.println("error"); } else if(rowData.length == 5) { rowData[4] = "0"; fName = rowData[0]; lName = rowData[1]; position2 = rowData[2]; firstParam = Double.parseDouble(rowData[3]); empNum = Integer.parseInt(rowData[4]); c.addEmployee(fName, lName, position2, firstParam, 0, empNum); } else { fName = rowData[0]; lName = rowData[1]; position2 = rowData[2]; firstParam = Double.parseDouble(rowData[3]); secondParam = Integer.parseInt(rowData[4]); empNum = Integer.parseInt(rowData[5]); c.addEmployee(fName, lName, position2, firstParam, secondParam, empNum); } } }
John:Smith:Manufacturing:6.75:120:444
Betty:White:Manager:1200.00:111
Stan:Slimy:Sales:10000.00:332
Betty:Boop:Design:12.50:50:244
解决方案Your Scanner is taking in a String instead of the variable. Instead of
Scanner scan = new Scanner("empFile");
Try
Scanner scan = new Scanner(empFile);
From the docs
Scanner(String source) Constructs a new Scanner that produces values scanned from the specified string.
You're currently scanning the string "empFile" instead of the actual file currently.
这篇关于String数组不存储在文本文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!