0

I am working on a project for my Java class, and cannot seem to figure out how to fix this problem. Based on the exception, I understand that it lies within the string index length, however, I cannot seem to correct the problem. This is my first foray into learning Java, so please forgive me for the novice question.

Some background on the task at hand: I am attempting to import an ascii art file (.txt), convert it to csv format, and output to specified file. The method signature was provided by the instructor, and cannot be manipulated. This code compiles, but when run, an out of bounds exception citing my call to imageToNumRep is thrown.

Here is my code for this particular section:

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

    public class Convert{
        public static void main(String[] args) throws FileNotFoundException {
            File input=new File("Flag.txt");
            File output=new File("result.txt");
            imageToNumRep(input, output);
        }

    public static void imageToNumRep(File input, File output) throws FileNotFoundException {
        Scanner in=new Scanner(input);
        PrintStream out= new PrintStream(output);
        int count=0; 
        while(in.hasNextLine()) {
            count++;
            String s=in.nextLine();
            out.print("(");
            for(int x=0; x < s.length()-1; x++) {
                int num=1;
                while(s.charAt(x)==s.charAt(x+1)) {
                    num++;
                }
                out.print(num+s.charAt(x-1));
                num=1;
                if(s.charAt(x) != s.charAt(x-1) && s.charAt(x) != s.charAt(x+1)) {
                    out.print("1,"+s.charAt(x));
                } else {
                    num=1;
                }
            }
            out.print(")");
            out.println();
        }
    }
} 
2
  • We are going to need to see the stacktrace and which line the exception in thrown on. Commented Jun 10, 2014 at 20:42
  • Yes, here it is. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:658) at Convert.imageToNumRep(Convert.java:24) at Convert.main(Convert.java:8) Commented Jun 10, 2014 at 21:00

2 Answers 2

2

Your are looping from index 0 to s.length()-2 (s.length()-1 won't be included because of the < operator), this will avoid getting an exception if you call s.charAt(x+1), but when you do

s.charAt(x-1)

and x = 0, then you will get an exception because it will be like s.charAt(-1), which is not valid.

What can you do? You can iterate starting from 1:

for(int x = 1; x < s.length() - 1; x++)
Sign up to request clarification or add additional context in comments.

5 Comments

where is the -2 at? I'm not seeing that line.
The condition is x < s.length()-1, so s.length()-2 would be the last value of x.
The loop is for(int x = 0; x < s.length() - 1; x++), so x will go from 0 to s.length() - 2, because of the <, it will exclude s.length() - 1
oh ok. Sorry, I was looking for the coded logic not the iteration. Thanks for pointing out and good catch.
Thank you very much, that makes so much sense now.
1

In addition to the previous answer, and at first glance,

while(s.charAt(x)==s.charAt(x+1)) {
num++;
}

should never terminate, since you are not changing the value of x. You might be lucky in the case that the first two consecutive chars are not equal however.

4 Comments

Would it be better to code the possible cases as a series of if/else statements? I thought it out that way in my head, but thought the while statement would work too.
The while statement will continue to loop it's body while the condition holds, in this case since the condition is not affected by the body, the condition will either always hold, or always not hold, depending on the value of x and s at the point you reach it. Without better understanding what you're trying to achieve by this block, I think you might have been looking for just an if statement?
Possibly, let me attempt to better explain the purpose. My thinking behind the while loop was simply to count the number of consecutive(if any) characters in each line of my ascii art file. If the char value at the current scanner position did not match the preceding char, the loop would be bypassed, and my counter would remain at 1.
'count the number of consecutive(if any) characters in each line of my ascii art file' Therefore, I think what you need to be doing is, change the while to an if, and initialise num = 1 outside the for loop, since the outer while loop is for each 'line', and otherwise you will loose your count in each char of the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.