1

while i am doing a simple cipher program. i was encounter this error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(Unknown Source)
at Caesar.main(Caesar.java:27)

well, i am not quite clear on what is the cause. i require some help from some veteran here @@ below are my code.

import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class Caesar {

    public static void main(String[] args){
         String from = "abcdefghijklmnopqrstuvwxyz";
         String to   = "feathrzyxwvusqponmlkjigdcb";
            Scanner console = new Scanner(System.in);
            System.out.print("Input file: ");
            String inputFileName = console.next();
            System.out.print("Output file: ");
        String outputFileName = console.next();

        try{ 
            FileReader reader = new FileReader("C:/"+inputFileName+".txt");
            Scanner in = new Scanner(reader);
            PrintWriter out = new PrintWriter("C:/"+outputFileName+".txt");

                while (in.hasNextLine()){
                    String line = in.nextLine();
                    String outPutText = "";
                    for (int i = 0; i < line.length(); i++){
                        char c = to.charAt(from.indexOf(line.charAt(i)));
                        outPutText += c;
                    }
                    System.out.println("Plaintext: " + line);
                    System.out.println("Ciphertext: " + outPutText);
                    out.println(outPutText);         
                }
                System.out.println("Processing file complete");
                out.close();
        }
        catch (IOException exception){ 
            System.out.println("Error processing file:" + exception);
        }
}
}
shuffle1990
  • 89
  • 1
  • 2
  • 7
  • "StringIndexOutOfBounds", surprisingly, means that you used an index on a String operation (in this case clearly `charAt`) that was invalid. The message even tells you what the invalid index was: -1. Breaking the complex statement up into simpler pieces would allow you to examine the intermediate results with a debugger or simple System.out.println statements and work this out for yourself. – Hot Licks Nov 22 '12 at 17:48

2 Answers2

6

This is your assignment inside your for loop: -

char c = to.charAt(from.indexOf(line.charAt(i)));

Here, in indexOf returns -1 when the char is not found in from string, and then it will throw an StringIndexOutOfBoundsException.

You can add a check before fetching the character: -

int index = from.indexOf(line.charAt(i));

if (index >= 0) {
    char c = to.charAt(index);
    outPutText += c;
}

or: -

char ch = line.charAt(i);

if (from.contains(ch)) {
    char c = to.charAt(from.indexOf(ch));
    outPutText += c;
} 
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    @shuffle1990.. You're welcome :) Glad that you learned something. That's the whole point of making mistakes. Cheers :) – Rohit Jain Nov 22 '12 at 18:05
3

indexOf() returns -1 if the character in question isn't found in the string. So, you need to build in some contingency for that happening. When the character isn't found in "from", what do you want the code to do?

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83