• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

another java question

dasoldier

Limp Gawd
Joined
Apr 29, 2005
Messages
185
I'm needing to create a program that will run through a series of math functions to determine whether a number is valid or not.

It starts out prompting the user to enter his/her card number


System.out.print( "Enter Card# " );
cardnumber = MyInput.readDouble( );
Very simple,

But after I get the cardnumber I need a way to tell how long the card number is... is it 10 digits long? 15 digits long?

and then can I build an array from card number that would seperate each of the digits?
so if the user enters 202021

private double [ ] newarray = {2,0,2,0,2,1};


So to restate I'm trying to count the number of digits in an int variable.
and I want to to build an array based on a variable.





:confused: maybe my brain will work better in the morning
 
not to hijack but I am having a similar issue.

I have to Write a program that inputs a word representing a binary number (0’s ans 1s). First, your program should check that it is indeed a binary number, that is, it contains only 0s and 1s. If the word does not represent a valid binary number, the program should keep prompting the user for a new word until a word representing a valid binary number is input by the user.

Once you have a valid binary number convert it to its decimal representation. For example, if the user inputs 10001, then the output will be 17.

My thought was too have the "binary word" be inputed as a string and then check each of those values to either 1 or 0. The only way i can think to do it this is to pull a number out of the string and compare it using equals().
 
Perhaps it would be useful for both of you to realize that a string is just a bunch of individual characters, and characters and ints are essentially equivalent via a 1-to-1 mapping. Furthermore, chars and ints are both native types in java so they can be compared using boolean equals (==) and not equals (!=). That is to say, (int)i == 0 is a boolean expression just like (char)c == '0'. They are not equivalent expressions however because character values do not map to equivalent integer values, but there is a 1:1 mapping if you're interested in that (this is what the string conversion methods, Integer.parseInt() and Integer.toString() use).

Hope that helps.
 
ameoba said:
ciel(log10(n)) = number of digits in n.

Why convert to an int/long/etc. in the first place? You're reading in from stdin as a string, why not just check the length of the string?
 
Yeah, read in as a string and go from there, i guess it automattically reads it in as a string but anyway, you really wont need to convert it to integers ( for the binary one ). Take a look at this page:

http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/lang/Integer.html

I could think of some uses for the length(), and toCarArray() functions. ;)

Gook Luck guys. :)

Edit: Well that link didnt go to the page it was supposed to. On the page linked above go to the bottom left frame. Then scroll down and click on the String class and look under Method Summary.
 
Back
Top