-3

This program converts from hexadecimal to binary. This is my program:

public static void main(String[] args) throws IOException {
    int nh = 0, k = 0, j = 0, w = 0, z = 0, lun = 0, r;
    String line;
    String nb = null;
    char nhc = 0;
    BufferedReader tastiera = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Inserire il numero in esadecimale");
    line = tastiera.readLine();
    lun = line.length();
    System.out.println(line);
    if (lun > 1) {
        lun--;
    }
    for (z = lun; z > 0; z--)
        nhc = line.charAt(z);
    {
        if (nhc == 'a' || nhc == 'A') {
            nhc = 10;
        }
        if (nhc == 'b' || nhc == 'B') {
            nhc = 11;
        }
        if (nhc == 'c' || nhc == 'C') {
            nhc = 12;
        }
        if (nhc == 'd' || nhc == 'D') {
            nhc = 13;
        }
        if (nhc == 'e' || nhc == 'E') {
            nhc = 14;
        }
        if (nhc == 'f' || nhc == 'F') {
            nhc = 15;
        }
        for (k = nhc, w = 0; k > 0 && w < lun * 4; k = k / 2, w++) {
            nb.charAt(w) = k % 2;
        }

    }
    System.out.println("Il numero binario è " + nb);
}

This appear when I run:

Inserire il numero in esadecimale
12
12

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unexpected type required: variable found: value at esadecimale.binario.EsadecimaleBinario.main(EsadecimaleBinario.java:56) Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds)

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • run: Inserire il numero in esadecimale 12 12 Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unexpected type required: variable found: value at esadecimale.binario.EsadecimaleBinario.main(EsadecimaleBinario.java:56) Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds) – Dario Marchitelli Oct 25 '16 at 11:28
  • 2
    you can´t do `nb.charAt(w) = k % 2;`. – SomeJavaGuy Oct 25 '16 at 11:28
  • run: Inserire il numero in esadecimale 12 12 Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unexpected type required: variable found: value at esadecimale.binario.EsadecimaleBinario.main(EsadecimaleBinario.java:56) Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds) – Dario Marchitelli Oct 25 '16 at 11:29
  • kevin why?????????????? – Dario Marchitelli Oct 25 '16 at 11:29
  • 1
    because `nb.charAt(w)` doesn´t represent a variable. And as you do assign and don´t have a variable this results in invalid code. This could be answered by different Questions here. for Example, java is [pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), so in generall the idea you had wont work for it. Addtionally [Strings in java are immutable](http://stackoverflow.com/questions/1552301/immutability-of-strings-in-java), so you can´t just change the existing `String`, but need to reassign the one you want to change. – SomeJavaGuy Oct 25 '16 at 11:30
  • sorry,I'm new on java what I have to do to fix – Dario Marchitelli Oct 25 '16 at 11:31
  • nb.charAt(w) represent the position on the string – Dario Marchitelli Oct 25 '16 at 11:32
  • If you paste messages that refers to line number, please indicate which line contains error, or paste the whole class so that we can check it by ourselves. You could also try to compile first, then run. In such case you'll have a compilation result, that is better readable than RuntimeError. – Przemysław Różycki Oct 25 '16 at 11:34
  • I writed as // in the code – Dario Marchitelli Oct 25 '16 at 11:34
  • I can't see // in the code. Am I blind? – Przemysław Różycki Oct 25 '16 at 11:38

1 Answers1

0

You can't do this assignment: nb.charAt(w)= k%2; That's a value on the left hand side ( chartAt(index) returns a character ) and it can't be assigned to. That's what is happening at runtime...

Check this answer. Hope it makes sense.

Community
  • 1
  • 1