Webmonkey wrote: » Surely, you generate 4 random numbers first, and then for each line the user enters, check each line against the 4 numbers.
Adam wrote: » So I spoke to the lecturer about this, because I knew if I was stuck everybody was too, a lot of people are struggling already! Turns out she wasn't aware that the unique numbers problem was included, and said not to worry about it! However, I want to know now for my own learning purposes, so can anybody tell me how it should be done? Here's my main class code, which all works as is. [php] /* LotteryApp.java */ import java.util.Arrays; import java.util.ArrayList; import javabook.*; class LotteryApp { public static void main(String args []) { //declare variables double money; int lines; int matches; double winnings = 0; double totalWinnings = 0; int[] lottoNums; int[][] userNums; //declare and create objects MainWindow mWin = new MainWindow(); InputBox iBox = new InputBox(mWin); MessageBox mBox = new MessageBox(mWin); LotteryNumbers myLottery = new LotteryNumbers(); Money myMoney = new Money(); StringBuffer messageOut = new StringBuffer(); mBox.show("Welcome to Java Lotto!"); money = iBox.getDouble("Please enter the amount of money you wish you use:"); myMoney.setMoney(money); while(myMoney.getMoney() >= 0.5) { while(true){ lines = iBox.getInteger("How many lines do you wish to play?"); if(lines >= 0 && lines <= 4){ break; } else { mBox.show("Must be a number between 1 and 4, or 0 to quit!"); continue; } } if(lines == 0){ mBox.show("Thanks for playing!"); System.exit(0); } myMoney.playCharge(lines); userNums = new int[lines][4]; for(int i=0; i < lines; i++) { for(int j=0; j<4; j++) { while(true){ userNums[j] = iBox.getInteger("Please enter number "+(j+1)+" for line "+(i+1)+": "); //myLottery.checkUnique(userNums[j]); if(userNums[j] >=1 && userNums[j] <= 10){ break; } else { mBox.show("Number must be unique and between 1 and 10!"); continue; } } } } lottoNums = myLottery.generateNumbers(); messageOut.append("===============================================\n"); messageOut.append("===================Java Lotto===================\n"); messageOut.append("===============================================\n"); messageOut.append("\n\n"); messageOut.append("Lotto Numbers:"); for(int nums=0; nums<4; nums++) { messageOut.append(" "+lottoNums[nums]); } messageOut.append("\n\n"); messageOut.append("Your numbers are: \n\n"); for(int x=0; x<lines; x++) { matches = 0; messageOut.append("Line "+(x+1)+": "); for(int y=0; y<4; y++) { messageOut.append(userNums[x][y]); messageOut.append(" "); for(int z=0; z<4; z++){ if(userNums[x][y] == lottoNums[z]){ matches++; } } } messageOut.append("\n"); winnings=myMoney.getWinnings(matches); messageOut.append("You matched "+matches+" numbers for \u20ac"+winnings); myMoney.addWinnings(winnings); messageOut.append("\n\n"); totalWinnings += winnings; } messageOut.append("You won \u20ac"+totalWinnings+" this round!"); messageOut.append("\n\n"); messageOut.append("You have \u20ac"+myMoney.getMoney()+" remaining!"); mBox.show(messageOut); messageOut=null; } } } [/php]Thanks!
Adam wrote: » My original question was how to validate the input to be both between 1 and 10 and unique numbers within each line.
Adam wrote: » My thinking exactly, I was attempting to do this at the input stage where I'm checking if the number is in the correct range, but I kept getting unreachable statement errors. My logic was at input, take the value in to a temp var, iterate through the existing elements in the current line and check each element against the temp var, and if i get a hit present the dialogue again. However I couldn't get it to work! I started going down the route of using a set, and counting the elements after each input until there was four since a set will only allow unique elements, but I couldn't get that to work either!
Adam wrote: » Thanks for taking the time to help me, I think I'm closer now, but i'm getting an out of bounds exception on the array in my checkUnique method. [php] // i = the line number incrementing for(int i=0; i < lines; i++) { // j = the four elements within each i for(int j=0; j<4; j++) { while(true) { newNum = iBox.getInteger("Please enter a number"); if(checkUnique(userNums, newNum, lines) == 1 && newNum > 0 && newNum < 11) { userNums[j] = newNum; break; } else { mBox.show("Number already in this line!"); continue; } } } } [/php]checkUnique method: [php] public static int checkUnique(int[][] userNums, int newNum, int line) { int result = 0; for(int x=0; x<=userNums.length; x++){ if(userNums[line][x] == newNum) { result = 0; } else { result = 1; } } return result; } [/php]
for(int x=0; x<=userNums.length; x++)
for(int x=0; x<userNums.length; x++)
for(int x=0; x<userNums.length; x++){ if(userNums[line][x] == newNum) { result = 0; } else { result = 1; } }
Adam wrote: » Ok I did that. I also corrected the value for the line I was passing to the checkUnique method, I was passing the number of lines chosen rather than the line number the loop was currently on! So now it's almost working, except that it's letting me add one duplicate i.e. I can add 1 twice on the same line, but not a third time... I'm thinking the hole in my logic is in the checkUnique function, am I right in saying that this blockfor(int x=0; x<userNums.length; x++){ if(userNums[line][x] == newNum) { result = 0; } else { result = 1; } } is wrong, for example if the line has three values in it, and it finds that the numbers on the same on the second loop but the numbers are different on the third loop it will return that the new number is not a match? Or does the return command cut the loop short regardless of the counter?
if(userNums[line][x] == newNum)
public static int checkUnique(int[][] userNums, int newNum, int line) { int unique = 1; for(int i=0; i<=userNums.length; i++) { if(userNums[line][i] == newNum) unique = 0; } return unique; }
Adam wrote: » public static int checkUnique(int[][] userNums, int newNum, int line) { int unique = 1; for(int i=0; i<=userNums.length; i++) { if(userNums[line][i] == newNum) unique = 0; } return unique; } So bloody simple! :rolleyes: One more question: Would it be considered bad practice to set the value of unique as true until proven otherwise in the loop? Thank you for your time, I really appreciate it.