Next number in Java

+2 Will Moffat · November 9, 2014
Hey.

I'm writing a program, and for part of it I need to get the next number in a sequence. So, say I have a list of numbers like this: { 2, 1, 12, 5, 4, 7, 8, 11} and my current number is 5.  How can I get to the next number up in the sequence from 5 (which in case would be 7)?

Thanks fo any help you can give.

- Will

Post a Reply

Replies

Oldest  Newest  Rating
0 jan burg · November 10, 2014
No problem. Here's the tutorials in case you can't find them:

https://www.youtube.com/watch?v=Sj2kCLjZZgk
https://www.youtube.com/watch?v=1QhS0aTiFhQ
0 Will Moffat · November 10, 2014
Beautiful, thank you. I'll go look at his tutorial now
0 jan burg · November 10, 2014
Ahh, I got it. This is what you want:


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class tester {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,4,7};
        int input=5;
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i:arr)
            list.add(i);
        Collections.sort(list);
        Integer[] arr2 = list.toArray(new Integer[arr.length]);
        for (int i=0; i<arr2.length; i++){
            if (((i+1)<arr2.length) && (arr2[_i_] == input))
                System.out.println(arr2[i+1]);
            else if (((i+1)==arr2.length) && (arr2[_i_] == input))
                System.out.println(arr2[_i_]);    
        }
        }
}


***REmove underscores in arr2[_i_].

Change input to the first number you're ttalking about. The array arr is your input array.

You convert it to a list so you can sort it, then you convert it back to an array to pick out the next entry in the array. Bucky had this in his tut's.

You can also do it without turning the list back to an array:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class tester {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,11,7};
        int input=3;
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i:arr)
            list.add(i);
        Collections.sort(list);
        for (int i=0;i<list.size();i++){
            if (((i+1)<list.size()) && (list.get(i) == input))
                System.out.println(list.get(i+1));
            else if (list.get(i) == input)
                System.out.println(list.get(i));
        }
        }
}
0 Will Moffat · November 10, 2014
No sorry, I think I worded this badly. From what I can tell, that gets the next term in the sequence so in my given instance it would return 4. 

I mean the next value, so it would find the number in the array where the value is the higher, but has gone up by as little as possible.

Examples use the arrays {2, 1, 12 ,5, 4, 7, 8, 11}:

  • If it was at 1 it would go to 2

  • If it was 11 it would go to 12

  • If it was 4 it would go to 5

  • If it was 5 it would go to 7

  • If it was 12 it would stay at 12

  • If it was 8 it would go to 11

  • etc...



I should point out now, I don't know what the actual values will be. I just made that array up.
0 Alex Sweps · November 10, 2014
This should do it too.


int[] arr2 = {2, 1, 12, 5, 4, 7, 8, 11};
for(int i = 0; i < arr2.length; i++)
     if(i == 5){
          System.out.println(arr2);
          break;
}



and yeah theres a bug in this code window that removes the [_i_]

It should be System.out.println(arr2[_i_]);  without the underscores...
0 jan burg · November 9, 2014
I think you mean to do something like this:


int[] arr2 = { 2, 1, 12,5, 4, 7, 8, 11}; //insert any array here
for (int i=0; i<arr2.length; i++){
      //if the next number exists and the current number is 5....
      if (((i+1) != arr2.length) && (arr2 == 5))
           //print out  the next entry in the array
           System.out.println(arr2[i+1]);
}

arr2 == 5 should be "arr2_[_i_] == 5" without underscores. BBCode is messing up real bad. Fix bucky!
  • 1

Java / Android Development

107,210 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator