java - I keep getting out of bounds error how do i fix this? -



java - I keep getting out of bounds error how do i fix this? -

scanner scan = new scanner(system.in); system.out.println("enter word"); string line = scan.nextline(); string pig = ""; int = line.length(); int x = 0; int y = 0; while (y < a) { int b = line.indexof(" "); if (line.substring(x, x + 1).equals("a") || line.substring(x, x + 1).equals("e") || line.substring(x, x + 1).equals("i") || line.substring(x, x + 1).equals("o") || line.substring(x, x + 1).equals("u")) { pig = line.substring(x, b - 1); } else { pig = line.substring(b - 1, x + 2) + line.charat(x); } line = line.substring(b + 1); y++; } system.out.println(pig);

i'm suppose convert words in sentence pig latin if word starts vowel add together way @ end if starts consonant bring first letter , add together way

stacktrace:

java.lang.stringindexoutofboundsexception: string index out of range: -1 @ java.lang.string.substring(unknown source) @ pig_latin.main(firstname_lastname.java:25) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:27‌​2

your method tracking words in line problem utilize string.split(" ") break line series of words , while loop can be

string[] words = line.split(" "); for(string word:words) { // pig latin on word }

i suspect it's substring on line that's failing don't terminate correctly when you've processed words because variable y based on characters, algorithm chunking words.

java

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -