[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
help! how to use Scanner to put items into an array  XML
Forum Index » New to Java
Author Message
Heather189

Newbie

Joined: 09/04/2009 18:55:50
Messages: 1
Offline

So, the assignment is to read from an input file, and store each word into an array. Please look at my code and tell me what you think is wrong. btw I'm totally unfamiliar with the Scanner class. I keep getting an error message:

if(inputFromFile != null){
while (inputFromFile.hasNextLine()) {
String line = inputFromFile.next();

for (int i = 0; i < array.length; i++)
array[i] = line;
}
}

for (int i = 0; i < array.length; i++)
System.out.println (array[i]);

It's just not printing what I want it to print. Help me!
stdunbar

Newbie
[Avatar]

Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline

I'm not sure that I'm following your logic, specifically the first "for" loop. You'll want to keep track of where you are in the array but your first loop just sets the same variable to each array element. Additionally, you'll want to use "hasNext" since you're parsing tokens, not lines. Something more like:



Thanks for using the forums at hotjoe.com
[WWW] [Yahoo!] [ICQ]
 
Forum Index » New to Java
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle