Link to home
Start Free TrialLog in
Avatar of Sdot718
Sdot718

asked on

How do I get Scanner class to only read and store strings

I am using scanner to read data from a text file for the purpose of ONLY storing the names to string values of an array. My issue is when I print these string arrays they print the double values of the text file also.  What is sufficient enough function to have scanner only pick out the first and last names and store them under a array.

I am aware that the method I am using  will generate alot of lines of code. Is there a way to have a method to pull the names and store them into the arrays using a counter type of logic?

The data file is formatted in such manner.

John Doe  3.2 4.2 4.2 5.5 3.9 8.2
Mary Jane 3.2 4.2 4.2 5.5 3.9 8.2
Adam Smith 3.2 4.2 4.2 5.5 3.9 8.2






String[]  first = new String[10];
		

		first[0] = inFile.next();
		first[1] = inFile.next();
		first[2] = inFile.next();
		first[3] = inFile.next();
		first[4] = inFile.next();
		first[5] = inFile.next();
		first[6] = inFile.next();
		first[7] = inFile.next();
		first[8] = inFile.next();
		first[9] = inFile.next();

inFile.close();

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

You can use StringTokenizer:

first[0] = inFile.next();
StringTokenizer t = new StringTokenizer(first[0]);
String firstName = t.nextToken();
String lastName = t.nextToken();
first[1] = inFile.next();
t = new StringTokenizer(first[1]);
....



Scanner s = new Scanner (file)

while(s.hasNextLine()){
String nextLine = s.nextLine();
String words[] = nextLine.split(" ");
String firstName = words[0];
String secondName = words[1];
}

Open in new window

You can also use split method of the String and get tow first element of the array
String [] firstNames = new String[100];
String [] lastNames = new String[100]; 
Scanner s = new Scanner (file)
int count =0;
while(s.hasNextLine()){
String nextLine = s.nextLine();
String words[] = nextLine.split(" ");
firstNames[count] = words[0];
secondNames[count]  = words[1];
count++;
}

Open in new window

ArrayList  firstNames = new ArrayList();
ArrayList lastNames = new ArrayList(); 
Scanner s = new Scanner (file)

while(s.hasNextLine()){
String nextLine = s.nextLine();
String words[] = nextLine.split(" ");
firstNames.add( words[0]);
secondNames.add (words[1]);

}

Open in new window

ArrayLists are in most cases much better than the arrays and you don't need
to worry about declaring the number of elements in the beginning
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sdot718
Sdot718

ASKER

CEHJ,

Using your example I get a InputMismatch Exception, how do I get around this to make this work.. Otherwise this logic is sound for the progression of my program.



Are you sure that you always have six numbers in each line?
>>Using your example I get a InputMismatch Exception

You'd need the exact format you posted for it to work - 2 strings followed by 6 doubles on every line

If you have different numbers of numeric items then prhaps better still
to read line by line,
Just tested the code i gave you with the input you posted - works fine. If you still have trouble,please post your actual file
:)