简体   繁体   English

Java从文件中读取具有不同长度的多种数据类型的输入

[英]java read input from file with multiple data types of varying lengths

I'm trying to read from a file containing names and doubles, this would be easy if it was first name, last name then the doubles, but some of the lines have a middle initial and some have just one name. 我正在尝试从包含名称和双打的文件中读取内容,如果它是名字,姓氏然后是双打,这会很容易,但是有些行的中间是缩写,有些只有一个名字。
Like this: 像这样:
Hancock John 40.00 9.75 汉考克·约翰40.00 9.75
Light Karen L 40.00 9.60 轻型Karen L 40.00 9.60
Bob 12.02 42.90 鲍勃12.02 42.90
and so on I'm putting the names into a string array and the numbers into a 2d array. 依此类推,我将名称放入字符串数组,将数字放入2d数组。 So how would I separate those data types? 那么我该如何区分这些数据类型呢? EDIT: I was trying this at first 编辑:我刚开始尝试

   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();

            }

            }

which did not work at all. 这根本没有用。 Since then I've searching Google and re-reading my java book. 从那时起,我一直在搜索Google并重新阅读我的Java书籍。 Sorry for the formatting, it's not being cut/paste friendly and last time I hit enter it posted before I was ready :( 对不起格式,它不被剪切/粘贴友好,上次我在准备好之前输入的内容已被输入:(

Are you familiar with Scanner? 您熟悉Scanner吗?

http://www.tutorialspoint.com/java/util/java_util_scanner.htm http://www.tutorialspoint.com/java/util/java_util_scanner.htm

There's a link, or just google Java.Util.Scanner. 有一个链接,或者只是google Java.Util.Scanner。

I would init a Java.Util.Scanner with the file text, then use calls to hasNextDouble() to determine if next token is a double. 我将使用文件文本初始化Java.Util.Scanner ,然后使用对hasNextDouble()调用来确定下一个标记是否为双hasNextDouble() If so, scan it with nextDouble , otherwise treat the next token as part of a name. 如果是这样,请使用nextDouble对其进行扫描,否则将下一个标记视为名称的一部分。

//psuedo code
Scanner s; // init with file
while ( s.hasNext() )
{
    if ( s.hasNextDouble() )
    {
        double d = s.nextDouble();
        // add to array
    }
    else
    {
        String name = s.next();
        // add to names
    }

}

You may have followed another approach. 您可能遵循了另一种方法。 Anyway I give you one easy trick to solve it with Scanner , just add tokens to a buffer until digits are encountered, then assign it to the name array. 无论如何,我给您一个简单的技巧来使用Scanner解决它,只需将令牌添加到缓冲区中直到遇到数字,然后将其分配给名称数组。

Again, your if-else block is erroneous since per iteration either if-part or the else-part will be executed. 同样,您的if-else块是错误的,因为每次迭代都会执行if-part或else-part。 Try something like this, it will definitely solve your problem : 尝试这样的事情,它肯定会解决您的问题:

for (i = 0; i < names.length; i++) {
        StringBuffer sbr = new StringBuffer();

        while (inFile.hasNext("[a-zA-Z]+")) {
            sbr.append(inFile.next() + " ");
        }
        names[i] = sbr.toString().trim();

        if (inFile.hasNextDouble()) {
            money[1][i] = inFile.nextDouble();
            money[2][i] = inFile.nextDouble();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM