• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

modifying a text file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone!

I'm a total beginner in programming, and I ran into a problem. My english is not that great but I'll try to explain my question properly.

I have an assignment where I have to write a program that modifies the string in a text file (this is the input file), by creating a new file (the output file).

The output file is almost the same as the input file, except it should sort of encrypt the original text by changing every letter of the input file to the following letter in the alphabet.

An example:

I have a text file which contains the string " abc". I have to write a program that creates another text file that would contain the string: "bcd".

I have a very shallow knowledge of programming, but I'm guessing the way I should do this is with a for loop that changes every charachter of the input file to the next character? Although I don't really know how to go about it.

How do I go about this?

Thanks for your help!

 
Saloon Keeper
Posts: 6843
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

For learning Java, Oracle has an extensive tutorial at https://docs.oracle.com/javase/tutorial/. For the particular problem at hand, the "Basic I/O" chapter seems relevant, especially its subsection on "Reading, Writing, and Creating Files".
 
greg mic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tim!

Thanks for the link, I'll look into that!

 
Sheriff
Posts: 16068
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you've described is a rotating cipher where each input character is shifted N places. In your example, it's ROT-1 because the shift is 1 place. Another typical one is ROT-13 where you shift each letter halfway around the (English) alphabet.

Individual characters of a string can be represented by the char type which is an integer type. That means you can perform arithmetic operations on it like + and %.

Try the following in jshell, the Java REPL:

jshell> char ch = 'e'
... some output

jshell> ch + 1
... some output

jshell> ch - 1
... some output

jshell> ch - 'a'
... some output

That last expression is how you calculate the "distance" between one letter from another which in that case is the distance between the letters 'a' and 'e'. You'll need this for your program.

Your biggest challenge will be when your input is 'z' or 'Z' which I assume you'll need to wrap back to 'a' or 'A', respectively. All the above hints will be useful for you.
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What you've described is a rotating cipher  . . .

Is that the same as a Caesar cipher?
 
Junilu Lacar
Sheriff
Posts: 16068
267
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following the link will give you the answer.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found two links giving Caesar's cipher wrongly, because they thought the Romans used a 26‑letter alphabet.
 
greg mic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey again!

This is my attempt at creating a code that changes a character of a string to the next character in the alphabet, but it doesn't seem to be working properly



I have "hello" written in the input text file, so I should get an output file that contains the string "ifmmp", but I get something jibberish and I don't know what goes wrong.

Can you help me with this code?

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for using code tags, but you didn't quite get them right. The code goes after [code=java] and before [/code] but this time I have corrected it for you.
Don't use \\ in a file path, despite what you see in some books. Let the runtime correct the file separator for you.
I would use the Files method and a Path to create my buffered reader.
Don't call close(), but use the construct shown here in the Java™ Tutorials.I think I might use a buffered writer, or a Formatter, instead of a print writer: see this method. I would also use a different logic for the loop:-Line 7 reads the line and assigns it to the String variable, which happens before the null test because the = is wrapped in (...). It is strange syntax which you would never guess in a million years, but it does work. You don't need to repat readLine() as you did in line 23.
I don't understand your loop in lines 20‑22, but it isn't using the text read from the input and you are incrementing small thrice per iteration. Once on each of the three lines. Both those features look like mistakes to me.
Remember you cannot alter the contents of a String, but you can create a StringBuilder, which can be altered.
 
Marshal
Posts: 26387
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately "char" is a numeric type in Java. You may represent a char value as 'a' but it's actually represented as 97. (If you think that's a strange idea you won't get an argument from me.) And then when you write it to a PrintWriter, like you did, you get 97 and not "a" written out there.

Also unfortunately you didn't show us what the jibberish actually looked like, which would have been helpful, but I'm fairly sure that's your problem. You need to convert the char to a String before writing it out. There's a few ways to do that but the least hacky is like this:

 
Paul Clapham
Marshal
Posts: 26387
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

greg mic wrote:I have "hello" written in the input text file, so I should get an output file that contains the string "ifmmp"...



I don't see why. Your code totally ignores the contents of the input text file. But if you want informed answers, show us the output.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

greg mic wrote:. . . I have "hello" written in the input text file, so I should get an output file that contains the string "ifmmp"... . . .

And have you considered what you would do if the word contains a “z”?
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic