The Artima Developer Community
Sponsored Link

Java Answers Forum
Reading an array of strings

6 replies on 1 page. Most recent reply: Oct 3, 2006 5:12 AM by Sachin Paradkar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

Reading an array of strings Posted: Dec 21, 2005 4:48 AM
Reply to this message Reply
Advertisement
Really simple problem this - I have a text file with a list of strings in the following

a;
b;
c;
d;

Currently I read in the strings as

a; b; c;

And return the result..

How can I modify my code to read the strings as a column and not a row? Currently only reading the first row...

Thanks

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.String;
 
import java.util.*;
 
 
/**
 * @author 
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Count {
	public static void main(String Args[])
	{
		BufferedReader file = null;
		String line = null;
		String fileName = "temp.txt";
		try {
			file = new BufferedReader(new FileReader(fileName));
			line = file.readLine();
		} catch (FileNotFoundException e) {
			System.out.println("File: " + fileName + " not found.");
		} catch(IOException e) {
			System.out.println("Error reading data from file: " + fileName);
		}
		
		//ArrayList names = new ArrayList();
		HashMap names = new HashMap();
		String name[] = null;
		
		name = line.split(";");
		int count = 0;
		
		
		
		//System.out.println(name[5]);
 
		
		
		// convert array into hashmap (using
		// keys to remove duplicates)
	    for (int x = 0; x < name.length ; x++) {
	    	if(names.put(name[], "")!= null)
	    			count++;
	    }
	    
	    // print count
	    System.out.println(names.size());
	    System.out.println(count);
	}
	
}


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Reading an array of strings Posted: Dec 21, 2005 5:25 AM
Reply to this message Reply
Haven't gone through your code entirely but if it works then
the suggestion below should work instead of splitting the
line with the String ";" try splitting it with ";\n"

i.e. line.split(";\n");

Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

Re: Reading an array of strings Posted: Dec 21, 2005 5:46 AM
Reply to this message Reply
Tried that and doesn't seem to make a difference - it halts after the first element in the column insteading of iterating through it

BTW if statement should be this:
if(names.put(name[x], "") != null)

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Reading an array of strings Posted: Dec 21, 2005 6:01 AM
Reply to this message Reply
Oh Crap. You're right...

Sorry I din't even go through your code; you don't even
iterate. You read the first line of the file and that's
it. You have to iterate till you reach EOF. or till
you readLine is null. Probably store the stuff in a
StringBuffer and Tokenize it afterwards.

Sorry I can't give you precise code, its christmas and I'm
swamped myself.

Hope you get it working.

Gotta jet.

One!

Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

Re: Reading an array of strings Posted: Dec 21, 2005 6:11 AM
Reply to this message Reply
No worries, thx for the suggestion. merry xmas

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: Reading an array of strings Posted: Dec 21, 2005 8:38 AM
Reply to this message Reply
Looks like you are only reading the first line.
The line file.readLine() should actually be in a loop.

Something like:

while (line is not null) {
get String into Array
}

Finally, you can eliminate duplicates in an array by putting its contents into a Set, which, by definition, does not contain duplicates.

Sachin Paradkar

Posts: 1
Nickname: sachino
Registered: Oct, 2006

Re: Reading an array of strings Posted: Oct 3, 2006 5:12 AM
Reply to this message Reply
Hi ,
We have a method in our project . Hope it helps !!

/**
* Used to read Strings from the file and return an ArrayList of Strings for
* each line present in the file.
*
* @param filename
* Filename, which needs to be complete path
*
* @return ArrayList of Strings read from file
*
* @throws FileNotFoundException
* If file is not found
* @throws IOException
* If an IO Exception occurs
*
*/
public static ArrayList readStringLines( String fileName ) throws FileNotFoundException, IOException
{
// Declaring variables required for the method
FileReader freader = null;
BufferedReader breader = null;
String dataLine = null;
ArrayList stringsRead = null;

try
{
// Creating new input stream
URL url = ( AMLFileUtility.class ).getClassLoader().getResource( fileName );
freader = new FileReader( url.getPath() );
breader = new BufferedReader( freader );
stringsRead = new ArrayList();

while ( ( dataLine = breader.readLine() ) != null )
{
stringsRead.add( dataLine.trim() );
}

freader.close();
breader.close();
}
finally
{
// Initialise the objects to null
freader = null;
breader = null;
dataLine = null;
}

return stringsRead;
}

Flat View: This topic has 6 replies on 1 page
Topic: Can't get output to perform corretly Previous Topic   Next Topic Topic: Maven-TestNG using JDk 1.4


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us