PLEASEEEEEEHELP MEEEEE ITS DUE TMR String index out of range: -1
Author |
Message |
worthless
|
Posted: Tue Mar 31, 2015 3:51 pm Post subject: PLEASEEEEEEHELP MEEEEE ITS DUE TMR String index out of range: -1 |
|
|
Java: |
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Rovarspraket
*
* @author
* @version
*
*/
public class Rovarspraket
{
public static void main (String[] args ) throws FileNotFoundException
{
Scanner inFile = new Scanner (new File("src/Rovarspraket.txt"));
String line = inFile. nextLine(). toLowerCase();
String consonant = "bcdfghjklmnpqrstvwxyz";
String closestVowel = "aaeeeiiiiooooouuuuuuu";
String newLine = "";
for (int pos = 0; pos < line. length(); pos++ )
{
char letter = line. charAt(pos );
int consonantPos = consonant. indexOf(letter );
char vowel=closestVowel. charAt(consonantPos );
char nextConsonant = (char ) (letter + 1);
String letterString = Character. toString(letter );
String vowelString = Character. toString(vowel );
String nextConsonantString = Character. toString(nextConsonant );
newLine = letterString + vowelString
+ nextConsonantString;
if (consonantPos==- 1)
{
String unchangeVowelString = Character. toString(letter );
newLine+=unchangeVowelString;
}
}
inFile. close();
}
}
|
AND I GOT THIS
code: |
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(Unknown Source)
at Rovarspraket.main(Rovarspraket.java:27)
|
Mod Edit: Please syntax="java" or code BBcode tags to wrap your code and stuff. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Mar 31, 2015 5:00 pm Post subject: RE:PLEASEEEEEEHELP MEEEEE ITS DUE TMR String index out of range: -1 |
|
|
String.indexOf() will return -1 if it can't find the string you supply it.
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#indexOf(java.lang.String)
Try debugging what character you're trying to find. Use System.out.println() to print the values of your variables.
Example:
Java: |
System. out. format("pos=%s\n", pos );
char letter = line. charAt(pos );
System. out. format("letter=%s\n", letter );
int consonantPos = consonant. indexOf(letter );
System. out. format("consonantPos=%s\n", consonantPos );
char vowel=closestVowel. charAt(consonantPos );
System. out. format("vowel=%s\n", vowel );
|
I'm guessing you forgot about the newline character (\n) or the whitespace character.
http://www.asciitable.com/index/asciifull.gif |
|
|
|
|
![](images/spacer.gif) |
|
|