0
import java.util.Scanner;

public class BubbleInt {

    public static void main(String [] args){
        Scanner sc1 = new Scanner(System.in);
        System.out.println("Please enter the total amount of number:);
        int n = sc1.nextInt();
        int [] missing = new int [n];
        System.out.println("Please enter your numbers:");
        for(int i=0; i<n; i++) {
            missing[i] = sc1.nextInt();
        }
        //Sorting from largest to smallest
        int temp = 0;
        for(int i =0; i<missing.length; i++) {
            for(int j =0; j<missing-1; j++) {
                if(missing[i] > missing [j+1] {
                    temp = missing[j+1];
                    missing[j+1] = missing[i];
                    missing[i] = temp;
                }
            }
        }
       //Displaying 
       for(int i = 0; i<missing.length; i++) {
           System.out.println(missing[i] + " ");
       }
   }
}

I want to sort an array from largest to smallest which the above code does perfectly but I want to check if there are two integers of the same number of digits. For example if I inputted 77,23,5,1,7,101 the output should be 101 23 77 1 5 7, since 1,5,7 and 23, 77 are of the same number of digits they are reversed.How could i check the elements are of the same length and reverse only them.

Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
New user
  • 57
  • 3
  • 1
    When I copied the code from your question to my IDE, it did not compile. You seem to be missing a closing bracket on this line: `if(missing[i] > missing [j+1]` Also this line contains a compiler error: `for(int j =0; j – Abra Apr 11 '20 at 10:18
  • `I want to sort an array from largest to smallest which the above code does perfectly` - No, it doesn't work correctly for numbers `13 133 1 3 163`. Also, do address the points raised by @Abra – Arvind Kumar Avinash Apr 11 '20 at 11:13
  • 1
    Does this answer your question? [Way to get number of digits in an int?](https://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int) – Joe Apr 11 '20 at 11:28

3 Answers3

1

To get the number of digits of an integer i > 0, try this:

var numDigits = ((int) Math.log10( i )) + 1;
tquadrat
  • 3,033
  • 1
  • 16
  • 29
  • Why should `var numDigits` changed to `int numDigits`? Both is valid Java … – tquadrat Apr 11 '20 at 10:26
  • `var` is a valid keyword only for Java10 onwards. – Arvind Kumar Avinash Apr 11 '20 at 10:32
  • @ArvindKumarAvinash: … and Java 11 is the current (as for 2020-04-11) LTS version, while Java 14 is the latest Java version available. If you are stuck to Java 8, your pity. – tquadrat Apr 11 '20 at 10:41
  • @ArvindKumarAvinash: Not to mention that `Math.log10()` was first introduced with Java 5 … – tquadrat Apr 11 '20 at 10:43
  • @tquarant - I don't understand why did you get hurt by my comment to change `var` to `int`. I'm not stuck at Java 8 but most of Java applications are still running on version 9 or less. As I've already mentioned `var` was introduced in Java10, so by changing `var numDigits` to `int numDigits`, you can ensure that it will work for most of Java compilers (I mean version 9 or below). Also, I didn't understand your comment: `Math.log10() was first introduced with Java 5`. Did anyone challenge it? – Arvind Kumar Avinash Apr 11 '20 at 10:53
  • @ArvindKumarAvinash: I am not hurt by your request to change it. If I would be hurt at all, it would be by your reason for that request. Most probably, this answer will be still visible when most programmers cannot remember the time before `var` was not a valid keyword, same a most programmers nowadays cannot really remember the time before generics were part of the language. Java 8 is as dead as COBOL – although both are still used … Zombies live longer! – tquadrat Apr 11 '20 at 11:00
  • @tquadrat - COBOL is not dead, far from it. Most of your fancy fintech transactions, credit card transactions and bank accounts are processed by COBOL, and you would be surprised by how many new lines of code are being written in COBOL every day. Same goes for Java 8, it takes a long time to upgrade application systems especially when they depend on several frameworks and other external software. – Jonathan Rosenne Apr 11 '20 at 18:10
  • @JonathanRosenne: As said: Zombies live longer … – tquadrat Apr 11 '20 at 20:35
  • @tquadrat - now is your chance to learn COBOL for free: [IBM and Open Mainframe Project Mobilize to Connect States with COBOL Skills] (https://newsroom.ibm.com/2020-04-09-IBM-and-Open-Mainframe-Project-Mobilize-to-Connect-States-with-COBOL-Skills) – Jonathan Rosenne Apr 12 '20 at 06:14
  • @JonathanRosenne: I started my career with COBOL … ok, today, it is rusted, but I can still read COBOL source code, and for my current job, that's sufficient. But thank you for the link! – tquadrat Apr 12 '20 at 07:40
  • According to this [report](https://www.jrebel.com/blog/2020-java-technology-report) less than one quarter of java development uses JDK 11 while almost three fifths use JDK 8. Also, have you not heard that there is a [shortage of COBOL programmers](https://edition.cnn.com/2020/04/08/business/coronavirus-cobol-programmers-new-jersey-trnd/index.html) ? – Abra Apr 12 '20 at 08:02
  • @Abra: I remember similar reports from mid 2006, about the use of Java 1.4 and Java 5, with similar figures … ok, this was before Stackoverflow came into existence, so it is unlikely that we will find the same discussion, regarding the use of generics that are not used by the majority of Java programmers yet, here on this forum … and regarding COBOL programmers: currently I have a decent job, thank you. – tquadrat Apr 12 '20 at 11:35
1

The condition inside the swapping loop can be updated to include 'number of digits' as follows:

for( int i = 0; i < missing.length; i++ ) {

    for( int j = 1; j < missing.length; j++ ) {

        // Find the number of digits for ith and jth elements
        int iDigits = ((int)Math.log10(missing[i]) + 1);
        int jDigits = ((int)Math.log10(missing[j]) + 1);

        // Perform a swap if iDigits > jDigits or ith element > jth element
        if( (iDigits > jDigits) || (missing[i] > missing[j]) ) {
            temp = missing[j];
            missing[j] = missing[i];
tquadrat
  • 3,033
  • 1
  • 16
  • 29
Gopinath
  • 4,066
  • 1
  • 14
  • 16
0

A Java 11 solution:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class BubbleInt {
    public static void main(String[] args) {
        var sc1 = new Scanner(System.in);
        System.out.println("Please enter the total amount of number:");
        var n = sc1.nextInt();
        var missing = new ArrayList<Integer>(n);
        System.out.println("Please enter your numbers:");
        while (missing.size() < n) {
            missing.add(sc1.nextInt());
        }
        missing.stream()
                .sorted(Comparator.comparing((Integer i) -> (int) Math.log10(i))
                        .reversed()
                        .thenComparing(Comparator.naturalOrder()))
                .forEach(i -> System.out.println(i + " "));
    }
}
Jonathan Rosenne
  • 2,159
  • 17
  • 27