Help with Scanner Class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help with Scanner Class

  1. #1
    Join Date
    Sep 2006
    Posts
    23

    Help with Scanner Class

    I need to find a way to take in either a char or a string using scanner class.
    Could someone help me with how to do it

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The "tokenized" object(s) in a scanner are strings. You take in the next token by using next() ... If you want to use the string a char at a time, use the string methods to accomplish this.

  3. #3
    Join Date
    Mar 2005
    Location
    Istanbul
    Posts
    10
    Scanner reader = new Scanner(new File("filename.txt"));
    StringTokenizer wordsInLine=new StringTokenizer(reader.nextLine()); //this returns a line
    String word=wordsInLine.nextToken(); //this returns a word in the line

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Why would you use a StringTokenizer in this? Just use something like this (using as an example, a prior question in another thread):

    Code:
    Scanner reader = new Scanner(new File( "filename.txt" )).useDelimiter( "\n");
    
    Scanner currentLine;
    
    while (reader.hasNext())
    {
         currentLine = new Scanner(reader.next());
         price = currentLine.nextFloat();
         StringBuilder sb = new StringBuilder();
         while (currentLine.hasNext())
         {
               sb.add(currentLine.next() );
         }
         item = sb.toString();
    
    }
    This will break the file into line sized tokens, first. Then you take each line, create a scanner object now using whitespace (the default delimiter for Scanner objects) to tokenize the line. Assign the first token to the float price, then read the remaining tokens into a StringBuilder object, assign it to the string item by then doing a toString() of the stringbuilder object.

    If you want to read a char, you need to do

    Code:
    char c = '';
    
    for( int i = 0; i < item.length(); ++i )
    {
           c = item.charAt(i);
           .....
           do what you're going to do with the char;
           .....
    }
    Last edited by nspils; 09-23-2006 at 07:46 PM.

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  3. Replies: 5
    Last Post: 10-31-2005, 02:50 AM
  4. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  5. Replies: 5
    Last Post: 10-17-2002, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center