Hi ,I am learning Java ,and I have problem ,when I call this method form main and chose 1 it is printing enter name and enter priority without letting me enter the data
how can I fixed that
Thanks

public void makeChoise(){
	switch(display())
	   {
	    case 1:System.out.print("Enter First and Last Name: ");
		       setName(input.nextLine());
		       System.out.print("Enter Priority: ");
		   	   setPriority(input.nextInt());
	    break;
	    case 2:System.out.println("Display priority");
	    break;
	    case 3:System.out.println("display by name");
	    break;
	    case 4:System.out.println("exit");
	    break;
	    default : System.out.println("wrong choise");
	    break;
	}
	}

Recommended Answers

All 6 Replies

It is because you are passing it to your setName() method without actually taking a value! Try storing input.nextLine() in a separate String variable first. Then pass that new variable which contains the input into your setName method!

Thanks ,I did that ,but it is doing the same

System.out.print("Enter First and Last Name: ");
		       String name=input.nextLine();
		       setName(name);
		       System.out.print("Enter Priority: ");
		   	   int priority=input.nextInt();
		   	   setPriority(priority);

Ok, scratch what I said, you are able to directly place a nextLine() call into a method. Sorry about that. I just noticed you pass a method in the switch statement, what value does this give? What does it do? I would speculate the reason it is not allowing you to input is because of the nextLine() method. You would usually use this for searching text files. You should use input.next() instead when taking String input.

It is returning the choise 1,2,3 or 4
Thanks

public int display(){
	System.out.println("1.Add new patient");
	System.out.println("2.Display by priority");
	System.out.println("3.Display by name(sorted by last name)");
	System.out.println("4.Exit");
	System.out.print("Enter your choise: ");
	int choise=input.nextInt();
	return choise;
	}

Thanks ,the input next works,if I have string without spaces,but if I do have John Smith ,gives me error,how can I fixed that

Oh, ok well, I have a solution to that but I doubt it is the best way to go about it. I can't actually think properly atm. But you will have to use nextLine() again to take the full line. If you call the nextLine() function before you ask for the proper input, it should skip that line and allow you to enter on the next one.

input.nextLine()
System.out.print("Enter First and Last Name: ");
		       String name=input.nextLine();
		       setName(name);
		       System.out.print("Enter Priority: ");
		   	   int priority=input.nextInt();
		   	   setPriority(priority);

I didn't tell you this the first time because, well, it's not a very good solution. But it is the best I know. If you are feeling adventurous though, you could try using the BufferedReader Class.

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

 case 1:
               System.out.println("enter your name: " );
               try{
                setName(in.readLine());
              }catch(IOException e) {
       System.err.println("Error: " + e);
              }
case 2:
               System.out.println("enter your age: " );
               try{
                setAge(Integer.parseInt(in.readLine()));
              }catch(IOException e) {
       System.err.println("Error: " + e);
              }

That will work...You will need to parse the string in your display() method aswell to be able to return a value. Or you can use the Scanner class for that method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.