2
while (inFile.hasNextLine()) {
    SortedArrayList<Fruit> fruitList = new SortedArrayList<Fruit>();
    String marketName = inFile.next();
    while (inFile.hasNexLine()) {
        String line = inFile.nextLine();
        if(line.isEmpty())
            break;
        else {
                Sting fruitName = inFile.next();
                int price = inFile.nextInt();
                int stock = inFile.nextInt();
                fruit = new Fruit(fruitName, price, stock);
                fruitList.insert(fruit);
        }        
    }
    market = new Market(marketName, fruitList);
    marketList.insert(market);
}

I want read text file.
The contents of the text file are as follows:

A_market  
apple 1000 11  
orange 2000 6   
grape 1600 17

B_market  
apple 900 20   
orange 2100 12  

...

Each market cannot know in advance the number of each fruit.
Each market will be separated by blank lines.
How do I identify the blank lines in the text file?

I want blank line on the basis of nested Iterator.
I try isEmpty(), regular expression, Check alphabet, etc..
However, the error is not the will.

Sorry question again then.
What to do to help would be appreciated.

Beginner
  • 53
  • 5

2 Answers2

1

One problem is likely related to mixing nextLine and next/nextInt. See Skipping nextLine() after use next(), nextInt() or other nextFoo() methods.

Generally mixing these is a bad idea. Pick either one or the other.

Some of your lines are also being skipped outright:

// this line is being skipped
// when it's not empty
String line = inFile.nextLine();
if(line.isEmpty())
    break;
else {
    Sting fruitName = inFile.next();
    int price = inFile.nextInt();
    int stock = inFile.nextInt();
    ...
}

What I'd suggest is to use a second Scanner each time you want to get the information for each fruit from a line:

while (inFile.hasNextLine()) {
    String marketName = inFile.nextLine();

    while (inFile.hasNextLine()) {
        String line = inFile.nextLine();
        if (line.isEmpty()) {
            break;
        }

        // create a new Scanner just
        // for this line so it doesn't
        // mess up the calls to nextLine
        Scanner lineIn = new Scanner(line);

        Sting fruitName = lineIn.next();
        int price = lineIn.nextInt();
        int stock = lineIn.nextInt();
        ...
    }

    ...
}
Community
  • 1
  • 1
Radiodef
  • 35,285
  • 14
  • 78
  • 114
  • Thank you for answers. Sorry, I forgot the code by mistake. SortedArrayList fruitList = new SortedArrayList(); SortedArrayList have used making. So I tried to use a second scanner .add do not use. But this was also an error. – Beginner May 10 '15 at 03:04
  • I don't understand what you're trying to say, I'm sorry. You can click the `edit` button and add information to your question. (If you are getting a compilation error or exception, add the error message to your post.) – Radiodef May 10 '15 at 03:07
  • English seotulreoseo I'm really sorry. The problem has been solved When I, as your advice. The error will be over by my mistake. Thank you very much. I thank you very much – Beginner May 10 '15 at 03:41
0

Java code

 while (inFile.hasNextLine()) {
        String marketName = inFile.next();
        while (inFile.hasNexLine()) {
            String line = inFile.nextLine();
            if(line.length()==0)     //blank line
                break;
            else {
                    Sting fruitName = inFile.next();
                    int price = inFile.nextInt();
                    int stock = inFile.nextInt();
                    fruit = new Fruit(fruitName, stock, price);
                    fruitList.insert(fruit);
            }        
        }
        market = new Market(marketName, fruitList);
        marketList.insert(market);
    }
Ritesh Karwa
  • 2,026
  • 11
  • 17
  • Thank you answer .I've tried that method. However, the length of 0, 1, etc. came out strangely, and noticed the error – Beginner May 10 '15 at 03:06