Results 1 to 2 of 2
  1. #1
    lornapowell13 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default can get to complie but getting an error

    Its probably something simple but can compile this code but I am getting an error that I cannot resolve:

    Exception in thread "main" java.lang.StringIndexOutOfoundsException: String index out of range :-2
    at java.lang.String.substring(unknown Source)
    at Plurals.main(Plurals.java:22)

    import java.util.Scanner;
    public class Plurals
    {
    public static void main(String[]args)
    {
    String result ="";
    String punultimateChar = "";
    String word = "";
    String vowels = "aeiou";
    String con = "bcdfghjklmnpqrstvwxyz";
    String wordCopy = word;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a word ending in y");
    word = in.nextLine();
    word = word.toLowerCase();
    if(word.endsWith("y"))
    {
    if(word.endsWith("quy"))

    result = wordCopy.substring(0,wordCopy.length()-1 )+("ies");
    else
    punultimateChar = wordCopy.substring(wordCopy.length()-2,wordCopy.length()-1);
    if(vowels.indexOf(punultimateChar)!=-1)
    result = wordCopy +"s";
    else if(con.indexOf(punultimateChar)!=1)
    result = wordCopy.substring(0,wordCopy.length()-1)+("ies");
    }
    else
    result = "Not a valid word";
    System.out.println(result);
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: can get to complie but getting an error

    The error message tells you exactly where the error happened: in line #22 of your main( ... ) method; my guess is that variable wordCopy is an empty String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 06:26 PM
  2. Cannot complie JApplet in Eclipe
    By fantasyme in forum Eclipse
    Replies: 5
    Last Post: 04-15-2010, 05:07 AM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 3
    Last Post: 03-03-2009, 10:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •