Results 1 to 4 of 4
  1. #1
    AdamG's Avatar
    AdamG is offline Member
    Join Date
    Jan 2011
    Location
    Ireland
    Posts
    4
    Rep Power
    0

    Default 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;
                            }
                        }
                    }
                }

  2. #2
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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

  3. #3
    AdamG's Avatar
    AdamG is offline Member
    Join Date
    Jan 2011
    Location
    Ireland
    Posts
    4
    Rep Power
    0

    Default 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;
    
    		}
    
    
    
    	}
    }

  4. #4
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Input validation help

    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++;
                            }
    I'm not entirely sure, but maybe try removing the continue statement and changing the j++ to z++
    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

  1. Regex for validation of an input file from file
    By _max_ in forum New To Java
    Replies: 6
    Last Post: 07-12-2011, 12:40 AM
  2. Taking user input as a date and validation!
    By Junki3JJC in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 02:28 PM
  3. Input Validation Help
    By Spyderpig in forum New To Java
    Replies: 3
    Last Post: 02-18-2011, 12:58 PM
  4. JTable input validation
    By Manfizy in forum New To Java
    Replies: 10
    Last Post: 06-10-2009, 10:04 AM
  5. Input Validation
    By kickflipper1087 in forum New To Java
    Replies: 5
    Last Post: 11-03-2008, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •