Results 1 to 5 of 5
  1. #1
    Agent is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Printing Out Every Other Letter

    Hey guys, I just started programming with java about a month ago and I am stuck on this project where I must take a user's input and output all the even positioned letters on the first line and all the odd positioned letters on the second line while removing all spaces and changing everything to upper case. I have written my code, but in some cases it doesn't output the second line or it gives me an error that looks like this:

    java.lang.StringIndexOutOfBoundsException: String index out of range: 24
    at java.lang.String.substring(Unknown Source)
    at Message.main(Message.java:46)
    Any help or suggestions would be greatly appreciated, my code looks like this:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Message
    {
      public static void main (String args[])
      {
        //Declaring Variables
        Scanner kbReader = new Scanner (System.in); //Reads user's input.
        String input; //The user's inputted message.
        String modified; //The user's inputted string with the spaces removed.
        String modded; //The user's inputted string with the spaces removed and stored in all capital letters.
        int length; //The length of the user's inputted message.
        int i; //The variable used for the loop structures.
        
        //Getting the user's input
        System.out.println("Welcome to the message encryption program.");
        System.out.println("Your message will be changed so that your teacher will not be able to understand it.");
        System.out.println("Please enter your message.");
        input = kbReader.nextLine();
    
        //Modifying the user's input, by removing space and changing all the letters to upper case.
        modified = input.replace(" ", "");
        modded = modified.toUpperCase();
        
        //Printing out all the odd positioned letters.
        length = modded.length();
        for (i = 0; i <= length; i+=2)    
        {
          System.out.print(modded.substring(i, i+1));
        }
        
        //Going to the next line to print all the even positioned letters.
        System.out.println("");
        
        //Printing out all the even positioned letters.
        for (i = 1; i <= length; i+=2)
        {
          System.out.print(modded.substring(i, i+1));
        }
      } //Main method
    } //Message Class

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Change the condition of your for loops to i<length, instead of i<=length. That's what's causing the string out of bounds error. The biggest index of a string is length-1. In your current code, you're trying to get the letter of index, "length", which doesn't exist.

  3. #3
    Agent is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by matzahboy View Post
    Change the condition of your for loops to i<length, instead of i<=length. That's what's causing the string out of bounds error. The biggest index of a string is length-1. In your current code, you're trying to get the letter of index, "length", which doesn't exist.
    Thanks for the quick reply and removing the "=" sign worked. :)

  4. #4
    Agent is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    I've now been modify my encryption program to output the message in one line, read row by row with a space input after every 5 letters. I tried adding this code at the end but I am getting the same error again. Any suggestions? Here's the code I added in:

    Java Code:
     for (i = 0; i < length; i+=2) 
        {
          String bonus = modded.substring(i, i+1);
          for (int j = 0; j < length; j+=5)
          {
          System.out.println("");
          System.out.print(bonus.substring(j,j+4));
          }
        }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    when you try use i + 1 and j + 4 you risk going over the length of the String. You should perhaps rethink your logic here. How would you solve this with only a pen and paper? Figure out the necessary steps to do this first, and only then translate those steps into Java.

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Letter with Letters
    By elgatoboricua in forum New To Java
    Replies: 7
    Last Post: 09-16-2008, 02:59 PM
  3. Replies: 0
    Last Post: 06-25-2008, 10:34 AM
  4. Replies: 20
    Last Post: 05-14-2008, 09:42 AM
  5. Drive letter sniff/open explorer window
    By iiz in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 03:50 AM

Posting Permissions

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