I think that my problem is quite easy to be solved by hard coders like you :-)

Its a conversion of binary number to decimal and hexadecimal.

I cannot find why i am keep having errors in my two classes.

Here is the code:

Main :

public class Times {

    public static void main(String[] args) {

        //Binary obj = new Binary(x1,x2);  
        /* Δεν χρειάζεται
         * Διότι έκανα static την συνάρτηση.
         */
         String x1 = Binary.varCheck();
         System.out.println("Τύπωσε την bin1: "+x1);                 
         String x2 = Binary.varCheck();
         System.out.println("Τύπωσε την bin2: "+x2);

        Binary obj = new Binary(x1,x2);



        System.out.println("\nΔυαδικός 1 : "+obj.get1()+"\n"
                + "Δυαδικός 2 : "+obj.get2());
        //Δεκαδική Μετατροπή
        System.out.println("\n *****Μετατροπή στο Δεκαδικό******* \n");
          System.out.print(" Το "+obj.get1()+"γίνεται: " +obj.getDec(obj.get1())+"\n");
          System.out.print("To "+obj.get2()+"γίνεται: "+obj.getDec(obj.get2())+"\n");               
        System.out.println("\n*****Μετατροπή στο Δεκαδικό******* \n");

        //Δεκαεξαδική Μετατροπή
        System.out.println("\n ********Μετατροπή στο Δεκαεξαδικό*****\n");
          System.out.print(" Το "+obj.get1()+"γίνεται: " +obj.getHex(obj.get1())  +"\n");
          System.out.print("To "+obj.get2()+"γίνεται: "+obj.getHex(x2)+"\n");       
        System.out.println("\n ********Μετατροπή στο Δεκαεξαδικό*****\n");

        //Υπολογισμός Αθροίσματος
        System.out.println("\n**********Υπολογισμός Αθροίσματος*************\n");
            System.out.println("\n Το Άθροισμα των δύο δυαδικών αριθμών είναι:\n "+obj.get1()+"\n+"+obj.get2()+
                "\n\n = "+obj.sumBin(obj.get1(),obj.get2()));       
        System.out.println("\n**********Υπολογισμός Αθροίσματος*************\n");


        //Δεξιά Ολίσθηση
        System.out.println("\n******Δεξιά Ολίσθηση******\n");
           System.out.println("Ο αριθμός: "+obj.get1() +" γίνεται: "+obj.rShift(obj.get1()) + "\n\nκαι ο αριθμός:"
                +obj.get2()+ " γίνεται: "+obj.rShift(obj.get2()) );


        System.out.println("\n******Δεξιά Ολίσθηση******\n");


       //Αριστερή Ολίσθηση
        System.out.println("\n******Αριστερή Ολίσθηση******\n");
           System.out.println("Ο αριθμός: "+obj.get1() +" γίνεται: "+obj.lShift(obj.get1()) + "\n\nκαι ο αριθμός:"
                +obj.get2()+ " γίνεται: "+obj.lShift(obj.get2()) );


        System.out.println("\n******Αριστερή Ολίσθηση******\n");








    }

}

Binary :

import java.util.Scanner;


public class Binary {

    private String bin1;
    private String bin2;



     //Κατασκευαστής 
    public Binary(String bin1, String bin2){
        this.bin1 = bin1;
        this.bin2 = bin2;
    }


    public String get1(){
        return bin1;
    }

    public String get2(){
        return bin2;
    }







    //Δέχεται δύο δυαδικούς αριθμούς κι επιστρέφει το άθροισμα αυτών!
    public String sumBin(String bin1, String bin2){

        int a = Integer.parseInt(bin1, 2);
        int b = Integer.parseInt(bin2, 2);
        int sum = a + b ;

        String sumStr = Integer.toString(sum,2);
        if(sumStr.length()>32){
            StringBuilder sb = new StringBuilder(sumStr);
            sb.deleteCharAt(0);

            sumStr = sb.toString();
        }

        return sumStr ;
    }

    //Τέλος της συνάρτησης μετατροπής^^^^^^^^^^^



    //Δεξιά ολίσθιση
    public String rShift(String bin){

        int x = getDec(bin);
        x = x/2;
        bin = Integer.toBinaryString(x);

        return bin;
    }


    //Τέσος συνάρτησης ^^^

    //Αριστερή ολίσθιση
    public String lShift(String bin){
        int x = getDec(bin);
        x= x*2;
        bin = Integer.toBinaryString(x);

        return bin;

    }

    //Τέλος συνάρτησης





    //Δέχεται δυαδικό κι επιστρέφει Δεκαεξαδικό!
    public String getHex(String bin){
        String hex = Integer.toHexString(Integer.parseInt(bin, 2));
        return hex;
    }
    //Τέλος της συνάρτησης μετατροπής^^^^^^^^^^^^^




    //Δέχεται Δυαδικό κι επιστρέφει στο Δεκαδικό!
    public int getDec(String bin){
        int dec = Integer.parseInt(bin,2);
        return dec;

    }
    //Τέλος της συνάρτησης μετατροπής^^^^^^^^^^^^^^^






    //Έλεγχος των τιμών αν είναι μέρα στα όρια των 32 μπιτ!
    //Την κάνω static γιατί δεν έχω δημιουργήσει το αντικείμενο ακόμη όταν την καλώ!
    public static String varCheck(){

        Scanner scan = new Scanner(System.in);
        String bin;
        System.out.print("\nΔώσε τον πρώτο δυαδικό αριθμό: ");
        do{
         bin = scan.nextLine();
        if(bin.length()>32)
            System.out.println("\nΔώσε ξανά δυαδικό αριθμό μικρότερο από 32 bits!\n");

        }while(bin.length()>32);

        scan.close();
        return bin;




    }
    //Τέλος του ελέγχου των τιμών!  ^^ 

}

I use Eclipse.

Thanks in advance :-)

Cheers!

Recommended Answers

it would be a lot easier if you mentioned what those errors are.
whether you use an IDE, and which one, for that matter, is completely irrelevant.

Jump to Post

looks like you didn't enter any text before pressing enter

Jump to Post

I don't really understand what's going wrong there, but if I try that single method's code, it works just fine.

Jump to Post

All 10 Replies

it would be a lot easier if you mentioned what those errors are.
whether you use an IDE, and which one, for that matter, is completely irrelevant.

Thank's for your immediate response .

The errors are the following:

Δώσε τον πρώτο δυαδικό αριθμό: 1010
Τύπωσε την bin1: 1010

Δώσε τον πρώτο δυαδικό αριθμό: Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Binary.varCheck(Binary.java:113)
    at Times.main(Times.java:14)

No such element exception ? :-/

looks like you didn't enter any text before pressing enter

yes that's because it don't allow me to do it.

I don't really understand what's going wrong there, but if I try that single method's code, it works just fine.

I don't want the single method.

Oh! I think i've found what's going on. The problem is that i close Scanner! An the second time iam accessing varCheck --> Scanner it has a conflict because it can no longer read from the same scan.

Maybe this is my problem ?

no. you only close the Scanner after you stopped using it.

I think i'm gonna start hitting my head on the wall.

Ι've managed to fix the problem by deleting the scan.close() but is this a good thing for a java application ?

There's no harm in not closing a Scanner on System.in The console is just a transient thing, unlike (say) a buffered disk file, so you aren't worried about losing any data when the application terminates.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.