0

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);
      }
     }
    }
Alex Mallo
  • 54
  • 2
  • 2
  • 6

4 Answers4

3

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+\

Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
2

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

Pyranja
  • 3,529
  • 22
  • 24
  • Ok thanks, it is saying that outFile cannot be resolved. I defined outFile in my main function so all my methods can access it. Do you know what the problem may be? – Alex Mallo Aug 07 '12 at 16:32
  • if you declare a variable (like outFile) in java inside a method its scope is limited to the method. Inside another method you cannot access these local variables - you have to pass them as a parameter or declare them in another, broader scope e.g. as static class member if you wan't to use them in static methods. – Pyranja Aug 07 '12 at 16:35
  • How would you suggest that I declare my variable outFile. I want to print it out in the end with the newly shifted text – Alex Mallo Aug 07 '12 at 16:46
0

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

La bla bla
  • 8,558
  • 13
  • 60
  • 109
0

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.

nook
  • 2,378
  • 5
  • 34
  • 54