import java.io.*;
import java.util.*;
import java.lang.String;
public class IgLatinPay
{
public static void main(String [] args)
{
String temporary = " ";
String piglatin = " ";
Scanner input = new Scanner(System.in);
System.out.println("Enter sentence that you want to translate: ");
String english = input.nextLine();
while(english.indexOf(' ') != -1)
{
temporary=english.substring(0,english.indexOf(' '));
english=english.substring(english.indexOf(' '));
if(temporary.charAt(0)=='a' || temporary.charAt(0)=='e' || temporary.charAt(0)=='i' || temporary.charAt(0)=='o' || temporary.charAt(0)=='u')
{
piglatin=temporary+"way";
System.out.println(piglatin);
}
else
{
char firstChar = temporary.charAt(0);
piglatin=temporary.substring(1,temporary.indexOf(' '))+ firstChar +"ay";
System.out.println(piglatin);
}
piglatin+=temporary+" ";
}
}
}
Pig latin translator errors
Page 1 of 11 Replies - 1133 Views - Last Post: 23 October 2007 - 03:37 PM
#1
Pig latin translator errors
Posted 23 October 2007 - 02:58 PM
Hello there. i need some help finishing up my pig latin translator program. It compiles fine, but when i run it and try to translate a sentence, it gives me a syntax error and i cannot see the error. i am almost done with it, please help!
Replies To: Pig latin translator errors
#2
Re: Pig latin translator errors
Posted 23 October 2007 - 03:37 PM
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(String.java:1768)
at IgLatinPay.main(IgLatinPay.java:31)
The above is the error that I got from attemting to run your code at line 31
piglatin=temporary.substring(1,temporary.indexOf(' '))+ firstChar +"ay";
the temporary.indexOf(' ') call is returning a -1, therefore
you are trying to do:
are executing the following:
piglatin = temporary.substr(1, -1) + "t" + "ay";
it doesn't know what to do with the -1, you have to check for a negative when doing the temporary.indexOf, and handle that case.
Oh, btw the "t" that I put in there is the character extracted from the input data that I used: "this is a test"...
at java.lang.String.substring(String.java:1768)
at IgLatinPay.main(IgLatinPay.java:31)
The above is the error that I got from attemting to run your code at line 31
piglatin=temporary.substring(1,temporary.indexOf(' '))+ firstChar +"ay";
the temporary.indexOf(' ') call is returning a -1, therefore
you are trying to do:
are executing the following:
piglatin = temporary.substr(1, -1) + "t" + "ay";
it doesn't know what to do with the -1, you have to check for a negative when doing the temporary.indexOf, and handle that case.
Oh, btw the "t" that I put in there is the character extracted from the input data that I used: "this is a test"...
This post has been edited by barker1: 23 October 2007 - 06:04 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|