Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Input text file to scanner to jcombobox  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stuck on trying to get the code to move from a txt file to an array using scanner. The output doesn't print the correct lines. The txt file has 6 lines (Selection Number One, etc.. up to Selection Number Six) and each line needs to go into the array. The array then needs to print each individual line. I can work it as an arraylist but need an array instead. How do I get rid of the null ouputs? It works with an arraylist --but I need an array. Any help on this is appreciated. Please don't respond with complex coding -I'm just learning java.  I want to use "scanner" not BufferRead.


The output comes out wrong:

Selection Number One
null
null
null
null
null
Selection Number Two
null
null
null
null
null
Selection Number Three
null
null
null
null
null
Selection Number four
null
null
null
null
null
Selection Number Five
null
null
null
null
null
Selection Number Six
null
null
null
null
null
BUILD SUCCESSFUL (total time: 3 seconds)








 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not doing what you think it's doing.
i=i++
Either use
i = i + 1
i += 1

or best
i++
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are testing with hasNextLine(), but in the loop you're not calling nextLine().
You also need to close the Scanner when you're done with it.
 
Bill Melendez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I made the corrections but the output is the same with the same problem. I am thinking that my looping is at fault --just don't know where--since it loops once for the first line and ignores the other lines from the txt file on the first iteration. The second gets the first and second line but then ignores the other lines, and so on..

anything to help is appreciated. With the corrections I get:

Selection Number One
null
null
null
null
null
Selection Number One
Selection Number Two
null
null
null
null
Selection Number One
Selection Number Two
Selection Number Three
null
null
null
Selection Number One
Selection Number Two
Selection Number Three
Selection Number four
null
null
Selection Number One
Selection Number Two
Selection Number Three
Selection Number four
Selection Number Five
null
Selection Number One
Selection Number Two
Selection Number Three
Selection Number four
Selection Number Five
Selection Number Six
BUILD SUCCESSFUL (total time: 3 seconds)
 
Bill Melendez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:

Selection Number One
Selection Number Two
Selection Number Three
Selection Number Four
Selection Number Five
Selection Number Six
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing in your loop is:
Read [0]
   Print [0], Print[1], Print[2], Print[3], Print[4], Print[5]
Only [0] has been read so far so the others will be null.
Next time you
Read [1]
Now when you print, [0] contains your first read, [1] contains your second read, all the others are still null.

Why not just read "a String" and then print "a String"? No need for arrays.
 
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . . close the Scanner when you're done with it.

The best way to close the Scanner is with try‑with‑resources.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . You are testing with hasNextLine(), but in the loop you're not calling nextLine(). . . . .

. . . which means your line count will be incorrect. Have you considered using this if yoiu are reading line by line and counting lines.
 
Bill Melendez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do appreciate your insight into this. I did make the corrections in the last post. I decided to try a different approach using BufferedReader. Still am getting the same problem as the last posted comment from me. I seem to miss the looping sequence somewhere. The txt file only has 6 lines. I can use an arraylist and get the results I need -however, this is just a demo so I can figure out how to do the larger text file with larger amounts of lines. I chose an array because I can specify which jcombobox the lines can go to. I have 20 jcombobox in my entry form and each has a certain amount of selections. The goal was to be able to update them from a text file whenever the selections need changing.


 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem isn't related to whether you use a Scanner or a BufferedReader to get your data. Carey Brown already suggested how you could fix your problem, I suggest you look at his suggestion.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!