How to insert a date variable in java?

Java : Exception in thread "main" java.lang.StringIndexOutOfBoun…

  • How to use substring in Java? When compile, it doesn't show any error but during run this program, an exception's happened....anyone knows about this? Please advice. import java.util.Scanner; import javax.swing.JOptionPane; import java.lang.String; public class Assg1 { public static void main (String[] args) { int date,month; String tahun,newYear; Scanner keyboard = new Scanner (System.in); JOptionPane.showMessageDialog (null,"Welcome! Please insert your D.O.B."); System.out.println("Month/Bulan [xx]: "); month = keyboard.nextInt(); System.out.println("Date/Hari Bulan [xx]: "); date = keyboard.nextInt(); System.out.println("Year/Tahun [xxxx]: "); tahun = keyboard.nextLine(); newYear = tahun.substring(2); int twoDigitYear = Integer.parseInt(newYear); //System.out.println(twoDigitYear); //Step 1: Add 18 to birth month int step1 = month + 18; //Step2: Multiply by 25 int step2 = step1*25; //Step3: Subtract 333 int step3 = step2-333; //Step4: Multiply by 8 int step4 = step3*8; //step5: Subtract 554 int step5 = step4-554; //step6: Divide by 2 int step6 = (step5)/2; //step7: Add your birth date int step7 = step6 + date; //step8: Multiply by 5 int step8 = (step7)*5; //step9: Add 692 int step9 = step8+692; //step10: Multiply by 20 int step10 = step9*20; //step11: Add only the last two digits of your birth year int step11 = step10 + twoDigitYear; //step12: Subtract 32940 to get your birthday! int step12 = step11-32940; System.out.println(step12); //JOptionPane.showMessageDialog (null,"Your birthday is : "+); } }

  • Answer:

    newYear = tahun.substring(2); This must be causing the problem. Year obviously isn't 3 digits long. If you enter something like 78 (for 1978), that error will occur.

ms effa at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.