Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] How can i have last number in List by index Java Android Studio

Posted on November 11, 2022 By

Solution 1 :

If you have an array with a lenght from 1-15, your pointer goes from 0-14. 0 is the first element and 14 the last. You get IndexOutOfBoundsException when you try to reach 15.

To create a list between 0 and 14 use:

Random rand = new Random();
List<Integer> rlist = new ArrayList<>();
while (rlist.size() != 15) {
int r = rand.nextInt(15) ;
if (!rlist.contains(r))
   rlist.add(r);
}

Problem :

I am doing a quiz and i have a class with String arrays of questions, choises and answers.
On my main class I have empty TextViev and buttons that are updating by a method:

 private void updateQuestion(int num){
    question.setText(mmQuestionFi.getQuestionFi(num));
    answer1.setText(mmQuestionFi.getFiChoice1(num));
    answer2.setText(mmQuestionFi.getFiChoice2(num));
    answer3.setText(mmQuestionFi.getFiChoice3(num));
    answer4.setText(mmQuestionFi.getFiChoice4(num));
    mAnswearFi= mmQuestionFi.getCorrectAnswearFi(num);
}

(answer1,2,3,4 – buttons ; mmQestionFi – class where i get data of arrays from)

Then i have a list of random index with a size 15 and from 1 to 15:

Random rand = new Random();
List<Integer> rlist = new ArrayList<>();
while (rlist.size() != 16) {
int r = rand.nextInt(16) ;
if (!rlist.contains(r))
   rlist.add(r);
}

I update question at first when the game hasnt started yet.

        updateQuestion(rlist.get(index));

And finaly i update my question like

 checkNumber++;
 if(checkNumber<16){
                if(checkNumber==2)
                    updateQuestion(rlist.get(index+1));
                if(checkNumber==3)
                    updateQuestion(rlist.get(index+2));
                if(checkNumber==4)
                    updateQuestion(rlist.get(index+3));
                if(checkNumber==5)
                    updateQuestion(rlist.get(index+4));
                if(checkNumber==6)
                    updateQuestion(rlist.get(index+5));
                if(checkNumber==7)
                    updateQuestion(rlist.get(index+6));
                if(checkNumber==8)
                    updateQuestion(rlist.get(index+7));
                if(checkNumber==9)
                    updateQuestion(rlist.get(index+8));
                if(checkNumber==10)
                    updateQuestion(rlist.get(index+9));
                if(checkNumber==11)
                    updateQuestion(rlist.get(index+10));
                if(checkNumber==12)
                    updateQuestion(rlist.get(index+11));
                if(checkNumber==13)
                    updateQuestion(rlist.get(index+12));
                if(checkNumber==14)
                    updateQuestion(rlist.get(index+13));
                if(checkNumber==15)
                    updateQuestion(rlist.get(index+14));

( checkNumber initialized before if as int checkNumber=1; 1 – because we update before any user action ( program starts and empty texfields are being filled with data) and only after that it will be updating with if ( when any button is pressed)
So the question is how to get all 15 questions ( get 15 index) and avoid ArrayIndexOutOfBoundsException: length=15; index=15
Thanks in advance!

Comments

Comment posted by Jugjin

That was so easy -_-. Absolutely forgot about start from 0. Thank you once more.

READ  [FIXED] java - I want to create my notification if the alert date is equal to now date and notification will create all dates from alert to vaccine date
Powered by Inline Related Posts

Android Tags:android-studio, java

Post navigation

Previous Post: [FIXED] Android Studios error : Unable to locate adb location
Next Post: [FIXED] android – Here Maps Navigation samples seem to be missing the Navigation Library

Related Posts

[FIXED] android – How to Cast a video using ExoPlayer cast extension from a Dynamic Feature Module Android
[FIXED] android – How do I enable access to Google API sensitive scopes with ‘Unverified’ mobile app under development? Android
[FIXED] android – How to detect video is Vertical or Horizontal in ExoPlayer Android
[FIXED] android – java.net.UnknownHostException: Unable to resolve host “api.themoviedb.org Android
[FIXED] android – How to parse a zipped file completely from RAM? Android
[FIXED] Replacement for “GROUP BY” in ContentResolver query in Android Q ( Android 10, API 29 changes) Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme