Hello everyone so this is the case. My lotto application works, but I just need a last thing to implement and I have a huuuuge problem with this. So I have 6 numbers saved in a "lotto.dat" file. 1, 2, 3, 4, 5, 6. If I don't choose to get new numbers, the app generates 6 new random numbers and compare them. This works.
But If I wan't 6 new numbers, save them in an ArrayList and PrintStream them into "lotto.dat", the file now contains the 6 numbers with brackets cus of the arrayList thing. I have a feeling that this might be the problem since when the new numbers are saved, it says there are no match even tho there is.
This is my numbers method:
Scanner scann = new Scanner(System.in);
File f = new File("lotto.dat");
PrintStream output = new PrintStream(f);
output.print(num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5 + " " + num6);
System.out.println("Your lotto numbers: " + num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5 + " " + num6);
System.out.println("Would you like to keep these numbers? y/n");
String yn = scann.nextLine();
if(!yn.equals("y")){
newNumbers();
}//While yn !y
In my newNumbers method I fill an ArrayList with the 6 newNumbs that is written in console. Then I printStream the arraylist into the "lotto.dat" which get overwrited.
Now this is my code where I compare the random numbers(numbers from ArrayList):
for(int n : numbers) { // go through all the numbers in the list
String match = doCompare(n); // doCompare metode
if(match.equals("true")){
List<Integer> numbersMatch = new ArrayList<>();
numbersMatch.add(n);
Thread.sleep(1000);
System.out.println();
System.out.println("Match on the number: " + n);
count++;
}
}
And here is my doCompare method:
Scanner sc = new Scanner(new File("lotto.dat"));
List<Integer> mn = new ArrayList<>();
while(sc.hasNextInt()){
mn.add(sc.nextInt());
}
if(mn.contains(n)){
String tf = "true";
return tf;
}
else{
String tf = "false";
return tf;
}
I've spent literally many hours trying to solve the problem but I can't. Why doesn't it compare the numbers? The only thing that really change, is that the new numbers saved in lotto.dat has "[]" inside the external file.