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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

fac586
Answer

Hi Scott
The checkboxes are a feature that is enabled when Page Designer is used on a device with a touch-based UI. This is therefore likely to be related to the switch to iPad emulation. Check that all of the relevant browser options have been reset, but you may need to shut down the entire browser session and clear the site data.

Marked as Answer by Scott Wesley · Sep 6 2021
Scott Wesley

That makes sense, thanks.
I couldn't find a relevant setting, but a restart of the browser did the job.

McRivers

Just stumbled across this in 21.2. Thought I was going crazy "those checkboxes haven't 'always' been there, right?" i was saying to myself. ;)
@fac586 appreciate the solution...

Scott Wesley

Based on information from Magaly Iraheta on Twitter, and combining that with what I new I did, I can now recreate and “fix ”this on demand, with no need for browser restart.

If you enable mobile emulation (toggle device toolbar) with the developer tools, then click anywhere in the application builder, the key APEX.userHasTouched appears in Application-Session Storage.

On refresh of the builder page, these checkboxes will appear.

If you delete this key, then refresh the page, the checkboxes will no longer be present.

Thanks again, Magaly.

1 - 4
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
553 views