Mairead
Mairead

Reputation: 275

For loops crashing my android app

I am writing a code relating to finding prime numbers, and it was working great until I realized I needed to divide each number in the ArrayList by all the numbers that came before it (ex: if the ArrayList was {1, 2, 3, 4, 5} I want to divide 5 by 4, 3, 2, and 1; 4 by 3, 2, and 1, etc.)

Because the contents of the ArrayList vary depending on what the user of the app inputted, I figured the best way to go about doing what I needed is this:

public class DisplayMessageActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    int limit = Integer.parseInt(message);

    ArrayList<Integer> finalPrime = new ArrayList<>();
    ArrayList<Integer> primeNumber = new ArrayList<>();

    for (int i = 1; i <= limit; i++) {

        if (!(i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0))
            primeNumber.add(i);
    }

    for (int k = 0; k <= limit; k++) {
        for (int j = 0; j <= k; j++) {
            int a = primeNumber.get(k) % primeNumber.get(j);
            if (!(a == 0)) {
                finalPrime.add(k);
                break;
            }
        }
    }

        String primes = primeNumber.toString();

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(primes);
    }
}

'message' is the number that the user inputs into the textbox at the beginning, which sets the limit for how high they want the prime numbers to count up to.

Android Dev Studio's IDE isn't saying there are any bugs in my code, and logically it should work, but when I try to run it in the Device Simulator, the app crashes after I click the 'send' button, which then switches the activities to DisaplayMessageActivity.

I took out the nested for loops, and the code ran perfectly and without error, so my questions are: (a) is there a more efficient way to go about doing what I aim to do; and (b) why isn't my code working?

Edit: Here is the LogCat at the time of the crash

06-20 13:09:57.459 12826-12826/com.example.name.primenumberfinder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: com.example.name.primenumberfinder, PID: 12826
                                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.name.primenumberfinder/com.example.name.primenumberfinder.DisplayMessageActivity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                                           Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
                                                                                              at java.util.ArrayList.get(ArrayList.java:411)
                                                                                              at com.example.name.primenumberfinder.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:32)
                                                                                              at android.app.Activity.performCreate(Activity.java:6662)
                                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

Upvotes: 1

Views: 799

Answers (1)

Sandeep dhiman
Sandeep dhiman

Reputation: 1921

Change your code for dividing each number in array list with rest of the items(number). Here's the code

int n =  primeNumber.size();
for (int k = 0; k <n-1; k++) {
        for (int j = 0; j <n-k-1; j++) {
            int a = primeNumber.get(j) % primeNumber.get(j+1);
            if (a != 0) {
                finalPrime.add(k);
                break;
            }
        }
    }

Upvotes: 1

Related Questions