• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading text from a file  RSS feed

 
Brett Banich
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking a java class and have a project that I've been working on. I've just come up to the next part of my project and I am stuck. Can I use two different scanners? I am using eclipse to and don't even know how to add the text file. Thanks for the help in advance. These are the instructions I am given:
-read the text to be ciphered from a file.
-prompt the user for the file name but allow the use of a default file that is hard coded into the program
         -e.g. "Enter name of file to cipher (just hit ENTER to accept default file): "
-verily that the file exists and re-prompt the user until they enter a valid file or accept the default file.
-after printing each ciphered line to the screen, print the ciphered line to an output file defined with the input file name + " - ciphered" appended to it.
-the input file should have the key that was previously prompted for as the first line (or record), and, any line starting with a "#" should allow the key to be changed



 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Eclipse, where is the package declaration? You would want the text file (if I remember correctly, and I maybe don't) in the folder with the same name as your project. And Eclipse usually expects a package name. So the text file and the source code are in different locations.
Is there a name for that encoding? Is it a Caesar cypher?
Don't write ....equals(""). Write ....isEmpty().
What does it mean that one loop is while the message equals # and the other loops is while the message isn't equal to #? Your poor code indentation, which Eclipse can correct with ctrl‑A‑ctrl‑I, makes the code very difficult to read.
What range do you expect for these random numbers (n)?Try letter -= 26; rather than casting.
It would be better to use a StringBuilder to append letters because += used on Strings can give slow performance. You probably won't notice until you are into many thousands of letters, however.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett Banich wrote:Can I use two different scanners? I am using eclipse to and don't even know how to add the text file.

You can have multiple Scanners open at given time but you should have only one Scanner at a time open for a given resource. That is, if you are using a Scanner to read a file, don't have that Scanner open and then open another one for the same file until you close the first one.

In Eclipse the text file should be at the project level. Example:
C:\working-folder\myproject\somefile.txt

Eclipse does not force you to create a package but it's a good idea.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 10, int key = console.nextInt(); you never use the variable key.

Campbell wrote:Your poor code indentation, which Eclipse can correct with ctrl‑A‑ctrl‑I, makes the code very difficult to read.

Also Ctrl-Alt-F works, formatting and indenting.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In line 10, int key = console.nextInt(); you never use the variable key.

Campbell wrote:Your poor code indentation, which Eclipse can correct with ctrl‑A‑ctrl‑I, makes the code very difficult to read.

Also Ctrl-Alt-F works, formatting and indenting.

With Windows and Eclipse it is Ctrl-Shift-F.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Knute Snortum wrote:Also Ctrl-Alt-F works, formatting and indenting.

With Windows and Eclipse it is Ctrl-Shift-F.

You are right; sorry for the confusion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!