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

Frank Kulash

Hi, @user-e2y4b
This question was posted in the English-language space. There's a separate space for Spanish language:
Base de Datos Discusión General — oracle-tech
I'll try to move the thread there for you.

mathguy

Please note that this forum es solo en ingles. Either you state your question and accept answers in English, or look for a Spanish language forum (knowing that participation will be much more limited).
As to the error: Tables like AH135D are understood within the subquery (the join), but not outside it. The UPDATE statement only sees the result of the subquery, with its columns.
Either move the WHERE clause INSIDE the query, or give the subquery and its columns aliases and use those in the WHERE clause. (You may leave out the subquery alias - and perhaps even the column aliases, if there is no risk of confusion - but in a problem like this it is a best practice to alias the subquery and also the columns within it.)

1 - 2
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
560 views