Note that there are some explanatory texts on larger screens.

plurals
  1. POreading from a comma separated text file
    text
    copied!<p>I am trying to write a Java program that simulates a record store shopping cart. The first step is to open up the <code>inventory.txt</code> file and read the contents which is basically what the "<em>store has to offer</em>". Then I need to read every line individually and process the id record and price. </p> <p>The current method outputs a result that is very close to what I need, however, it picks up on the item id of the next line, as you can see below.</p> <p>I was wondering if someone can assist me in figuring out how to process every line in the text document individually and store every piece of data in its own variable without picking up the <code>id</code> of the next item?</p> <pre><code>public void openFile(){ try{ x = new Scanner(new File("inventory.txt")); x.useDelimiter(","); } catch(Exception e){ System.out.println("Could not find file"); } } public void readFile(){ while(x.hasNext()){ String id = x.next(); String record = x.next(); String price = x.next(); System.out.println(id + " " + record + " " + price); break; } } </code></pre> <p><strong>.txt document:</strong></p> <pre><code>11111, "Hush Hush... - Pussycat Dolls", 12.95 22222, "Animal - Ke$ha", 9.95 33333, "Hanging By A Moment - Lifehouse - Single, 4.95 44444, "Have A Nice Day - Bon Jovi", 9.99 55555, "Day &amp; Age - Killers", 10.99 66666, "She Wolf - Shakira", 15.99 77777, "Dark Horse - Nickelback", 12.99 88888, "The E.N.D. - Black Eyed Peas", 10.95 </code></pre> <p><strong>actual output</strong></p> <pre><code>11111 "Hush Hush... - Pussycat Dolls" 12.95 22222 </code></pre> <p><strong>expected result</strong></p> <pre><code>11111 "Hush Hush... - Pussycat Dolls" 12.95 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload