Using delimiter when reading a file

前端 未结 6 1014
执念已碎
执念已碎 2020-12-16 04:29

I have little experience using delimiters and i need to read a text file that stores several objects whose data is stored in single lines separate by commas (\",\"). The sep

相关标签:
6条回答
  • 2020-12-16 04:45

    You should be using next(); where you are using nextLine();

    Have a look at the tutorial: http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

    Notice the lines:

    try {
       s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
    
       while (s.hasNext()) {
       System.out.println(s.next());
    }
    
    0 讨论(0)
  • 2020-12-16 04:45

    One issue is:

    while(read.hasNext())
       {
           title = read.nextLine();
           category = read.nextLine();
           runningTime = read.nextLine();
    
    hasNext()
    

    Returns true if this scanner has another token in its input. Not entire line. You need to use hasNextLine()

    You are doing nextLine() three times. I think what you need to do is, read the line and the split the line.

    0 讨论(0)
  • 2020-12-16 04:49

    All of the answers above are right and actually the same. However, one important point everybody should remember that the Scanner has a buffer size of only 1024. That means if the length of the delimited text is more, the parsing will stop.

    So, a little enhancement to the given solution, use BufferedReader instead of directly passing the file to the Scanner. Example:

        BufferedReader in = new BufferedReader(new FileReader("datafile.txt"), 16*1024);
        Scanner read = new Scanner(in);
        read.useDelimiter(",");
        String title, category, runningTime, year, price;
    
        while(read.hasNext())
        {
            title = read.next();
            category = read.next();
            runningTime = read.next();
            year = read.next();
            price = read.next();
            System.out.println(title + " " + category + " " + runningTime + " " + year + " " + price + "\n"); //just for debugging
        }
        read.close();
    
    0 讨论(0)
  • 2020-12-16 04:55

    You can also use a String.split() function to convert the string to an array of strings, then iterate over each of them for your values.

    How to convert comma-separated String to ArrayList? see this for more details.

    0 讨论(0)
  • 2020-12-16 04:57

    I think you want to call .next() which returns a String instead of .nextLine(). Your .nextLine() call is moving past the current line.

    Scanner read = new Scanner (new File("datafile.txt"));
       read.useDelimiter(",");
       String title, category, runningTime, year, price;
    
       while(read.hasNext())
       {
           title = read.next();
           category = read.next();
           runningTime = read.next();
           year = read.next();
           price = read.next();
         System.out.println(title + " " + category + " " + runningTime + " " + year + " " + price + "\n"); //just for debugging
       }
       read.close();
    
    0 讨论(0)
  • 2020-12-16 05:07

    Use read.next() instead of read.nextLine()

       title = read.next();
       category = read.next();
       runningTime = read.next();
       year = read.next();
       price = read.next();
    
    0 讨论(0)
提交回复
热议问题