1

i have following code:

Scanner input = new Scanner(System.in);
input.useDelimiter("\n")
System.out.println(input.next());
System.out.println("end");

when i run it and type "orange apple" then enter, it will print

orange apple

end

instead of

orange apple
end

which is what i actually wanted to print. i have no idea why it keeps printing an extra empty line. How can i fix this?

2 Answers 2

5

System.out.println already makes a new line as opposed to System.out.print which doesn't. So when System.out.println is used with \n you're making 2 new lines.

1
  • 1
    uhm sorry but i don't quite get what 'is used with \n' means...since \n is a delimiter, should it not have been excluded from what input.next() returns? Jul 22, 2015 at 22:47
0

System.out.println automatically move the cursor to next line and "\n" also do the same thing. So in your program you are using both that's why 2 new lines are entered. Either use System.out.println or use System.out.print with "\n".

1
  • I'm sorry but i don't understand what 'using both ways' means..i set '\n' as the delimiter so shouldn't input.next() only return "orange apple" since that's the token before the delimiter('\n')? Jul 29, 2015 at 16:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.