Results 1 to 4 of 4
Thread: Input validation help
- 10-17-2011, 01:53 PM #1
Input validation help
Hi, I'm trying to validate some user input. The int must be between 1 and 10, and unique to each line, and I thought this would work but I'm just getting the error message no matter what number I enter!
Java Code:for(int i=0; i < lines; i++) { for(int j=0; j<4; j++) { while(true){ userNums[i][j] = iBox.getInteger("Please enter number "+(j+1)+" for line "+(i+1)+": "); //myLottery.checkUnique(userNums[i][j]); if(userNums[i][j] >=1 && userNums[i][j] <= 10 && userNums[i][j] != userNums[i][0] && userNums[i][j] != userNums[i][1] && userNums[i][j] != userNums[i][2] && userNums[i][j] != userNums[i][3]){ break; } else { mBox.show("Number must be unique and between 1 and 10!"); continue; } } } }
- 10-17-2011, 06:31 PM #2
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: Input validation help
It would be more helpful if you gave the entire program, not just the loop that is giving you an error. It is difficult to try and find what is wrong without seeing what the whole program is
- 10-17-2011, 07:05 PM #3
Re: Input validation help
Thanks for the response. When I said error what I meant was the message telling the user the number must be unique and between 1 and 10. I'm still struggling to work this out, I can validate the between 1 and 10 part but I'm stuck on the unique number part! :(
Here's the whole class:
Java Code:/* 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){ int temp = iBox.getInteger("Please enter number "+(j+1)+" for line "+(i+1)+": "); List<Integer[]> uList = Arrays.asList(userNums); int z = 0; while (z < uList.size()) { if(uList[i][z].contains(j) != temp){ break; } else { mBox.show("Number must be unique within each line!"); continue; } j++; } if(userNums[i][j] >=1 && userNums[i][j] <= 10){ break; } else { mBox.show("Number must be 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; } } }
- 10-17-2011, 09:29 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Input validation help
I'm not entirely sure, but maybe try removing the continue statement and changing the j++ to z++Java Code:while (z < uList.size()) { if(uList[i][z].contains(j) != temp){ break; } else { mBox.show("Number must be unique within each line!"); continue; } j++; }
The way you have it now, it looks like it will go into an infinite loop if the else section in that loop is taken.
Similar Threads
-
Regex for validation of an input file from file
By _max_ in forum New To JavaReplies: 6Last Post: 07-11-2011, 11:40 PM -
Taking user input as a date and validation!
By Junki3JJC in forum New To JavaReplies: 3Last Post: 03-29-2011, 01:28 PM -
Input Validation Help
By Spyderpig in forum New To JavaReplies: 3Last Post: 02-18-2011, 11:58 AM -
JTable input validation
By Manfizy in forum New To JavaReplies: 10Last Post: 06-10-2009, 09:04 AM -
Input Validation
By kickflipper1087 in forum New To JavaReplies: 5Last Post: 11-03-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks