我正在尝试从包含名称和双打的文件中读取,如果它是名字,姓氏然后是双打,这将很容易,但有些行有一个中间首字母,有些只有一个名字。
像这样:
Hancock John 40.00 9.75
Light Karen L 40.00 9.60
Bob 12.02 42.90
等等 我将名称放入字符串数组,将数字放入二维数组。那么我将如何分离这些数据类型呢?编辑:我起初正在尝试这个
import java.io.*;
import java.text.*;
import java.util.*;
public class JEdwards11
{
public static void main(String[] args) throws Exception
{ PrintWriter outFile = new PrintWriter("H:/Java/program11.out");
String filename = "H:/Java/program11.dat", line, fullname;
StringTokenizer st;
Scanner inFile = new Scanner(new FileReader(filename));
int ctr = 0, i = 0, k = 0;
String [] names = new String [30];//print vertically
double [][] money = new double [7][30];//short across, long down
//hours worked, hourly rate, gross pay, net pay, fed, state, union
//while(inFile.hasNext())
for(i=0; i< names.length; i++)
{
if(inFile.hasNextDouble())
{money[1][i] = inFile.nextDouble();
money[2][i] = inFile.nextDouble();}
else
names[i] = inFile.next();
}
/*while(inFile.hasNext())
{line = inFile.nextLine();
st = new StringTokenizer(line);
names[i]=st.nextString;
money[1][i] = st.nextDouble();
money[2][i] = st.nextDouble();*/
for(i=0;i<names.length;i++)
for(i=0; i<names.length; i++)
System.out.println("name = " + names[i] +" money1 = "+money[1][i] +" money2= "+money[2][i]);
inFile.close();
outFile.close();
}
}
这根本不起作用。从那时起,我一直在搜索谷歌并重新阅读我的 java 书。很抱歉格式化,它不是被剪切/粘贴友好的,上次我在准备好之前点击输入它发布:(