This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reading and converting only one line to String and storing it in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading and converting only one line to String and storing it in an array" Watch "Reading and converting only one line to String and storing it in an array" New topic
Author

Reading and converting only one line to String and storing it in an array

Jimsson Lim
Greenhorn

Joined: Sep 05, 2011
Posts: 2
Hi,

I'm new to java and I am having some problem solving my code for quite some time. I'm trying to make a line that was read in a text file convert it to String and store it in an array and then distribute it to three linked list.

for example, one of the line that is read from the text consist of "24567hguy4565e" I need to put each individual number or letter to one of index of an array and then transfer it to two linked list.

I would like to ask also for someone to clarify to me how the System.in in the "Scanner file = new Scanner(System.in);" works. I read the description from the book, but I'm not sure how it would find my text file or multiple text file and able to read from it.




I tried other methods of storing it on a linked list or array and I usually get either an array of nulls, or [L]java.lang.String;@7c6768, or an error on the loop code.

Thank you in advance.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
I can help with one part of your question. String.toCharArray gives you an array populated by all the chars of the String. Then you can easily iterate over it converting each char to a String.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

I prefer to use String.length and String.charAt:
Using toCharArray copies the entire String contents into the resulting char[]. The above loop doesn't.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
And welcome to the Ranch
Jimsson Lim
Greenhorn

Joined: Sep 05, 2011
Posts: 2
Thank you Campell.

Ok so I did the loop for loop mention and it work. Now I got a brand new problem how will I convert all the letters that is read on the text file to numbers? For example A=1, B=2 C=3..etc.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
What makes you think you have got letters? Read the Java Language Specification and you find out that a char is an unsigned number. If it is an unsigned number, you can do arithmetic on it.
Have a look at the Unicode charts, and inspect the numbers which correspond to A..Z and a..z. You can use one of the arithmetic operators to get A=1, a=1, B=2, b=2, etc. But, if you look at this Unicode page, you find that sort of arithmetic will produce completely different results for é or ç etc.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

I have never used scanner so just read the first line about scanner and it says it can read anything which implements readable, well file doesn't implement this.

I always use a BufferedReader, just do a google search on "java reading from a file" and you will find pages and pages of answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
You can get a Scanner to read a File, Wendy, as long as it is a text file.A good place to start reading about input from files is the Java Tutorials. There will be more in that link than you need at present. You will find an example with BufferedReaders in, I think called buffered input or similar. You need to close the Reader in a finally block (or use the new try-with-resources which only works on Java7).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
. . . and how far have you got with the A=1, a=1 problem and arithmetic on chars?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading and converting only one line to String and storing it in an array
 
Similar Threads
Help with creating a game.
Help with my game.
moving a linked list into an array
Write a program that reads and writes from binary or text files
reading values from txt file via loop and storing them into class instances