I am not sure what is wrong with my While Loop because if select ("Press 0 to edit info."), I will not get prompt for "Name" anymore.

Another thing is my exception does not work. I wanted to deny user from entering alphanumeric under "Mobile". Only numeric allowed here.

Pls advise. Tks.

import java.io.*;
import java.util.*;

public class Profile {

    public static void main (String [] args) {

    String name="",user="",pass="",add="",con="";
 	int done = 0;
 	int ch = 500;


   Scanner scn = new Scanner(System.in);
		while(done != 1){


 	 		System.out.println("Enter your profile\n");
 	 		System.out.print("Name:");
 	 		name = scn.nextLine();

 	 		System.out.print("Username:");
 	 		user = scn.nextLine();

 	 		System.out.print("Address:");
 	 		pass = scn.nextLine();

			//try{

 	 		System.out.print("Mobile:");
 	 		con = scn.nextLine();

			//}
 	 		/*catch ( InputMismatchException inputMismatchException )
        		 {
          			  System.err.printf( "\nException: %s\n", inputMismatchException );
            			  scn.nextLine(); // discard input so user can try again
            			  System.out.println("You enter nos. Please retry again.\n" );
         		} // end catch*/

			System.out.println("");

 	 	    System.out.println("Press 0 to edit info.");
 	 	    System.out.println("Press 1 to save.");
 	 	    System.out.println("Press 2 to quit w/o saving.");


 	 		System.out.print("Select a choice:");
 	 		ch = scn.nextInt(); //Read in user prompt (0, 1 or 2)

 	 		if (ch != 2)
				done = ch;
 	 		else
 	 			done = 1;

 	 }//End of while loop

 	if(ch!=2 ){

 	String info= "\r\n"+name+";"+user+";"+add+";"+con;
 	try {

 	PrintWriter out = new PrintWriter(new FileWriter("Profile.txt",true));
 			out.write(info);
 			out.close();
 		}

 	catch (IOException e) { }
 	}//if

 	System.out.println("Tks for registering.");

 }
}//END

Edited 6 Years Ago by avataralien: n/a

import java.io.*;
import java.util.*; 

public class Profile {

    public static void main (String [] args) {

    String name="",user="",pass="",add="";
    int ch=0, con=0;
   Scanner scn = new Scanner(System.in);

    while(true){
        System.out.println("Enter your profile\n");
        System.out.print("Name:");
        name = scn.nextLine();
        System.out.print("Username:");
        user = scn.nextLine();
        System.out.print("Address:");
        pass = scn.nextLine();

    while(true){  // only numeric allowed 
    try{
    System.out.print("Mobile:");
    con = scn.nextInt();
    }catch ( InputMismatchException e ){
          scn.nextLine(); 
          System.out.println("You enter nos. Please retry again.\n" );
          continue;
        } 
        break;
        }
        System.out.println("");
        System.out.println("Press 0 to redo info.");
        System.out.println("Press 1 to save.");
        System.out.println("Press 2 to quit w/o saving.");

        System.out.print("Select a choice:");
        ch = scn.nextInt(); //Read in user prompt (0, 1 or 2)

        switch(ch){
            case 0:
            System.out.println("Editing...");
            continue;
            case 1:
            String info= "\n"+name+";"+user+";"+add+";"+con;
            try {
    PrintWriter out = new PrintWriter(new FileWriter("Profile.txt",true));
            out.write(info);
            out.close();
            System.out.println("Tks for registering.");
            }catch (IOException e) { };
            System.exit(0);
            case 2:
            System.out.println("Tks for registering.");
            System.exit(0); 
            }   
        }
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

For example if you enter: A word to the wise <PRESS ENTER>
and use next() only A is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.

Tks. I will study the exception code segment.

Edited 3 Years Ago by Nick Evan: Fixed formatting

I think I got my problem solved. I added

scn.nextLine(); after ch = scn.nextInt();

and it works.

Thank you.

Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

For example if you enter: A word to the wise <PRESS ENTER>
and use next() only A is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.

This question has already been answered. Start a new discussion instead.