Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I charAt In a Input with a variable length, without getting the StringIndexOutOfBoundsExcepti

DynamiAug 16 2017 — edited Aug 21 2017

Hello. I've been trying to make a timer in an applet, by making it

1. You input the Hours:Minutes:Seconds

2. The program will separate them into different ints.

3. It will subtract 1 from the "Sec" Int every second, and when Sec runs out it will subtract from Min, And when it ends it will subtract from Hour, and at last, Play a sound.

But, because not always you will have Hours or Minutes on the timer, when I try to use charAt to extract the digits (on the second step) some times it throws a StringIndexOutOfBoundsException.

I'm probably doing the unefficient way, and I'm certain that I have more than 1 mistake in my code.

I am not asking someone to spoonfeed the code. But I would really appreciate if someone could enlighten on what I'm doing wrong here.

Thanks in advance.

(Also, this code is uncompilable. But I will ask about that in another discussion.)

  private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

        int HMS = Integer.parseInt(jTextField1.getText());

      String HourMinSec = Integer.toString(HMS);

if(HourMinSec.regionMatches(0,"00000",0,5)){

     int Sec = (int) HourMinSec.charAt(0);

     int Min = 0;

     int Hour = 0;

     } else

{ if (HourMinSec.regionMatches(0,"0000",0,4)){

     int Sec = (int) HourMinSec.charAt(0) * 10 + (int) HourMinSec.charAt(1);

     int Min = 0;

     int Hour = 0;} else{

   if (HourMinSec.regionMatches(0,"000",0,4)){

     int Sec = (int) HourMinSec.charAt(1) * 10 + (int) HourMinSec.charAt(2);

     int Min = (int) HourMinSec.charAt(0);

     int Hour = 0;}

     else {

      if(HourMinSec.regionMatches(0,"00",0,2)){ 

      int Sec = (int) HourMinSec.charAt(2) * 10 + (int) HourMinSec.charAt(3);

      int Min = (int) HourMinSec.charAt(0) * 10 + (int) HourMinSec.charAt(1);

      int Hour = 0;

      } else {

      if(HourMinSec.regionMatches(0,"0",0,1)){ 

      int Sec = (int) HourMinSec.charAt(3) * 10 + (int) HourMinSec.charAt(4);

      int Min = (int) HourMinSec.charAt(1) * 10 + (int) HourMinSec.charAt(2);

      int Hour =(int) HourMinSec.charAt(0);

      } else {

      int Sec = (int) HourMinSec.charAt(4) * 10 + (int) HourMinSec.charAt(5);

      int Min = (int) HourMinSec.charAt(2) * 10 + (int) HourMinSec.charAt(3);

      int Hour =(int) HourMinSec.charAt(0) * 10 + (int) HourMinSec.charAt(1);}}}}

      while(Sec != 0 || Min != 0 || Hour != 0){

          Thread.sleep(9);

        Sec -= 1;

        if(Sec == 0){

         Min -= 1;

          Sec = 60;}

        if(Min == 0){

            Hour -= 1;

            Min = 59;

        }

      jTextField1.setText(Hour + ":" + Min + ":" + Sec);

    }

     

    }                          

This post has been answered by mNem on Aug 17 2017
Jump to Answer

Comments

wbriceno

Hi Bob and Lucas;

The information was interesting. As an Oracle University instructor I've allways explain this feature of clear form to the students.

Thanks and continue doing what you do.

Regards

1 - 1
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 18 2017
Added on Aug 16 2017
5 comments
559 views