-1

We received an exercise at college about unicode and ASCII and chars and so on. One part of the exercise is to find a way to read the users input in console that have to be a char and give back the ASCII and Unicode.

Quite easy I think, but my scanner won't scan. Because there's no .nextChar method for the scanner. I was used to use the String method (scanner.nextLine). In the next step, I turned the singlechar-String to a char with char ... = s.charAt and from there I wanted to get the hex and ASCII. Whatever I do my Scanner wont scan the String. Here's the code:

do{

    System.out.println("Geben Sie ein Zeichen ein um den dazugehoerigen ASCII- und Unicode anzuzeigen");
    input = scan.nextLine();

} while(input.length() >= 2 & input.length() < 1);

zeichen = input.charAt(0);

i = (int) zeichen;

System.out.println("Sie haben " + zeichen + " gewählt.\nDer dazugehoerige ASCII-Code ist " + i + "\nDer dazugehoerige Unicode ist " + (buffer.toString()+ Integer.toHexString(i)));
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
  • Let me help you helping us click [here](https://stackoverflow.com/posts/44383573/edit) and edit your post so we can understand what have you done so far! – ΦXocę 웃 Пepeúpa ツ Jun 06 '17 at 07:15
  • And spent some time at the help center to learn **how** to properly format/indent your code. That preview pane and markdown language explanations around the edit window exist for a reason. And read [mcve] to then improve your question. dramatically. – GhostCat Jun 06 '17 at 07:16
  • have a check https://stackoverflow.com/questions/5032356/using-scanner-nextline – yılmaz Jun 06 '17 at 07:21
  • Try using buffered reader. It has much flexibility – Binu Jun 06 '17 at 07:24

4 Answers4

0

your condition here is wrong:

(input.length() >= 2) & (input.length() < 1)

and boolean condition can be done as && and not &

on the other hand there is no string ever whose length can be bigger than 2 and less than 1 at the same time! so that condition will never meet!

ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83
0

The condition is wrong, because it can never be true. Maybe you should use this:

input.length() >= 2 || input.length() < 1

Moreover, is your scanner reading from keyboard? This is how it should looks:

Scanner scan = new Scanner(System.in);

Tried this code and it works well:

String input = null;
Scanner scan = new Scanner(System.in);
do{

    System.out.println("Geben Sie ein Zeichen ein um den dazugehoerigen ASCII- und Unicode anzuzeigen");
    input = scan.nextLine();

}while(input.length() >= 2 || input.length() < 1);

    char zeichen = input.charAt(0);

    int i = (int) zeichen;
    System.out.println("Sie haben " + zeichen + " gewählt.\nDer dazugehoerige ASCII-Code ist " + i + "\nDer dazugehoerige Unicode ist " + (buffer.toString()+ Integer.toHexString(i)));
Christian Ascone
  • 917
  • 6
  • 14
0

input.length() can not >= 2 and < 1 at the same time. Check your condition.

Duong Nhat
  • 79
  • 9
0

I agree with all the previous comments. You had to add further code, so we could have checked your Scanner (is it initialized with System.in ?) and further things.

Do you only have to regard single-char inputs? Otherwise, a for-loop, where every char in the string is changed to ASCII and UNICODE would make more sense to me; if you have only single-char inputs I would recommend an error statement or an exeption so the user knows about his input mistake!

And finally, if you name your variables in German (which is not bad), do it with all of them, cause otherwise one gets confused a little! I do this mistake myself and sometimes loose track of my variables!

Here is one example of the for-loop I talked about:

    String input = "";
    Scanner scan = new Scanner(System.in);
    char zeichen = ' ';

    System.out.println("Geben Sie ein Zeichen ein");
    input = scan.nextLine();

    for(int a=0; a<input.length();a++)
    {
        zeichen = input.charAt(a);
        int i = (int) zeichen;
        System.out.println("Sie haben " + zeichen + " Ascii: " + i + " Unicode: "); //this line has been slightly simplified
    }
S.M.
  • 29
  • 5