Fixing Java Compiler Error: ';' Expected

  • Context: Comp Sci 
  • Thread starter Thread starter stripes
  • Start date Start date
  • Tags Tags
    Compiler Error Java
Click For Summary

Discussion Overview

The discussion revolves around a Java compiler error related to the implementation of a method intended to encode a string using the ROT13 cipher. Participants are examining the code structure, syntax issues, and the logic behind the ROT13 algorithm.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to create a method that applies the ROT13 cipher but encounters a compile error due to incorrect syntax involving the initialization of a string.
  • Another participant shares an updated version of the code but still faces a similar compile error, indicating confusion over how to properly append characters to a string.
  • A third participant points out that the syntax used for string initialization is invalid in Java and suggests using either a StringBuilder or a char array for building the output.
  • One participant claims to have resolved their issues with the ROT13 implementation and provides a new method that correctly handles character encoding based on the ROT13 rules.
  • The same participant also shares their main method, which includes file handling for reading and writing messages, indicating a more complex application of their ROT13 implementation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to implement the ROT13 cipher, as there are multiple proposed methods and ongoing confusion about syntax and logic. The discussion remains unresolved regarding the optimal solution.

Contextual Notes

There are limitations in the code provided, including unresolved syntax errors and potential misunderstandings about how the ROT13 algorithm should be implemented in Java. The discussion reflects varying levels of understanding and experience with Java programming.

Who May Find This Useful

Readers interested in Java programming, particularly those dealing with string manipulation and encoding algorithms, may find the discussion relevant.

stripes
Messages
262
Reaction score
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
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:
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.
 
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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K