Enter Java Decimal -> Output Hex

Asked 2 years ago, Updated 2 years ago, 91 views

You need to use an array to print out a decimal number as a hexadecimal number There is an error when you input the value with scanner and print it out in the if statement. I'd appreciate it if you watched it.

==================================

    private static int count;

    private static Object intput;



    public static void main(String[] args) {

        // // TODO Auto-generated method stub

         Scanner scanner = new Scanner(System.in);


            char[] hex_out = new char[4];



            int remainder = 0;


            System.out.println ("Enter an integer value between 1 and 20000");



            int x = scanner.nextInt();

                    for (int i=0; i<4; i++) {

                           remainder = x%16;
                           x=x/16;


                 if (remainder>9) {
                   hex_out[3-i] = (char)(remainder-10+'A'); 

                                   System.out.println("" + hex_out[str.charAt(i)]);  
                                   //It works well until you enter a value with scan, but there is an error here. 

                    }

                                   else if (remainder<10) {
                                hex_out[i] = (char)(remainder-0); 

                            System.out.println("" + hex_out[str.charAt(i)]);
                        }   
          }
    }
}

java array

2022-09-22 19:28

2 Answers

System.out.println("" + hex_out[str.charAt(i)]);  
//It works well until you enter a value with scan, but there is an error here. 

In str.charAt(i), the variable str is not declared anywhere in the code.

But there will be a compilation error because there is no variable, but it is strange that the value is entered as scan~


2022-09-22 19:28

Oh, str is String str = " korea";

I declared it above, but the previous output is different

I erased the code, uploaded it, and erased it

Please enter a value It's a situation where there's an error if you type it up


2022-09-22 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.