so i need the program to read a file with a list of ints, if there are consecutive equal numbers, such as 10 20 20 20 20 30 30 ... 0, the output should be
(1 * 10)
(4 * 20)
(2 * 30)
etc, and it will end when a zero is reached
however im getting an error due to my else if statement and i dont know why?
can anyone help?
would an arraylist make i easier?
import java.util.*; import java.io.*; public class Proj57015 { public static void main (String [] args) throws FileNotFoundException{ System.out.println(""); System.out.println("CMSC 255 - Proj#5 - Jimmy Whitten"); String filename = args[0]; System.out.println("Data File: " + filename); System.out.println(""); Scanner filescan = new Scanner(new File(filename)); int num1 = filescan.nextInt(); int count = 0; while (filescan.hasNext()){ if (num1 == filescan.nextInt()) count++; else if (num1 != filescan.nextInt()) num1 = filescan.nextInt(); } System.out.println("(" + count + "*" + num1 + ")"); } }
--- Update ---
error is:
exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Proj57015.main(Proj57015.java:27)