1

I was wondering how I can do this.

I was told to use kb.nextLine() instead of kb.next() , but that just results in the input being skipped when I run the program.

String address;
student.setAddress(address = kb.nextLine()); 

I was then told to do this to fix it:

student.SetAddress(String address = kb.nextLine());

but I get an error: String cannot be resolved into a variable" "Syntax error on token "address"

1
  • 3
    Can you show more of your code? The first line you posted should work, but without the context, it's hard to say where the problem is. Commented Nov 2, 2014 at 6:39

1 Answer 1

5

Your second example is wrong. Your first example looks okay. But not great. I assume you've been using something like nextInt() or nextDouble(). They leave a trailing new line, so you need to consume it.

kb.nextLine(); // <-- consume empty trailing line.
String address; 
student.setAddress(address = kb.nextLine()); //<-- reads line.

If you don't need a local copy of address,

kb.nextLine(); // <-- consume empty trailing line.
student.setAddress(kb.nextLine()); //<-- reads line.

As Scanner.nextLine() Javadoc says,

Advances this scanner past the current line and returns the input that was skipped.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.