Fixing Java Compiler Error: ';' Expected

In summary, the programmer tried to solve the problem of encrypting a message by using a rot13 algorithm, but ran into errors.
  • #1
stripes
266
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!
 
Physics news on Phys.org
  • #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
stripes said:
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!
 
  • #5




The issue here is that you are trying to use the "+=" operator with a string variable, which is not allowed in Java. The correct syntax for concatenating a string with another value is to use the "+" operator, as you did in your commented out line. So your code should look like this:

public static String hideMessage(String message) {
String newMessage = ""; //initialize the string variable outside of the for loop
for (int i = 0; i <= message.length() - 1; i++) {
newMessage += message.charAt(i) + 13; //use the "+" operator to concatenate the string with the value
}
return newMessage;
}

Also, make sure to initialize the string variable "newMessage" before trying to concatenate values to it. This should fix the compiler error and your method should work as intended. Good luck!
 

1. What does the error "';' expected" mean?

The error "';' expected" means that the compiler encountered a code statement that is missing a semicolon (;). In Java, semicolons are used to mark the end of a statement, so the compiler expects to find one at the end of every line of code.

2. Why am I getting this error?

There are a few reasons why you may be getting this error. It could be due to a simple typo or missing character in your code, such as forgetting to add a semicolon at the end of a statement. It could also be caused by incorrect syntax, such as using parentheses instead of curly braces or using the wrong type of parentheses. Additionally, this error can occur if there is a problem with a previous line of code that causes the compiler to incorrectly interpret the current line.

3. How can I fix this error?

To fix this error, you will need to carefully review your code and check for any missing semicolons or other syntax errors. Look for any typos or incorrect use of parentheses, parentheses, or curly braces. If you are unable to find the error, try commenting out sections of code to identify which line is causing the issue. You may also want to consult with other programmers or use online resources for troubleshooting tips.

4. Can this error be prevented?

Yes, this error can be prevented by following good coding practices. Make sure to add a semicolon at the end of every statement, check for typos and syntax errors, and use proper indentation to make your code more readable. It is also helpful to use a code editor that highlights syntax errors and provides suggestions for fixing them.

5. Are there any other common errors related to this one?

Yes, there are a few other common errors that are related to the "';' expected" error. These include "missing method body, or declare abstract" and "unclosed string literal". These errors usually occur when there is a missing semicolon or incorrect syntax in the code. It is important to carefully review your code and fix any errors to avoid these related errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
856
  • Engineering and Comp Sci Homework Help
Replies
1
Views
949
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top