1

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.

user3703289
  • 347
  • 1
  • 3
  • 9
  • 1
    Sorry, my coffee is not working correctly so I need a bit of clarification. Your exact problem is: _"Comparison of numbers from a file is not working if the numbers are newly generated?"_ Am I right? – Keale Oct 31 '14 at 03:31
  • Your `doCompare()` method should return a `boolean`, not a `String` that has to be compared to `true` or `false`. Your question remains obscure. – user207421 Oct 31 '14 at 05:03

1 Answers1

0

You said

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.

considering your statement above you'll have a problem with your code below since when the scanner see the bracket your while loop will exit immediately and won't have any int in your array

 while(sc.hasNextInt()){
 mn.add(sc.nextInt());}

Now that your problem is clear you may try having a for loop for writing at the file to prevent those brackets to be included in your file or you may use regex to eliminate those characters that are not int from your file before reading.

geno
  • 78
  • 2