Java BigDecimal

Sort by

recency

|

496 Discussions

|

  • + 0 comments
    package introduction.Java_BigDecimal_25;
    
    import java.math.BigDecimal;
    import java.util.*;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = Integer.parseInt(scanner.nextLine());
    
            List<String> list = enterData(n, scanner);
            List<BigDecimal >bigList = list.stream().map(BigDecimal::new).collect(Collectors.toList());
    
            sortedArray(n, bigList, list);
            scanner.close();
            list.forEach(System.out::println);
        }
    
        private static void sortedArray(int n, List<BigDecimal> bigList, List<String> list) {
            for(int i = 0; i< n -1; i++) {
                for(int j = 0; j< n -1; j++) {
                    if(bigList.get(j).compareTo(bigList.get(j+1)) <0 ) {
                        String temp = list.get(j);
                        BigDecimal temp1 = bigList.get(j);
                        list.set(j, list.get(j+1));
                        bigList.set(j, bigList.get(j+1));
                        list.set(j+1, temp);
                        bigList.set(j+1, temp1);
    
                    }
                }
    
            }
        }
    
        private static List<String> enterData(int n, Scanner scanner) {
            return IntStream.range(0, n)
                    .mapToObj(i -> scanner.nextLine())
                    .collect(Collectors.toList());
        }
    }
    
    
    
  • + 0 comments

    import java.math.BigDecimal; import java.util.*; class Solution{ public static void main(String []args){ //Input Scanner sc= new Scanner(System.in); int n=sc.nextInt(); String []s=new String[n+2]; for(int i=0;i

        //Write your code here
         try {
            BigDecimal A,B;
            int i = 0;
            String temp;
            while(i < n){
                for(int j = 0; j < n-1; j++){
                    A = new BigDecimal(s[j]);
                    B = new BigDecimal(s[j+1]);
                    if(A.compareTo(B) < 0){
                        temp = s[j];
                        s[j] = s[j+1];
                        s[j+1] = temp;
    

    // System.out.println(s[j]); } } i++; }

        }catch (NullPointerException  | NumberFormatException e){
            System.out.println(e);
        }
    
        //Output
        for(int i=0;i<n;i++)
        {
            System.out.println(s[i]);
        }
    }
    

    }

  • + 0 comments
       //Solution
        BigDecimal a,b;
        for(int i = 0;i<n;i++){
            for(int j=0;j<n-1-i;j++){
                a=new BigDecimal(s[j]);
                b=new BigDecimal(s[j+1]);
                if(a.compareTo(b)<0){    // algorithm of bubble sort
                    String temp = s[j];
                    s[j]=s[j+1];
                    s[j+1]=temp;
                }
            }
        }
    
  • + 0 comments

    Important hint, the print is already in the lower end of the code. Hope no one wastes so much time like me because I wasn't realizing how my list was duplicating

  • + 0 comments
    import java.util.*;
    class Solution{
    
        public static void main(String []args){
            //Input
            Scanner sc= new Scanner(System.in);
            int n=sc.nextInt();
            String []s=new String[n+2];
            for(int i=0;i<n;i++){
                s[i]=sc.next();
            }
              sc.close();
    
            //Write your code here
    
    for(int i=0;i<n;i++)
    {
        //inserting string values to bigdecimal
        BigDecimal First=new BigDecimal(s[i]);
        int index=i;
        for(int j=i+1;j<n;j++)
        {
            //second BigDecimal to compare the first Bigdecimal
            BigDecimal Second=new BigDecimal(s[j]);
    
            //comparing if First element is greater that second element
            //if the First element is greater than Second element than compareTo() returns 1
    
            if(Second.compareTo(First)==1){
                First=Second;
                index=j;
            }
        }
    
        //temporary variable to store s[i] value
    
            String temp=s[i];
            s[i]=s[index];
            s[index]=temp;
    }
          
            //Output
            for(int i=0;i<n;i++)
            {
                System.out.println(s[i]);
            }
        }
    
    }