java - How to compare Numbers from a file with random numbers? -
hello case. lotto application works, need lastly thing implement , have huuuuge problem this. have 6 numbers saved in "lotto.dat" file. 1, 2, 3, 4, 5, 6. if don't take new numbers, app generates 6 new random numbers , compare them. works.
but if wan't 6 new numbers, save them in arraylist , printstream them "lotto.dat", file contains 6 numbers brackets cus of arraylist thing. have feeling might problem since when new numbers saved, says there no match tho there is.
this 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 maintain these numbers? y/n"); string yn = scann.nextline(); if(!yn.equals("y")){ newnumbers(); }//while yn !y
in newnumbers method fill arraylist 6 newnumbs written in console. printstream arraylist "lotto.dat" overwrited.
now code compare random numbers(numbers arraylist):
for(int n : numbers) { // go through numbers in 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 number: " + n); count++; } }
and here 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"; homecoming tf; } else{ string tf = "false"; homecoming tf; }
i've spent literally many hours trying solve problem can't. why doesn't compare numbers? thing change, new numbers saved in lotto.dat has "[]" within external file.
you said
but if wan't 6 new numbers, save them in arraylist , printstream them "lotto.dat", file contains 6 numbers brackets cus of arraylist thing.
considering statement above you'll have problem code below since when scanner see bracket while loop exit , won't have int in array
while(sc.hasnextint()){ mn.add(sc.nextint());}
now problem clear may seek having loop writing @ file prevent brackets included in file or may utilize regex eliminate characters not int file before reading.
java methods arraylist printstream
No comments:
Post a Comment