Alex Mallo
Alex Mallo

Reputation: 54

Syntax error on tokens, ; expected?

I commented all my errors in my program. The point of my program is to shift my input file by whatever the user inputs. My errors that I commented out are kind of pointless to me because it all make sense and the computer is not reading it the way I want it to. Not to be confused with one of the errors that I commented one of the tokens is ",". The others are "(", and ")". These all are errors that expect a semi colon somewhere in the line where I commented them.

Here is what the program looks like:

import java.util.*;
import java.io.*;

class CaesarCipher
{
 public static void main (String [] args) throws FileNotFoundException
 {
  Scanner keyboard = new Scanner(System.in);
  System.out.println("What shift should I use? ");
  int shift = keyboard.nextInt();
  System.out.println("What is the name of the input file? ");
  String name = keyboard.next();
  File f = new File(name);
  Scanner inFile = new Scanner(f);
  System.out.println("What is the name of the output file? ");
  String text = keyboard.nextLine();
  PrintWriter outFile = new PrintWriter(text);
  String encrypted = "";

  while (inFile.hasNextLine())
  {
     String line = inFile.nextLine();
      if ( shift == 1)
     encrypted = caesarEncipher(line, shift);
      else if (shift == 2)
     encrypted = caesarDecipher(line, shift);// the method caesarDecipher(java.lang.String, int) is undefined for the type CaesarCipher
      System.out.println(encrypted);
      outFile.println(encrypted);
  }
 }
  static String caesarEncipher(String text ,int shift) throws FileNotFoundException
  {
   String t = "";
   int i = 0;
   while (i < t.length())
   {  
    if (shift < 0)
    {
        shift = (shift % 26) + 26;
     }
        int move = (char) ((text.charAt(0) - 'A' + shift) % 26 + 'A');
        t += move;
        i++;
        System.out.println(t);
        outFile.println(t);
        return "DONE!";
     }
                                                          // for each token listed, it expects the semi colon.
     static String caesarDecipher(String text, int shift) throws FileNotFoundException // Syntax error on token "(", "," , ")", ; expected      
     {
      return caesarEncipher(input, -shift);
      }
     }
    }

Upvotes: 0

Views: 24445

Answers (4)

Richard Sitze
Richard Sitze

Reputation: 8463

Your caesarDecipher method definition is embedded in the method caesarEncipher. That's not legal Java, and you've confused the poor compiler.

Strict indenting makes these kinds of things very clear. If you're using an IDE, or emacs, look for tooling to re-indent your entire file (there are also command line tools under Unix):

For Eclipse: Ctrl+Shift+F

For Emacs: Highlight region (entire file), then: Esc Ctrl+\ OR Alt+Ctrl+\

Upvotes: 3

nook
nook

Reputation: 2396

As said, you are missing a closing brace. I would recommend using a IDE to spot these mistakes. Many people prefer eclipse but I personally like Intellij. Eclipse is free, and the full version of intellij is not.

Upvotes: 0

La bla bla
La bla bla

Reputation: 8708

You're missing } in the end of your

static String caesarEncipher(String text ,int shift) throws FileNotFoundException

and you have an extra one in the end of

static String caesarDecipher(String text, int shift) throws FileNotFoundException

also note, when you are returning in this ^ method, you're using the variable input which is undefined in this method. maybe you ment text which is your argument

Upvotes: 0

Pyranja
Pyranja

Reputation: 3589

you missed the closing } on your method caesarEncipher. Use intendation to spot these errors faster.

Upvotes: 2

Related Questions