So, i'm doing this program that takes 2 lines of input, the first line being a number that signifies how many numbers are going to be in the next line. I then have to find which number in the second line occurs the most often. Sounds simple. However, it is giving me problems. I am thinking with the whole read 2 lines thing. I thought I knew how, but my method of doing it didnt work as I thought it should, and ended up in not knowing when to stop collecting data from the scanner in the loop, even though I told it when. Buttt anyway, I looked for solutions online and came upon BufferedReader, which I know nothing about but looked it up and gave it a try. Now I am stuck with this mess of code that I cannot make sense of. The number it prints for occurring the most is 0, even when 0 is not in the second line. I do not know where to go from here as I cant find where the problem lies. I am really hoping for someone to just suggest a different solution to read the input. Unless you can fix this. I'll shut up and post the code now.

PS: even though it says if it reads "e" to stop, it really stops after you hit enter twice. If I change this it does not work the same. I have no idea why.

Code :

import java.util.*; import java.io.*; public class SandwichCounting { public static void main(String [] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please enter the total number of orders, hit enter, then list the order numbers with spaces inbetween each one, then press enter twice when done.: "); int num = in.read(); int [] list = new int [num]; int n = 0; int[] number = new int [num]; int [] ammount = new int [num]; while (in.ready()) { if (in.readLine().equalsIgnoreCase("e")) break; list[n] = in.read(); n++; } for(int i = 0; i < num -1; i++) { for(int k = i+1; k < num -1; k++) { if(list[i] == list[k]) { number[i] = list[i]; } } for (int p = 0; p < num; p++) { if(number[i] == list[p]) ammount[i]++; } } int order = 0; int temp = ammount[0]; for (int i = 0; i < ammount.length; i++) { if(ammount[i] > temp) temp = ammount[i]; order = number[i]; } System.out.println("The most popular sandwich was #" + order); } }