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 3 of 3

Thread: I keep getting an error message but I don't know how to fix this!

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I keep getting an error message but I don't know how to fix this!

    I have to create a simple program that allows the user to input the date in the following format : month day, year and the program is supposed to output the date in this format: date-month-year. My code keeps giving me that my string index is out of bounds but I don't know how to fix it. Help please!
    import java.util.Scanner;
    public class UserDate {
    	public static void main (String [] args){
    		Scanner input = new Scanner(System.in);
    		String userDate = null;
    		String month = null;
    		String day = null;
    		String year = null;
    		int firstBlank = 0;
    		int comma = 0;
     
     
    		System.out.println("Please enter month day, year in that format");
    		userDate = input.nextLine();
     
    		firstBlank = userDate.indexOf(0,' ');
    		comma = userDate.indexOf(' ', ',');
     
    		month = userDate.substring(0,' ');
    		day = userDate.substring(' ',comma);
    		year = userDate.substring(comma);
     
    		System.out.println(day + "-" + month + "-" + year);
     
    }
    }
    note that the ' ' is supposed to represent a space


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I keep getting an error message but I don't know how to fix this!

    Quote Originally Posted by mpagudelo View Post
    I keep getting an error message
    Always include the full text of the error message with your code and question

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I keep getting an error message but I don't know how to fix this!

    Never mind I resolved it, thanks though!

Similar Threads

  1. Need help with error message!!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 14
    Last Post: March 25th, 2013, 10:24 AM
  2. Error message when using JOptionPane
    By runkerr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 5th, 2013, 12:17 PM
  3. Replies: 1
    Last Post: June 9th, 2012, 11:29 AM
  4. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  5. help with a error message
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2010, 02:56 PM