• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read each number from a file  RSS feed

 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have in a file some random numbers, let's say 61254 and I want to read each number as an int how can I do that?



Right now, this program outputs:
Index 0: 31416
Index 1: 0
Index 2: 0
Index 3: 0
Index 4: 0
Index 5: 0
Index 6: 0
Index 7: 0
Index 8: 0
Index 9: 0

But I want to have:
Index 0: 3
Index 1: 1
Index 2: 4
Index 3: 1
Index 4: 6
Index 5: 0
Index 6: 0
Index 7: 0
Index 8: 0
Index 9: 0

I have done a few homework programs involving reading from a file but I am not too comfortable with it yet. I know that if the numbers have space in the text file 6 1 2 5 4 then it will work but I don't want that. This is not homework BTW 
Don't worry about the fact that my class is named Main or that I am working in Main instead of using a different class. I like to create programs in my spare time and since I ran into this issue, I created a generic class Main just for the purpose of my question.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to read in each digit separately, you can call the scanner's useDelimiter("").  An empty string as the delimiter will make the scanner treat each character in the input as a separate token so when you call nextInt() it will only read in one digit.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!