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:
Any help or suggestions would be greatly appreciated, my code looks like this:
java.lang.StringIndexOutOfBoundsException: String index out of range: 24
at java.lang.String.substring(Unknown Source)
public class Message
public static void main (String args)
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)
//Going to the next line to print all the even positioned letters.
//Printing out all the even positioned letters.
for (i = 1; i <= length; i+=2)
} //Main method
} //Message Class