Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

delimiters and Scanner class

Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
beginner programmer having trouble with Scanner and delimiters.

Here is the code that does not work:

import java.util.Scanner;

public class ScannerTester {

public static void main(String[] args) {
Scanner s = new Scanner(;

System.out.println("Enter a double ");
double d = s.nextDouble();
System.out.println("Your double is " + d);
System.out.println("enter some more text\n");
String thisString = s.nextLine();

The problem is that there is no pause to input the string. The method apparently reads something without waiting for keyboard input. I tried changing the new Scanner line to

Scanner s = new Scanner("\n");

without success.

Am I right that a delimiter other than default is needed? If so, which one?

Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not quite. what you have to know is that a Scanner#nextDouble() will get the next token and return it if it parses into a well-formed double, but it does not eat the end of line mark. To do that, you should call scanner#nextLine() like so:
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!