Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple code error

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple code error

    I am getting an error with this code

     import java.util.Scanner;
     
    public class five
    {
    public static void main(String[] args)
    {
     
    Scanner keyboard = new Scanner(System.in);
    String sentence = "Please enter a line of text. No punctuation please";
     
    System.out.print(sentence);
     
    String s1, firstWord, firstLetter, endSentence, newSentence;
    s1 = keyboard.nextLine();
     
    int wordEndLoc = s1.indexOf(' ');
     
    //retrieve the first word to be replaced
    firstWord = s1.substring(0,wordEndLoc);
     
    //get the second part of the sentence without the first word and trim trailing spaces
    endSentence = s1.substring(wordEndLoc,s1.length()).trim();
     
    //combine the second part with the first word at the end
    newSentence = endSentence + " " + firstWord;
     
    //retrieve he first letter of the newly organized sentence, and capitalize it as well
    firstLetter = newSentence.substring(0,1).toUpperCase();
     
    //again, get the second part of the sentence (without the first letter)
    endSentence = newSentence.substring(1,s1.length());
     
     
    //combine the first letter with the second part
    newSentence = firstLetter + endSentence;
    String mess2 = "I have replaced that line to read: ";
    System.out.print(mess2);
    System.out.print(newSentence);
    }
    }

    My output says
    Please enter a line of text. No punctuation please
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at five.main(five.java:18)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Simple code error

    indexOf returns -1 if the search item was not found, so if what a space isn't found you will see this exception when your code continues to try and parse the string.

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (December 7th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple code error

    Hmm. So what do I need to do to fix this? Im sorry, I am very new at this...Thank you for the reply

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Simple code error

    Check and make sure the value is not -1
    if ( wordEndLoc == -1 ){
    // most likely the user entered a single word or just pressed the enter button (the s1 string is empty).
    //Deal with each however you wish your program to respond to each.
    }

  6. The Following 2 Users Say Thank You to copeg For This Useful Post:

    javapenguin (December 7th, 2010), robin28 (December 7th, 2010)

Similar Threads

  1. Error with code
    By JJTierney in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 04:23 PM
  2. Simple error can't figure out.
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 30th, 2010, 12:19 PM
  3. im dying here..on a simple code
    By Jason in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 28th, 2010, 10:33 PM
  4. Help! how would you code these simple games?
    By makarov in forum Java Applets
    Replies: 1
    Last Post: November 14th, 2009, 11:31 AM
  5. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM