Results 1 to 2 of 2
  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Read the first word of every line in a txt file

    I want to read the contents of a text file into an ArrayList of objects, each object containing 2 String values. To make it easier to explain, here is an example:

    input.txt:

    124 Line 1
    453 Yet another line
    948 And 1 more line - The Final Line


    Java Code:
    class object{
    	public String num;
        public String string;
    
    	public object(String n, String s){
    		num = n;
    		string = s;
    	}
    }
    
    class readFile{
    	public static void main(String args[]){
    		ArrayList<object> list = new ArrayList<object>();
    
    		try{
    		BufferedReader file = new BufferedReader(new FileReader("input.txt"));
    		String line;
    		
    		while((line = new String(file.readLine())) != null) {
    			String firstWord = new String();
    			String otherContent = new String();
    			
    			/* firstWord = first word of current line */
    			/* otherContent = other words on the same line */
    			
    			object myObject = new object(firstWord, otherContent);
    			list.add(myObject);
    		}
    		file.close();
    		}catch(Exception e){System.err.println(e);}
    		
    		for(int i = 0; i < list.size(); i++){
    			System.out.println(list.get(i).num + " " + list.get(i).string);
    		}
    	}
    }
    The above code would need to produce output which is the same as input.txt.

    The only way I can think of to achieve the desired result is by splitting the string and then reassembling the resulting array into two strings. But I guess I am hoping for a more efficient method for doing this?

    Thanks in advance

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Read the first word of every line in a txt file

    What is this code supposed to do? Why mix creating a new String with the testing for the end of the input?
    Java Code:
     while((line = new String(file.readLine())) != null) {
    Read the line and extract the data you want from it. What separates the first String from the second String? The String class has methods that will help breaking a String into two parts.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. How to read a specific line and word in a txt file?
    By MjM8082 in forum New To Java
    Replies: 1
    Last Post: 09-07-2012, 07:10 AM
  2. How to read a 2d array file line by line
    By jsoeung in forum New To Java
    Replies: 35
    Last Post: 04-18-2012, 03:23 PM
  3. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  4. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  5. Replies: 7
    Last Post: 11-19-2008, 02:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •