Results 1 to 12 of 12
  1. #1
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Restart a program if wrong input

    Hi i have this class:
    Java Code:
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class PrimaClasse {
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		System.out.println("WELCOME!");
    		try{
    			Thread.sleep(1000);
    			} catch(InterruptedException ie) {}
    		System.out.println("CHOOSE  | 1 |  IF YOU ARE A REGISTERED USER");
    		System.out.println("  OR");
    		System.out.println("CHOOSE  | 2 |  IF YOU ARE A NEW USER");
    		
    		Scanner value = new Scanner(System.in);
    		int option_choosed = value.nextInt();
    		
    		if(option_choosed == 1){
    			System.out.println("Enter your username:");
    			Scanner name = new Scanner(System.in);
    			String name_used = name.nextLine();
    			
    			UsersCheck checking_information = new UsersCheck();
    			checking_information.controlloNomi(name_used);
    			
    			int response = checking_information.controlloNomi(name_used);
    			
    			
    			if(response == 1){
    				System.out.println("Now you can choose a level:");
    				System.out.println("1  |  2  |  3  |  4  |  5  |  6");
    				Scanner level = new Scanner(System.in);
    				int level_choosed = level.nextInt();
    				
    				LevelCreation oggettoTerza = new LevelCreation();
    				oggettoTerza.ChoosingLevel(level_choosed);
    			}
    		}else{
    			System.out.println("ENTER YOUR NAME FOR CREATE YOUR ACCOUNT");
    			
    			Scanner name_registration = new Scanner(System.in);
    			String new_name = name_registration.nextLine();
    			
    			SavingUser user_saving = new SavingUser();
    			user_saving.salvaUtente(new_name);
    		}
    	}
    }
    with other 3 class (UserCheck, SavingUser, LevelCreation) and for now all it's going well.
    NOW i want that, in the main class, if i insert a wronf username (that is passed to the class UserCheck, which return 1 if is ok and 0 if is wrong) the program restart from the beginning.
    And if is possibile i wanna make it restar also in the case when i choose the option 2 and inser a new username that will be saved in a txt file.

    i have no idea how make this happen

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Restart a program if wrong input

    Why you want to start the entire program from the beginning? All you have to do is to prevent the user from giving you wrong input. I give you a simple example below:

    Java Code:
    import java.util.*;
    
    public class Test{
    
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		int option_choosed = -1 ; // just a not correct value
    		while(option_choosed != 1 && option_choosed != 2){
    			System.out.println("CHOOSE  | 1 |  IF YOU ARE A REGISTERED USER");
            	System.out.println("  OR");
            	System.out.println("CHOOSE  | 2 |  IF YOU ARE A NEW USER");
    			try{
    			option_choosed = input.nextInt();
    		}catch(InputMismatchException e){
    			// e.printStackTrace() // this will print the error trace
    			// someone just give a not integer value
    			// for example string or double.
    			input.next(); // refresh scanner and discard the previous value
    		}
    		}
    	}
    }
    Just some tips.
    If you didn't add the input.next() (which is scanner.next() ) you will fall in the infinitive loop.
    It's good to give objects names referred to their objects for example you wrote :

    Scanner value = new Scanner(System.in);
    Scanner name_registration = new Scanner(System.in);
    Scanner name_registration = new Scanner(System.in);

    which are the same objects and hard to read them cause the value object doesn't sound like scanner object if you understand what i mean.

    Reading again that you say i maybe haven't understand you correct :P
    I guess you want to do something like this :

    Java Code:
    import java.util.*;
    
    public class Test{
    
    	public static void main(String args[]){
    
    		Scanner input = new Scanner(System.in);
    		boolean accessGranded ;
    		while(!accessGranded){
    			int option_choosed = -1 ; // just a not correct value
    			while(option_choosed != 1 && option_choosed != 2){
    				System.out.println("CHOOSE  | 1 |  IF YOU ARE A REGISTERED USER");
    	        	System.out.println("  OR");
    	        	System.out.println("CHOOSE  | 2 |  IF YOU ARE A NEW USER");
    				try{
    					option_choosed = input.nextInt();
    				}catch(InputMismatchException e){
    					// e.printStackTrace()
    					// someone just give a not integer value
    					// for example string or double.
    					input.next(); // refresh scanner and discart the previous value
    				}
    			}
    
    			if(option_choosed == 1){
                	System.out.println("Enter your username:");
                	String name_used = input.nextLine();
                 
                	UsersCheck checking_information = new UsersCheck();
                 
                	int response = checking_information.controlloNomi(name_used);
                 	if(response == 1){
                 		accessGranded = true;
                 	}
    	        }else{
    	            System.out.println("ENTER YOUR NAME FOR CREATE YOUR ACCOUNT");
    	            String new_name = input.nextLine();
    	             
    	            SavingUser user_saving = new SavingUser();
    	            user_saving.salvaUtente(new_name);
    	        }
    		}
    		// we have take access so we continue with out program
    		System.out.println("Now you can choose a level:");
            System.out.println("1  |  2  |  3  |  4  |  5  |  6");
                    
            int level_choosed = input.nextInt();
                     
            LevelCreation oggettoTerza = new LevelCreation();
            oggettoTerza.ChoosingLevel(level_choosed);
    
    	}
    }
    In general i advice you to protect every scanner input like the first one i did (i bored to do the others.. :P ) cause not every user will be friendly.
    Last edited by ShadowWalker; 02-07-2015 at 06:56 PM.

  3. #3
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Restart a program if wrong input

    UsersCheck checking_information = new UsersCheck();
    // checking_information.controlloNomi(name_used); dunno why you call it twice

    UsersCheck is another class in my program and controlloNomi is his method. i use a checking_information variable to save the return value from the class, this value is used after in this mani class

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Restart a program if wrong input

    Yes i understand that point but correct me if i am wrong you create an object of UsersCheck Class like this:
    UsersCheck checking_information = new UsersCheck();
    And then you call the same method twice .. one without keeping the return value and one with it.. and i just can't understand why..

    Java Code:
    checking_information.controlloNomi(name_used);
    int response = checking_information.controlloNomi(name_used);
    I mean only one call of the controlloNomi(String name) method is enough i guess.. anyway i hope the code i gave you has solved the problem. You just need a way to know if the user has enter the "System" correctly so the program can move on , otherwise the user will have to repeat the same process until he grands access
    Last edited by ShadowWalker; 02-07-2015 at 05:39 PM.

  5. #5
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Restart a program if wrong input

    ok i solved...

    this:
    Java Code:
    UsersCheck checking_information = new UsersCheck();
    checking_information.controlloNomi(name_used);
    			
    int response = checking_information.controlloNomi(name_used);
    became this:
    Java Code:
    UsersCheck checking_information = new UsersCheck();
    int response = checking_information.controlloNomi(name_used);
    Last edited by maki; 02-07-2015 at 05:55 PM.

  6. #6
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Restart a program if wrong input

    i lookd at your code, but sorry i think you not understand what i want to do, you wrote a code that is a user insert a wrong number for the menu, like 4, the system ask again for a number until is 1 or 2.
    but i not want this, i want that if the user choose 1:
    ask him username, and now the system made this, but the username is wrong the system stopped, now instead i want that restart from the beginning.
    and so the point 2, is i save my name in the list i wanna star the program again.

    my english is not so good so probabily is my fault for your misunderstanding. sorry, can help?

  7. #7
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Restart a program if wrong input

    My English is not very good as well. But i think that understand you correct..(at least i hope so).
    I am not sure you understand my code correct..
    Now let's take everything from the begging. You are trying to make a class where you ask the user to choose between two options.

    Option 1 : Will prompt the user to insert a name and then check the programs database to verify the user information. After that if the informatio is correct continue with the choosing of level.
    Otherwise if the user name doesn't exists repeat the process again (choose between option 1 or 2 cause he fail to enter in the "System").

    Option 2 : Will prompt the user to insert a name which will be stored in programs database and then ask your to choose again between two options.

    As you can see my code to achieve that we need to have a variable(let's call it access_variable) which will inform the program if the user has access (which means that he enter correct information in your case the username and he can go on with the rest of the program). So while the user hasn't access to the "System" we will ask him to choose between of try to login or to make an account (choice 1 or 2). In choice 1 he enter a name and then the program check if the information is correct and if so then inform access_variable and leave the while loop. If the information is not correct then because everything is in a while loop the whole process will begin from the top. In Option 2 the user enter a name and when the write to file ends (in your other class) the whole process will begin again from the top so the user will have to use the option 1 to make the 'log-in".

    If again i haven't understand correct then i am sorry.. :P
    Last edited by ShadowWalker; 02-07-2015 at 06:36 PM.

  8. #8
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Restart a program if wrong input

    YES!!! you understand right. Maybe my english is not so terrible as i was thinking...


    So probably i'm making confusion... i make better go to read very carefully your code and try to manually apply to mine for understand the passage (in a logical way i understand and know the routine, is the scripture method that i miss)


    edit: sorry, but is not allowed post image or gifs?
    Last edited by maki; 02-07-2015 at 11:56 PM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Restart a program if wrong input

    Please delete your previous post (before a moderator does it for you).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Restart a program if wrong input

    ok i made your code step by step but i have some problem.

    1- eclipse say and obligate me to initialize the variabile accessGranded.
    if i initialize it as true the system jump login and goes directly asking me for the level
    if i initialize it as false the output is:
    XML Code:
    WELCOME!
    CHOOSE  | 1 |  IF YOU ARE A REGISTERED USER
      OR
    CHOOSE  | 2 |  IF YOU ARE A NEW USER
    1
    Enter your username:
    You are a registered member, WELCOME BACK!
    Now you can choose a level:
    1  |  2  |  3  |  4  |  5  |  6
    mario
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at PrimaClasse.main(PrimaClasse.java:50)
    as you can see i insert "1" and the system stamp all witout wait for the name and when i insert name... errors many errors
    and with 2 even worse
    Last edited by maki; 02-08-2015 at 02:07 AM.

  11. #11
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Restart a program if wrong input

    why if i try to send private message i obtain a blank page with "page not found"?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Restart a program if wrong input

    Quote Originally Posted by maki View Post
    edit: sorry, but is not allowed post image or gifs?
    Well, I don't like posting images but others do it. I find them distracting. But they are not against forum rules. But when it comes to large animations or videos of sorts (which is what you posted), I have seen the moderators remove them. So I thought I would alert you. If I am incorrect then a moderator can certainly restore your original post.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to restart the program once an answer has been found?
    By OldSkoolBray in forum New To Java
    Replies: 2
    Last Post: 10-29-2014, 12:56 PM
  2. Help A Program to Restart
    By Icyis in forum New To Java
    Replies: 12
    Last Post: 11-23-2013, 09:30 PM
  3. Replies: 6
    Last Post: 06-19-2013, 05:36 PM
  4. How can I restart my jvm and program programmatically?
    By guest_user in forum Advanced Java
    Replies: 7
    Last Post: 08-30-2011, 02:45 PM
  5. Replies: 6
    Last Post: 11-04-2009, 09:41 PM

Posting Permissions

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