0

I have to use different methods for this code, no java shortcuts! Here is my code:

import java.io.*; 

import java.util.Scanner; 

public class pg3a { 

public static void main(String[] args) throws IOException { 

   Scanner keyboard = new Scanner(System.in); 

   String hex; 
   char choice = 'y'; 
   boolean isValid = false; 
   do { 
      switch (choice) { 
   case 'y': 
      System.out.print("Do you want to enter a hexadecimal number? "); 
      System.out.print("y or n?: "); 
      choice = keyboard.next().charAt(0); 

      System.out.print("Enter a hexadecimal number: #"); 
      hex = keyboard.next(); 
      hex = hex.toUpperCase(); 
      int hexLength = hex.length(); 
      isValid = valid(hex); 
        if (isValid) { 
            System.out.println(hex + " is valid and equal to" + convert(hex)); 
        } 
        else { 
           System.out.println(hex + " is invalid."); 
       } 
     case 'n': 
       System.out.println("quit"); 
      } 
      }while (choice != 'n'); 
} 

public static boolean valid (String validString) { 

  int a = 0; 
  if (validString.charAt(0) == '-') { 
  a = 1; 
} 
 for (int i=a; i< validString.length(); i++) { 
    if (!((validString.charAt(i) >= 'a' && validString.charAt(i) <= 'f')|| (validString.charAt(i) >= 0 && validString.charAt(i) <= 9))) 
{ 
return false; 
} 
} 
return true; 
} 

How can I make it so that after the program checks all the parameters for the hexadecimal number and calculates what it should be in decimal form, it prints out that the hexadecimal number is valid and then what the decimal number is??

Also how can I make it a loop that ends with either ^z or ^d to end the program?

  • 1
    You must have had a very sheltered life. – Scary Wombat Oct 02 '13 at 05:40
  • 5
    Im sure this wont be the last of "I've never had so much trouble in my entire life" situation, but your problem is fairly simple, to parse the content of the HEx String http://stackoverflow.com/questions/11194513/convert-hex-string-to-int, and printing is straight forward. – Anantha Sharma Oct 02 '13 at 05:41
  • this is a place where people help any kind of people who are in trouble. So it is no need to use "PLEASE PLEASE PLEASE" kind of words again and again. Resource person will answer you even without those words. Try to be more professional. – Malintha Oct 02 '13 at 05:52

1 Answers1

0

To convert Strings representing hexadecimal numbers to Integer, you can use the Integer.toString(String, int); method:

Integer parsedValue = Integer.parseInt(hex, 16);

The first argument is the string to be converted, the second is the radix specification, hence is this value 16 for now.

To be complete, the Integer.toString(Integer, int) is the reverse if the above: it converts an Integer value to a string in the specified radix.

Just create a method named convert, and make it return this.

Printing an Integer is not a big issue, you can just concatenate it to any String using the + operator.

System.out.println("The value: " + parsedValue);

Also, keep in mind, that you have a little problem:

This line makes all the charachters uppercase in your string:

hex = hex.toUpperCase(); 

But here you check for lowercase letters:

if (!((validString.charAt(i) >= 'a' && validString.charAt(i) <= 'f')|| (validString.charAt(i) >= 0 && validString.charAt(i) <= 9))) 

Either do hex=hex.toLowerCase();, or adjust the above condition to check to be between 'A' and 'F'.

Have to mention though that checking the validity of a String ot be converted to a numeric value is different: it tinvolves a try-catch block: try to convert the number, and if it fails, it is not valid...

Integer value; //have to declare it here to be able to access it outside of the try block
try {
   value = Integer.parseInt(hex,16);  

} catch(NumberFormatException e) {
   //if you want to get the stack trace
   e.printStackTrace(); //if not using a proper logging framework!!! Don't just print it!
   //handle the situation: e.g. break loop, write eror message, offer retry for user, etc...
}
ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • OP said he can't use libraries or API to parse hex (he called them "shortcuts"), because this is a CS assignment. Most of your answer is not applicable – Bohemian Oct 02 '13 at 06:42
  • @Bohemian Hmm, that is true. That requirement is out of this world. I'd understand it if it were for C, or ASM, but for Java - come on... It would make a lot more sense to teach poor guys how to use the provided APIs and not have to reinvent the wheel over and over again. Especislly that OP is an arts major... – ppeterka Oct 02 '13 at 06:44
  • Actually I completely agree with you: Exercises should be realistic and use realistic solutions (java API). I think learning the language *means* learning the API, but I could understand a few assignments using "raw" java to get a grounding in the language. But only a very few. – Bohemian Oct 02 '13 at 07:05