Note that there are some explanatory texts on larger screens.

plurals
  1. POFix an error? Reading from File
    primarykey
    data
    text
    <p><strong>My output is:</strong></p> <blockquote> <p>IKE Dinner 25.00 04/15/1993</p> <p>HUNTER Dinner 50.00 DATE1</p> <p>STEVE Lodging 25.00 DATE2</p> <p>ROY Lodging 50.00 DATE3</p> <p>MAX Conference 25.00 DATE4File contents invalid.</p> </blockquote> <p><strong>How do I fix the error? It is reading from the file "hotel" with contents:</strong> </p> <blockquote> <p>IKE;Dinner;25.00;04/15/1993;</p> <p>HUNTER;Dinner;50.00;DATE1;</p> <p>STEVE;Lodging;25.00;DATE2;</p> <p>ROY;Lodging;50.00;DATE3;</p> <p>MAX;Conference;25.00;DATE4;</p> </blockquote> <p><strong>Expected Output:</strong></p> <blockquote> <p>IKE Dinner 25.00 04/15/1993</p> <p>HUNTER Dinner 50.00 DATE1</p> <p>STEVE Lodging 25.00 DATE2</p> <p>ROY Lodging 50.00 DATE3</p> <p>MAX Conference 25.00 DATE4</p> <p>Dinner: TOTAL Conference: TOTAL Lodging: TOTAL</p> </blockquote> <p><strong>CODE</strong></p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.util.Scanner; public class P7Point16 { public static void main(String[] args) { double dinner = 0; double conference = 0; double lodging = 0; String name = null, service = null, amount = null, date = null; File file = new File( "DIRECTORY\\src\\hotel"); Scanner read = null; try { read = new Scanner(file); read.useDelimiter(";"); while (read.hasNextLine()) { if(read.hasNext()) { name = read.next(); service = read.next(); amount = read.next(); date = read.next(); if (service.equalsIgnoreCase("Lodging")) { lodging += Double.parseDouble(amount); } else if (service.equalsIgnoreCase("Conference")) { conference += Double.parseDouble(amount); } else if (service.equalsIgnoreCase("Dinner")) { dinner += Double.parseDouble(amount); } System.out.print(name + " " + service + " " + amount + " " + date); } } System.out.println(""); System.out.println("Dinner: " + dinner + " Conference: " + conference + " Lodging: " + lodging); } catch (FileNotFoundException exception) { System.out.println("File not found."); } catch (NoSuchElementException exception) { System.out.println("File contents invalid."); } finally { read.close(); } } } </code></pre> <p><strong>WORKING CODE</strong></p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.util.NoSuchElementException; import java.util.Scanner; public class P7Point16 { public static void main(String[] args) { double dinner = 0; double conference = 0; double lodging = 0; String name = null, service = null, amount = null, date = null; File file = new File( "DIRECTORY\\src\\hotel"); Scanner read = null; try { read = new Scanner(file); read.useDelimiter(";"); while (read.hasNext()) { name = read.next(); service = read.next(); amount = read.next(); date = read.next(); if (service.equalsIgnoreCase("Lodging")) { lodging += Double.parseDouble(amount); } else if (service.equalsIgnoreCase("Conference")) { conference += Double.parseDouble(amount); } else if (service.equalsIgnoreCase("Dinner")) { dinner += Double.parseDouble(amount); } System.out.print(name + " " + service + " " + amount + " " + date); } System.out.println(""); System.out.println("Dinner: " + dinner + " Conference: " + conference + " Lodging: " + lodging); } catch (FileNotFoundException exception) { System.out.println("File not found."); } catch (NoSuchElementException exception) { System.out.println("File contents invalid."); } finally { read.close(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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