P
P
pwnsauce2016-11-21 23:19:18
Java
pwnsauce, 2016-11-21 23:19:18

I'm trying to list the elements in the condition, how?

Hello, I'm just learning Java. And now I ran into a problem, the program reads a number in the hexadecimal number system from the keyboard, and if the user "accidentally" enters a letter that is not part of the hexadecimal number system, write to him that he made a mistake. So far my code is this:

import java.util.*;
public class 987 {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    System.out.print("Введи гексо число: ");
    String cislo = sc.next();
    cislo = cislo.toUpperCase();
    
    
    
    char[] kontrola = cislo.toCharArray();
    
    for(int i = 0; i<kontrola.length; i++){
            
      if(kontrola[i] == 'G'){  
                    
        kontrola[i] = '.';
      
            }	
    }
  }

}

And in that part with the condition, I wanted to list all the other letters from the alphabet that do not fit, but it doesn’t work like that, how can it be written differently?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2016-11-21
@aol-nnov

Integer res = 0;
try {
    res = Integer. parseInt(aString, 16);
} catch(NumberFormatException ex) {
    System.out.println("кисо, куку!")
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question