Java: Is reading String by String possible?

This is a discussion on Java: Is reading String by String possible? within the Tech Board forums, part of the Community Boards category; I couldn't find anything out there, except reading char by char or char array with specified length. I need to ...

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Java: Is reading String by String possible?

    I couldn't find anything out there, except reading char by char or char array with specified length. I need to read Strings from a text file like this:
    Code:
    string firstName, lastName;
    infile >> firstName >> lastName;
    I thought I could use *Scanner* but that's only available in java 1.5.

    thnx in advance
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    many thanks,
    was gonna bump up your reputation but not allowed by the system
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    that link is a good start but it reads line by line. if you want to separate strings (by spaces or any other char) you may want to consider a StringTokenizer.

    Code:
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class Foo {
        
        public static void main(String [] args) {
    	if(args.length != 1) {
    	    System.err.println("usage: java Foo file");
    	    System.exit(1);
    	}
    
    	try {
    
    	    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    	    String line;
    	    
    	    while( (line = in.readLine()) != null ) {
    		StringTokenizer toker = new StringTokenizer(line); // uses white space delimeter by default
    		while( toker.hasMoreTokens() )
    		    System.out.println(toker.nextToken());
    	    }
    	} 
    	catch(IOException e) {
    	    e.printStackTrace();
    	}
        }
    }

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    you're right. I started copying the code then I realized it reads *line by line*
    well I guess I have to use your method, that sucks..just kidding
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps I thought you wanted line by line for some reason

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 06:34 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21