Regards, Rene Larsen
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Regards, Rene Larsen
Rene Larsen wrote:You could use e.g. int newC = Integer.parseInt(c);
Regards, Rene Larsen
David Newton wrote:Please, please try to indent your code in a meaningful, consistent manner--without proper indentation it's quite difficult to understand.
Also note that you can print the hexadecimal representation of a number a bit more concisely than in the above code.
David Newton wrote:This line makes no sense:Or rather it does, but is almost certainly not what you mean.
Regards, Rene Larsen
Rene Larsen wrote:The first error you need to fix, is this line
here you'll get a NumberFormatException if you enter a letter.
You need to tell parseInt that it is a hex (HINT: base 16)
PS: I don't get an error, with the first if.
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:because you are using the Integer.parseInt(String s) method. This clearly states:
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value
Perhaps there is another method in the Integer class you could use...or an overloaded one...
David Newton wrote:Why "should" it work? Have you traced out the program's execution with paper and pen[cil]?
David Newton wrote:Have you traced out the program's execution with paper and pen[cil]?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:I took your code as posted above, changed it to 'public' so it could run, and ran it.
When I gave it an input of 'b', I got nothing (which is what i expected).
When i gave it an input of 'a', i got '9' (which, again, is what i expected).
I would suggest you put a ton of 'System.out.println()' statements in there, and run it again, to see what it is really doing.
Note that you're line 19 is meaningless. c will ALWAYS equal c.
Regards, Rene Larsen
import java.io.*;
class Exxx3
{
public static void main(String[] args)
{
Console console=System.console();
System.out.println("please type a hexadecimal number");
String input;
input=console.readLine();
int count=1;
int count2=0;
String hex = "0123456789abcdef";
while(input.charAt(0)!=hex.charAt(count))
{count2=count+1;
count++;
}
System.out.println("your decimal number is"+" "+count2);
}
}
Dmitri Makovetskiy wrote:i mean, i cant compare a character with a number. cause if i could i would do it.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:
Dmitri Makovetskiy wrote:i mean, i cant compare a character with a number. cause if i could i would do it.
Why can't you?
C:\slop>java Exx2
Yup
Nope
Dmitri Makovetskiy wrote:
fred rosenberger wrote:
Dmitri Makovetskiy wrote:i mean, i cant compare a character with a number. cause if i could i would do it.
Why can't you?
C:\slop>java Exx2
Yup
Nope
Fred, i finished my 2 tasks. i posted them above yoru message.
could you help me finding some method that will prevent the user typing something that isnt allowed.. like a character...dkaldaalsda or number 20000000
Regards, Rene Larsen
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:before we can do that, you have to tell us what is and is not allowed. 20000000 seems like a perfectly valid number to me, hex or decimal.
did you look at the API for the Integer class I linked to above? Did you see ANYTHING that might help you? Between me and Rene, it should be pretty obvious.
Go look there, and tell us what you find.
Dmitri Makovetskiy wrote:i dont remember if i looked. i think i did.
Rene Larsen wrote:Is it in your would only allowed to enter one hex digit/letter??
The value 2000 is a valid hex....
Do you read what has been posted in this thread??
Dmitri Makovetskiy wrote:
Rene Larsen wrote:
Do you read what has been posted in this thread??
okay, it doesnt matter..
Regards, Rene Larsen
Rene Larsen wrote:Have you tried the code I posted??
This should give you an idea, so you can do what you want.
Dmitri Makovetskiy wrote:
Rene Larsen wrote:Have you tried the code I posted??
This should give you an idea, so you can do what you want.
i am in a direction of this code:
i want to convert all those while loops to for. this would allow me to use if and else.
i must have if and else's.
The structure should be like that.
if
the letter appears in the string
print it
if it doesnt
ask for another number.
so far the code isnt working and tells me there is a mistake>
to tell you the truth. i dont think my code will recognize 2000 as a value... i think i need to redesign the whole thing, with different loops. what do you think?
Regards, Rene Larsen
Regards, Rene Larsen
Rene Larsen wrote:The code I posted was this one
Now, I don't know WHAT it is you're supposed to learn, but the code above detect legal - and illegal hex numbers.
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|