# Help for java beginner

• October 10th, 2011, 11:40 AM
sarasaleem
Help for java beginner
import java.util.Scanner;
2
3 public class HexToDecimalConversion {
4 /** Main method */
5 public static void main(String[] args) {
6 // Create a Scanner
7 Scanner input = new Scanner(System.in);
8
9 // Prompt the user to enter a string
10 System.out.print("Enter a hex number: ");
11 String hex = input.nextLine();
12
13 System.out.println("The decimal value for hex number "
14 + hex + " is "+ );
15 }
16
17 public static int hexToDecimal(String hex) {
18 int decimalValue = 0;
19 for (int i = 0; i < hex.length(); i++) {
20 char hexChar = hex.charAt(i);
21 decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
22 }
23
24 return decimalValue;
25 }
26
27public static int hexCharToDecimal(char ch) {
28 if (ch >= 'A' && ch <= 'F')
29 return 10 + ch - 'A';
30 else // ch is '0', '1', ..., or '9'
31 return ch - '0';
32 }
33 }

the above code implements the hexToDecimal(String
hexString) method, which converts a hex string into a decimal number. Implement
the hexToDecimal method to throw a NumberFormatException if the
string is not a hex string.
• October 10th, 2011, 11:48 AM
KevinWorkman
Re: Help for java beginner
Please don't post multiple copies of the same question. I deleted your duplicate thread.
• October 10th, 2011, 11:50 AM
KevinWorkman
Re: Help for java beginner
Also, please see the link in my signature on asking questions the smart way before you post again. For example, what's your actual question? Where are you stuck? Simply dumping your homework assignment here is not going to get you very far.
• October 10th, 2011, 11:54 AM
copeg
Re: Help for java beginner
To add to the advice given by Kevin, here's some recommended reading: http://www.javaprogrammingforums.com...-get-help.html
• October 10th, 2011, 12:57 PM
sarasaleem
Re: Help for java beginner
Its not an homework assignment ,i started learning java and was stuck with a question that i am trying to do .So just asked for help.That's it
• October 10th, 2011, 12:59 PM
KevinWorkman
Re: Help for java beginner
Quote:

Originally Posted by sarasaleem
Its not an homework assignment ,i started learning java and was stuck with a question that i am trying to do .So just asked for help.That's it

Okay, that's fine, but that doesn't make it any easier to answer a question that has not been asked.
• October 10th, 2011, 07:56 PM
sarasaleem
Re: Help for java beginner
Anyways thanks for your advice i already solved it by myself .