-1

I'm trying to code a deciphering program. I looked all over but I cannot figure out what is giving me the error. The code is supposed to take user Input, take a number (labeled key), then shift all characters in the string by that number. Any help would be greatly appreciated.

Here is my code:

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

class Assignment3 {

    public static void decipher(int key, String scanInput)
    {
         
          for (int x = 0; x < scanInput.length(); x++) {
            int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
            deciphered = deciphered + abc.charAt(letterDecode); 
            
            }    
            System.out.println(deciphered);
    
    } 
    
    public static void main(String[] args)
    {
    
      Scanner scan = new scanner(System.in);
      String abc = "abcdefghijklmnopqrstuvdxyzABCDEFGHIJKLMNOPQRSTUPWXYZ";
      String deciphered = "";
      
      
      System.out.println("Please enter your code below");
      String scanInput = scan.nextLine();
      
      System.out.println("Please enter your key (Preferrable from 1-26");
      int key = scan.nextInt();
      
      decipher();        
           
    }
}

These are the errors:

     Assignment3.java:21: error: cannot find symbol
                int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
                                   ^
      symbol:   variable abc
      location: class Assignment3
    Assignment3.java:22: error: cannot find symbol
                deciphered = deciphered + abc.charAt(letterDecode); 
                ^
      symbol:   variable deciphered
      location: class Assignment3
    Assignment3.java:22: error: cannot find symbol
                deciphered = deciphered + abc.charAt(letterDecode); 
                             ^
      symbol:   variable deciphered
      location: class Assignment3
    Assignment3.java:22: error: cannot find symbol
                deciphered = deciphered + abc.charAt(letterDecode); 
                                          ^
      symbol:   variable abc
      location: class Assignment3
    Assignment3.java:25: error: cannot find symbol
                System.out.println(deciphered);
                                   ^
      symbol:   variable deciphered
      location: class Assignment3
    Assignment3.java:32: error: cannot find symbol
          Scanner scan = new scanner(System.in);
                             ^
      symbol:   class scanner
      location: class Assignment3
    Assignment3.java:43: error: method decipher in class Assignment3 cannot be applied to given types;
          decipher();
          ^
      required: int,String
      found: no arguments
      reason: actual and formal argument lists differ in length
    7 errors
  • 1
    It looks like the code is trying to reference variables that are not within the scope of the method `decipher` – John Mercier Nov 11 '20 at 20:26
  • 1
    This does not even compile, I doubt that you ever got an `IndexOutOfBoundsException`. – f1sh Nov 11 '20 at 20:26
  • 1
    java is case-sensitive. `new scanner(...)` does not work if you want to use `Scanner`. – f1sh Nov 11 '20 at 20:27
  • move the whole body of the `decipher` method to the bottom of your `main` method and you're done. – f1sh Nov 11 '20 at 20:29

1 Answers1

0

First of all you need to declare abc and deciphered variables in decipher method. Moreover do not forget to pass key and scanInput variables in your method(last line in the main method). Please see the following example:

public static void decipher(int key, String scanInput)
{
    String abc = "abcdefghijklmnopqrstuvdxyzABCDEFGHIJKLMNOPQRSTUPWXYZ";
    String deciphered = "";
    for (int x = 0; x < scanInput.length(); x++) {
        int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
        deciphered = deciphered + abc.charAt(letterDecode);

    }
    System.out.println(deciphered);

}

public static void main(String[] args)
{

    Scanner scan = new Scanner(System.in);
    
    System.out.println("Please enter your code below");
    String scanInput = scan.nextLine();

    System.out.println("Please enter your key (Preferrable from 1-26");
    int key = scan.nextInt();

    decipher(key, scanInput);
}
Andrian Soluk
  • 474
  • 6
  • 12