• Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to read in a string from a text file  RSS feed

 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i am saving a file. it is an array of two strings each containing three words. i figured out how to save to a text file and read it back in and put it back into an array. i am using scanner.hasnext and scanner.next and i think that separates the strings into variables using the spaces in the strings. well for my project i need to do it with a symbol instead of a space


i put some comments in the read method so you can get exactly what im trying to accomplish, Thank you!!!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you try some simple reading. Try things like myScanner.next() and myScanner.nextLine(). Print them out like this
System.out.printf("\u201c%s\u201d%n", text);
Which means you get the text surrounded by posh quotes. That way you can see the exact length of what you have read. After you have run this sort of thing for a half‑hour or so, you will become familiar with Scanner. Probaby easier if you have the Scanner pointing to System.in, so you can enter text at runtime.
Look at this post which tells you about a potential nasty pitfall with nextLine. Also look at the Scanner documentation which has a few simple examples of how to read things in.

Also divide your code. you should have a method for writing and one for reading. In fact, every method should do one thing and one thing only.
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thank you for the provided reading... so i have got it to work but
Also divide your code. You should have a method for writing and one for reading. In fact, every method should do one thing and one thing only.


i have a save method which writes my arrays to a file, i have an open method which opens the file, and then i have a read method which iterates through it and rewrites it to an array. What here is not divided like it should be?
im reading the oracle documentation i realized i needed to change the delimiter to find things other than white spaces. however i am again a little stuck. i mean i guess i could use this but it isn't exactly the same as my teacher suggests.

this is the code i used to set the delimiter, so if it reads the line, howzzarezzyouzz, it produces, how are you... i think the \\s* \\s* means something about white space... but even with no white space in my entered string it appears i need those im curious as to why.
So what does \\s* \\s* mean?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kip Bodey wrote:ok, thank you for the provided reading... so i have got it to work but
Also divide your code. You should have a method for writing and one for reading. In fact, every method should do one thing and one thing only.


i have a save method which writes my arrays to a file, i have an open method which opens the file, and then i have a read method which iterates through it and rewrites it to an array. What here is not divided like it should be?
im reading the oracle documentation i realized i needed to change the delimiter to find things other than white spaces. however i am again a little stuck. i mean i guess i could use this but it isn't exactly the same as my teacher suggests.

this is the code i used to set the delimiter, so if it reads the line, howzzarezzyouzz, it produces, how are you... i think the \\s* \\s* means something about white space... but even with no white space in my entered string it appears i need those im curious as to why.
So what does \\s* \\s* mean?

"\\s*" is the regular expression meaning zero or more white space characters.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:"\\s*" is the regular expression meaning zero or more white space characters...

And if you are indeed using it as a delimiter, it should possibly be "\\s+" (one or more white space characters).

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I missed the separate methods.

Don't call an array list, and don't call a Scanner x. Both names can be confusing. You are liable to suffer Exceptions if you have an array of fixed size and run a loop with hasNext(); that represents close coupling between your code and the structure of the file. See what happens if you enter seven tokens from the keyboard.
Read the Scanner documentation which tells you how to change the delimiter. Beware in case you use a metacharacter for regular expressions however. You would then have to escape it. You will have to go through that tutorial to find whether & is a metacharacter.
 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell... I was just working to get things to work correctly... my entire program is already done, i just made a new little program to work on my saving and opening methods. Thank you all so much!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!