This is a part of my code.

String name=sc.nextLine();
int no=sc.nextInt();

The problem is if I write sc.next() instead of sc.nextLine() then i can pass only "james" not "james watson".
when i write sc.nextLine() then it takes integer not string.Why is it so?
Please Help!!!

This is a typical problem with Scanner. Most of the methods use the delimiter (default white space) to separate tokens, and keep the following delimiter. Except for readLine that discards the following new line character(s). So, for example if you use a nextInt followed by a nextLine with a file that has "123\n a line of text" the nextLine will unexpectedly return "" - the remainder of the line that's left after the nextInt. To fix that you need an extra, dummy, readLine toget past the newLine character.
Unless you need to use Scanner it's often easier in the lon run to use a BufferedReader and its readLine() method, then parse each line yourself using String's split method and parsers like Integer.parseInt

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.