• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jimsson Lim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and how far have you got with the A=1, a=1 problem and arithmetic on chars?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic