0

After using next() to skip the extra line after a user entry I try and get nextLine() to pick up the variable name that the user is currently entering. My program is using a text based menu and this is the only way I have been able to make the menu flow smoothly (along with a do-while loop and what have you but I digress) My problem is this: while I know that the object that I am trying to name is receiving data from my main method, it is not giving it the proper name, just giving it a blank character. I know this because it renames the object, but it does not rename the object correctly, it just gives in a space (i give the name variable for the object a "N/A" in the constructor method). How can I combat this problem now and in the future?

Main method snippit

        String last;        
        selector = in.nextInt();
        if (selector == 1)
        {
           System.out.print("Please enter Last Name: ");
           in.next();
           last = in.nextLine();
           entry.setLast(last);
           terminator = true;
        }

Object naming method

   private static String last_name;
   public static String setLast(String a)
   {
      last_name = a;
      return last_name;
   }

this seems like a simple problem but I need some outside perspective! I do not think that im focusing on the true issue here. Thank you

2 Answers2

0

The problem is right here:

in.next();
last = in.nextLine();
entry.setLast(last);

The in.next(); line will read the first available token (sequence of nonwhitespace characters) and just discard it from the input.

The last = in.nextLine(); saves the rest of the current line of input into the String last, but the last name that you want was already discarded, so there is nothing left in the line. The first character in the input that in.nextLine() sees will be a newline, so it just returns an empty string as if there were nothing in the line.

You want to save the String returned by in.next(), not in.nextLine() like so:

last = in.next();
in.nextLine();
entry.setLast(last);
mikebolt
  • 676
  • 4
  • 11
0
package stackoverflow.q_24947751;

import java.util.Scanner;

public class UserInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("To exit please type 'Quit'");
        while (sc.hasNext()) {
            String input = sc.nextLine();
            if(!input.equalsIgnoreCase("Quit")) {
                System.out.println("Enter first name");
                String name = sc.nextLine();
                System.out.println("Enter surname");
                String surname = sc.nextLine();
                System.out.println("Enter number");
                try {
                    Integer.parseInt(sc.nextLine());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }
    }
}

//Output:
//To exit please type 'Quit'
//Proceed
//Enter first name
//Nikhil
//Enter surname
//Joshi
//Enter number
//22
Nikhil Joshi
  • 801
  • 1
  • 8
  • 31