How Do I Complete This Java Program?

I already have

import java.util.Arrays;

public class firstprogram11beromi {

public static void main(String[] args) {

int[] numbers = {19, 13, 17, 12, 16, 15, 18};

Arrays.sort(numbers);

int num = numbers[0];

for (int i = 1; i < numbers.length; i++){

num ++;

if(num != numbers[i])

System.out.println(numbers);

break;

}

}

}

as my code (as per the suggestion of a user earlier, as well as a minor modification) in order to take those numbers, sort them from least to greatest, and then fill in the missing number (14)

I am a beginner in Java, and I am trying to finish this up so I can go to sleep, because I have been at it for hours. Any and all help is greatly welcomed.

Comments

  • Ohhh, I see what you're trying to do.

    Here it is:

    public static void main(String[] args) {

    int[] numbers = {5, 8, 9, 6, 10};

    boolean quit = false;

    Arrays.sort(numbers);

    int num = numbers[0];

    while (num <= numbers[numbers.length - 1] && !quit) {

    for (int i = 1; i < numbers.length; i++) {

    if (num == numbers[i]) {

    quit = true;

    break;

    }

    }

    num++;

    }

    System.out.println("missing number is: " + num);

    }

    output:

    missing number is: 7

    +add

    Here's another way that compares the correct sum with the calculated sum. It's a little better in that it's calculation rather than logic. Most errors are logic errors, not calculation errors. It's probably possible to do both sums in 1 loop:

    // another way:

    // get the sum:

    int sum = 0;

    for (int k = 0; k < numbers.length; k++) {

    sum += numbers[k];

    }

    // get what the sum should be:

    int corrSum = 0;

    int k = numbers[0];

    while (k <= numbers[numbers.length - 1]) {

    corrSum += k;

    k++;

    }

    // missing numbers is the difference:

    System.out.println(

    "Missing number: " + (corrSum - sum));

  • Here is the correct program

    import java.util.Arrays;

    public class firstprogram11beromi {

    public static void main(String[] args) {

    int[] numbers = {19, 13, 17, 12, 16, 15, 18};

    Arrays.sort(numbers);

    int num = numbers[0];

    for (int i = 1; i < numbers.length; i++){

    System.out.println(num);

    num ++;

    if(num != numbers[i])

    {

    i--;

    }

    }

    }

    }

  • instead of going for a "for" loop , just include this statement directly ! It'll show you the sorted array. just discard the for loop & whatever is included inside the for loop & include this statement only !

    System.out.println("the nos"+Arrays.toString(numbers));

    The correct code will be:

    import java.util.Arrays;

    public class num {

    public static void main(String[] args) {

    int[] numbers = {19, 13, 17, 12, 16, 15, 18};

    Arrays.sort(numbers);

    System.out.println("the sorted numbers are"+Arrays.toString(numbers));

    }

    }

  • import java.util.Arrays;

    public class Sample3 {

    public static void main(String args[]) {

    int numSet[] = {19, 13, 17, 12, 16, 15, 18};

    boolean hasFound = false;

    Arrays.sort(numSet);

    int min = numSet[0];

    int max = numSet[numSet.length-1];

    int i2 = 0;

    for(int i = min;i<max;i++) {

    System.out.println(i);

    }

    System.out.println(max);

    }

    }

    That's my answer :)

  • int s=a.length; int b[]=new int[s];//no of elemnts in a & b comparable int j=0; for(int i=0;i<s;i++) { if(a[i]>0) { b[j]=a[i]; //b[ j ] because of the fact if we take b[i] then there'll be some array indices havin no fee j++; } } for(i=0;i<b.length;i++) equipment.out.println(b[i]);

Sign In or Register to comment.