Hi,
Here is the code - The program does not provide any message to the user when no argument is pass
[code=java]
public class Hexnumbers{
public static int conv1(String s)
throws NumberFormatExc eption {
String digits = "0123456789ABCD EF";
s = s.toUpperCase() ;
int val = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int d = digits.indexOf( c);
val += d*Math.pow(16, s.length()-1-i);
}
return val;
}
public static String conv2(int d)
throws NumberFormatExc eption
{
String digits = "0123456789ABCD EF";
if (d == 0) return "0";
String hex = "";
while (d > 0) {
int digit = d % 16;
hex = digits.charAt(d igit) + hex;
d = d / 16;
}
return hex;
}
public static void main(String[] args) {
try{
int sum = 0;
for (int i = 0; i < args.length; i++) {
System.out.prin tln(args[i]);
int decimal = conv1(args[i]);
System.out.prin tln("Here is the decimal number = " + decimal);
sum += decimal;
String hex = conv2( decimal);
System.out.prin tln("Here is the Hex number = " + hex);
System.out.prin tln("Here is the sum of **Hex** + **Decimal** == " + sum);
}
}catch(NumberFo rmatException nfe) {
System.out.prin tln ("Please Enter a valid argument");
}
}
}
[/code]
Here is the code - The program does not provide any message to the user when no argument is pass
[code=java]
public class Hexnumbers{
public static int conv1(String s)
throws NumberFormatExc eption {
String digits = "0123456789ABCD EF";
s = s.toUpperCase() ;
int val = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int d = digits.indexOf( c);
val += d*Math.pow(16, s.length()-1-i);
}
return val;
}
public static String conv2(int d)
throws NumberFormatExc eption
{
String digits = "0123456789ABCD EF";
if (d == 0) return "0";
String hex = "";
while (d > 0) {
int digit = d % 16;
hex = digits.charAt(d igit) + hex;
d = d / 16;
}
return hex;
}
public static void main(String[] args) {
try{
int sum = 0;
for (int i = 0; i < args.length; i++) {
System.out.prin tln(args[i]);
int decimal = conv1(args[i]);
System.out.prin tln("Here is the decimal number = " + decimal);
sum += decimal;
String hex = conv2( decimal);
System.out.prin tln("Here is the Hex number = " + hex);
System.out.prin tln("Here is the sum of **Hex** + **Decimal** == " + sum);
}
}catch(NumberFo rmatException nfe) {
System.out.prin tln ("Please Enter a valid argument");
}
}
}
[/code]
Comment