Hello, Script error someone help please?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Hello, Script error someone help please?

  1. #1
    Join Date
    Aug 2004
    Posts
    1

    Hello, Script error someone help please?

    Well, i am very new to java and am taking a computer science course at my school. for our first project we where asked to create a very simple code that took the users birthdate input (month date, year [format]). Then it took that birthdate and broke it up using strings, displaying the users month of birth date of birth and year of birth into a message box.

    My problem is that i got everything to work except for when it shows the user Date of birth, for me it always adds the , year next to the date and everything i have tried yas lead me into complier errors or not done the desired results

    well here is my code can anyone help me fix the problem?




    /* Author: Arsalan Modjbafan
    * Due date: 08-25-04
    * Lab #: Lab 2A
    * File: Birthday.Java
    *
    * The Lab2A class will bring up an input box that asks for a users birth date
    * Then uses that information to show them a message spliting up the Month Day and Year
    */

    import javax.swing.*;

    public class Birthday {

    public static void main( String[] args) {




    JFrame lab2A;

    lab2A = new JFrame();

    lab2A.setSize(700,400);
    lab2A.setLocation(324,0);
    lab2A.setTitle("Lab 2A");
    lab2A.setVisible(true);



    String month, day, year, birthday;


    birthday = JOptionPane.showInputDialog(lab2A,
    "Enter birth date in the format Month Day, Year\nFor example: August 25, 2004");




    month = birthday.substring(0, birthday.indexOf(" "));

    day = birthday.substring(birthday.indexOf(" ") + 1);

    year = birthday.substring(birthday.indexOf(", ") + 1);

    JOptionPane.showMessageDialog(lab2A, "Month: " + month + "\nDay: " + day + "\nYear: " + year);


    }

    }
    Amature Computer Science Student please help me!

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    The problem is the substring method you're using to get the date. It only has one paramter, the start index. It substrings from that index to the end of the string. Obviously that will always include the year along with the date. Instead you need to use the same substring method you use for the month. It takes a start index and an end index, and cuts out just that part of the string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center