• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner methods

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put together code to read a line of user input which should be in the form of a sentence. Then split it into its constituent words, ignoring punctuations and spaces,these words are then stored in an array, and the array is printed out.
I used the scanner class to read user input.
I used two methods from the scanner class:next() and nextLine() and got different results which I am having a tough time understanding.
Both methods seem to read the user input, but when using the next() method, my code reads only the first word in the sentence and ignores the rest. While the nextLine() method processed the code perfectly and split all the whole sentence.

I used the regex \W to split the line.

The 1.5 API defines the methods as such:
next()-Finds and returns the next complete token("what exactly is a token?") from this scanner.
nextLine()-Advances this scanner past the current line and returns the input that was skipped.

The first one returns the next token, the second one returns a previous line of input. Please explain.

My code with nextLine()(works as per requirements):

public void Splitstrg()
{
String br[]= new String[50];
int i;
String empt = "",line;
System.out.print("Please enter a sentence\n");
Scanner input = new Scanner(System.in);
line = input.nextLine();
br = line.split("[\\W]");
//System.out.println(br[0]+br.length);
for(i=0;i<br.length;i++)
{
if (br.compareTo(empt)==0)
continue;
System.out.println(br);
}
}

My code with next()(Ignores all except first word):
public void Splitstrg()
{
String br[]= new String[50];
int i;
String empt = "",line;
System.out.print("Please enter a sentence\n");
Scanner input = new Scanner(System.in);
line = input.next();
br = line.split("[\\W]");
//System.out.println(br[0]+br.length);
for(i=0;i<br.length;i++)
{
if (br.compareTo(empt)==0)
continue;
System.out.println(br);
}
}

Any help is appreciated.>
 
Marshal
Posts: 79259
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The next() method. . . reads only the first word in the sentence and ignores the rest.

Have you looked in the API about Scanner? That is what it is supposed to do.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic