i am using scanner class, i faced problem when i tried to get a string with space. How can i modify my code to solve the problem. Here is my code:

import java.util.Scanner;

class abc
{
public void main(String[] args)
{
Scanner abc = new Scanner(System.in);
System.out.print("Name: ");
String nameStr = abc.next();
}
}

Recommended Answers

All 5 Replies

If this is what you want to enter: John Smith, then try inserting
"John Smith"
By the way you don't say what exactly is you problem and what you are trying, to do. Post your inputs, what it is stored in the variable and what you wanted to be stored

Scanner.next() returns up to the next delimeter, which is a space by default

what do you expect it to do?

if you want to read the rest of the line, you can use the "nextLine()" method

i think this should solved the problem. Sorry for confusing because i am still new in java.

thanks alot but i want the method for accept the space

thanks alot but i want the method for accept the space

Try nextLine

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.