0

I'm trying to reverse a sentence. (if the sentence is "i am a cat", then the result should be "cat a am I") The problem is that I'm getting an index out of bounds error, I'm not sure how to fix the problem. Not sure if my index is wrong or if the substring part is wrong. I'm just a beginner in Java, so any help is really appreciated. error: String index out of range: -1

Thanks

public class ReverseWords {

    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter File Name: ");
        String fileName = in.nextLine();
        File f = new File(fileName);
        try {
            Scanner input = new Scanner(f);
            int x = 1;
            int n = input.nextInt();
            while (x<n) {
                String line = input.nextLine();
                Queue<String> q = new LinkedList<String>();
                q.add(line);
                int ws = line.indexOf(" ");
                String word = line.substring(0,ws);
                ArrayList<String> a = new ArrayList<String>();
                while (line.length() > 0) {
                    a.add(word);
                    q.remove(word);
                }
                System.out.println("Case #" + x);
                for (int i = a.size(); i != 0; i--) {
                    System.out.print(a.get(i));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
3
  • Where exactly are you getting the error? Try to be more specific when asking questions. Furthermore, why i != 0 and not i > 0 in your for statement? Commented May 31, 2013 at 16:24
  • 1
    Seriously, you should rename your variables to ease the readability of your code for future readers (remember that you in few days/weeks/months will become in a future reader as well). Commented May 31, 2013 at 16:24
  • sorry about that...the for loop is to read backwards, right? I'm trying to print the reverse Commented May 31, 2013 at 16:31

2 Answers 2

1

From the Java documentation of String:

public int indexOf(int ch)

...

If no such character occurs in this string, then -1 is returned.

are you prepared for that there is no more whitespace?

For example at the last iteration...

But it looks like there are other bugs, too.

Sign up to request clarification or add additional context in comments.

2 Comments

ahhh...thanks a lot...so the last word of the sentence will contain no whitespace.
Btw, split() is a nice method.
0

Your problem would be with a line like the following:

int ws = line.indexOf(" ");
String word = line.substring(0,ws);

whenever indexOf doesn't find what it's looking for, it will return -1. If you ever try to use that index on the string, you'll be indexing at -1 which is out of bounds.

it looks like you're trying to find the space in a line that has no space.

4 Comments

i have the file and there is a space after the first word though
@XcutionX run your program in the debugger, and check whether or not the variable line contains a space during Each and every iteration of the loop.
@XcutionX if you don't know how to use a debugger, than you can just print the contents of line in the console immediately before you get your error. do something like System.out.print("|"+line+"|"); so you can be sure that you'll see any spaces that are there.
@XcutionX note that we both spotted the same error ... consider what happens when you arrived at the first word!

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.