This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Putting numbers into array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Putting numbers into array" Watch "Putting numbers into array" New topic

Putting numbers into array

MR Chahal

Joined: Nov 27, 2005
Posts: 14

I have a file with 6 numbers like this

1 <- first line
6 <- last line

The file is opened into a jTextArea in the above manner. But how can i insert those numbers into an int array?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
well i think all you have to do is put the file in the same folder..

and do this in the main program

then do the :

something along those lines I believe...

someone plz correct me if i'm wrong...


You down with OOP? Yeah you know me!
MR Chahal

Joined: Nov 27, 2005
Posts: 14
Hello Justin

I get this error:

symbol : class Scanner
location: class fio.Tframe
Scanner scan = new Scanner(f);

I know i have to import something but not sure what. How do i solve this?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Mr. MR,

Could you pls post the whole code so that we can analyze...???
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
If you need an import, it would be import java.util.Scanner;

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Be aware that java.util.Scanner is only since 1.5. If you are using this version of Java, or later, consider nextInt rather that just readLine, as well. If you are using an earlier version of Java, check out And it's never a bad idea to study the IO tutorial:

There is no emoticon for what I am feeling!
MR Chahal

Joined: Nov 27, 2005
Posts: 14
I have 1.4.2 hency why the scanner wont work. I can load and save text files with no problem. What i am doing now is take the text in the jTextArea and puting it in a String. But i dont know how to put those 6 numbers from the string into an array. Any help will be appreciated.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802

i think versions before 1.5 have scanner. but to create the object

requires different syntax... like

Scanner scan = Scanner.create(file);

and in the while loop i made a boo boo, don't let info += scan.nextLine,

only info = scan.nextLine();

and you could declare info to be an int, and say.

info = scan.nextInt();

wasn't thinking i guess.

and you have to import the scanner package.

so before the...


I lied!! the Scanner scan = Scanner.create(); only applies to the first

beta of java 1.5... sorry!

[ May 03, 2006: Message edited by: Justin Fox ]
I agree. Here's the link:
subject: Putting numbers into array
It's not a secret anymore!