Java compiler error

  • #1
265
0

Homework Statement



So I'm trying to create a method in java. The method is supposed to take a string and use the rot13 method to hide the message. That is, it takes each letter and places it forward 13 letters, so a becomes n, b becomes o, etc.

Now I'm pretty sure this method is at least on the right track, but what has been driving me crazy is I keep getting a compile error:

program.java:16: ';' expected
String newMessage += message.charAt(i) + 13;
^
1 error

How the heck am I supposed to put a damn semi colon in the middle of a string name? I hate it when I get an error that just ain't so!

Homework Equations



here is my code:

import java.util.*;

public class MessageEncryption
{
public static void main(String[] args)
{
//code goes here
}

public static String hideMessage(String message)
{
for (int i = 0; i <= message.length() - 1; i++)
{
//String letter = message.charAt(i) + 13; < maybe this is redundant.
String newMessage += message.charAt(i) + 13;
return newMessage;
}
return;
}
}

The Attempt at a Solution



Just been fiddling with the code. Thank you in advance!
 

Answers and Replies

  • #2
I've since found a better code to do the job, but I still get a "; expected" error that are making me want to throw my computer across the room:
public static String hideMessage(String message)
{
String newMessage = "";
for (int i = 0; i < message.length(); i++)
{
char[] letter = new char[message.length() - 1];
letter = message.charAt(i) + 13;
String newMessage += letter;
return newMessage;
}
return newMessage;
}
^
MessageEncryption.java:56: ';' expected
String newMessage += letter;
^
1 errors
>Exit code: 1
 
Last edited:
  • #3
String newMessage += letter;


This is not valid Java code.

In your loop you seem to mix two approaches to store the output. You should either use a String (or, better yet, a StringBuilder) where you append each output character as you make them, or you can preallocate a char array and use array index to set the output character. You also need to figure out how to make a proper ROT13; adding 13 to a character is not only not possible in Java it is also not enough for a ROT13 algorithm. You may want to think about how you would explain how ROT13 works to a friend and then see if you can turn that into Java code.
 
  • #4
Yeah, I figured out how to do everything. Yes I didn't have the ROT13 method correct, but I figured it out:

//Begin hideLetter method
public static char hideLetter(String message)
{
for (int i = 0; i < message.length(); i++)
{
char c = message.charAt(i);
if (c >= 'a' && c <= 'm') c += 13;
else if (c >= 'n' && c <= 'z') c -= 13;
else if (c >= 'A' && c <= 'M') c += 13;
else if (c >= 'A' && c <= 'Z') c -= 13;
else
c = message.charAt(i);
return c;
}
//This return value will never be used, but is here because the control doesn't know that.
return 'a';
}
}

That seemed to do the job different letters, and:

public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
//Asks user what to do
System.out.println("This program opens a .txt file with a message, converts it to a hidden message, and then saves the hidden message as a new .txt file.");
System.out.print("Enter the directory and filename of the message you wish you hide, without '.txt': ");
String filename = in.nextLine();
//Creates a file instance
java.io.File file = new java.io.File(filename + ".txt");
//Error if the file doesn't exist
if (file.exists() == false)
{
System.out.println("The file specified does not exist.");
System.exit(0);
}

//Creates a file
System.out.print("Enter the directory and filename that you want your hidden message stored in, without '.txt': ");
String newFileName = in.nextLine();
java.io.PrintWriter newFile = new java.io.PrintWriter(newFileName + ".txt");
java.io.PrintWriter output = new java.io.PrintWriter(newFile);

Scanner input = new Scanner(file);
//Sets message to the empty string
String message = "";
//Program uses a delimiter that would most likely not be in the message that is to be converted.
input.useDelimiter(" ");

//Reads data from the file
while (input.hasNext())
{
message = input.next();
}
input.close();

//Emtpy console messages are for readability
System.out.println("Here is your input file's text: ");
System.out.println();
System.out.println(message);
System.out.println();
System.out.println("Here is your hidden message:");
System.out.println();
//Calls the hideLetter method within the for loop. Loop will continuously output the hidden letters from the file.
for (int ind = 0; ind < message.length(); ind ++)
{
char newLetter = hideLetter(String.valueOf(message.charAt(ind)));
System.out.print(newLetter);
output.print(newLetter);
}
System.out.println();
output.close();
}

Should handle the input/output. Perhaps there was a more efficient way but the program was due by midnight yesterday and I was scrambling to get it done, since I left it to the last minute. Thank you for your reply though!
 

Suggested for: Java compiler error

Comp Sci Truncation Error
Replies
6
Views
699
Replies
2
Views
697
Replies
7
Views
943
Replies
12
Views
2K
Replies
17
Views
770
Replies
3
Views
650
Replies
7
Views
933
Replies
5
Views
1K
Replies
2
Views
409
Replies
12
Views
903
Back
Top