?
Solved

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

Posted on 2011-04-27
13
Medium Priority
?
285 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Sdot718
13 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35477147
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]);
....


0
 
LVL 23

Expert Comment

by:cmalakar
ID: 35477153

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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35477155
You can also use split method of the String and get tow first element of the array
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 35477182
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35477195
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35477201
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
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 35477544
>> Is there a way to have a method to pull the names and store them into the arrays using a counter type of logic?

Yes. You're close:
String[]  first = new String[10];
int ix =0;
while(inFile.hasNext()) {
	first[ix++] = inFile.next();
	first[ix++] = inFile.next();
	for(int i = 0;i<6;i++) inFile.nextDouble();
}

Open in new window

0
 

Author Comment

by:Sdot718
ID: 35477914
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.



0
 
LVL 47

Expert Comment

by:for_yan
ID: 35477930
Are you sure that you always have six numbers in each line?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35477943
>>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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35477973

If you have different numbers of numeric items then prhaps better still
to read line by line,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35477974
Just tested the code i gave you with the input you posted - works fine. If you still have trouble,please post your actual file
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35477993
:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question