Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting numbers into array

 
MR Chahal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a file with 6 numbers like this

1 <- first line
2
3
4
5
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

-Justin-
 
MR Chahal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. MR,

Could you pls post the whole code so that we can analyze...???
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need an import, it would be import java.util.Scanner;

CR
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 java.io.BufferedReader. And it's never a bad idea to study the IO tutorial:

http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
MR Chahal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umm...

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...



type...



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

beta of java 1.5... sorry!

-Justin-
[ May 03, 2006: Message edited by: Justin Fox ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic