Java Concatenation  | |
March 19th, 2002, 12:36 AM
|
#1 (permalink)
| | Member
Join Date: Oct 2001 Location: St. Louis, MO
Posts: 463
| Java Concatenation
Hi all,
I need to be able to enter a five digit number into my java app and then split each digit apart so that I can compare them to see if they are equal.
Ex) Enter 52125 and then compare the first and last numbers and the second and fourth numbers for equality. I'm having trouble splitting the number apart. Here's what I have so far:
package ex424;
import javax.swing.*;
public class ex424 {
public static void main( String args[] )
{
String firstNumber;
int number,
digit1,
digit2,
digit3,
digit4,
digit5;
firstNumber =
JOptionPane.showInputDialog( "Enter number" );
number = Integer.parseInt( firstNumber );
digit1 = number % 10000;
digit2 = number % 10000 % 1000;
digit3 = number % 10000 % 1000 % 100;
digit4 = number % 10000 % 1000 % 100 % 10;
digit5 = number % 10000 % 1000 % 100 % 10 % 1;
JOptionPane.showMessageDialog( null,
"Results" + (digit1), "Results",
JOptionPane.INFORMATION_MESSAGE );
System.exit ( 0 );
}
}
When I run this though, digit1 = whatever I enter minus the first number. What I want is the first number minus the rest of the number. Does anyone know where I've gone wrong. Thanks,
Jeff |
| |
March 19th, 2002, 12:56 AM
|
#2 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
x % y gives the remainder 
so
digit 1 = 2125 52125 / 10000 = 5 r2125
digit 2 = 125
digit 3 = 25
digit 4 = 5
digit 5 = 0
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
March 19th, 2002, 01:29 AM
|
#3 (permalink)
| | Member
Join Date: Oct 2001 Location: St. Louis, MO
Posts: 463
|
Thanks Jkrohn,
That makes sense now. Do you know what code I would use to strip the digits out of the main number? I need to split the main number into 5 digits.
Ex) 12345 = 1 2 3 4 5
Thanks,
Jeff |
| |
March 19th, 2002, 01:42 AM
|
#4 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
digit1 = (number - (number % 10000)) / 10000
digit2 = ((number % 10000) - (number %10000%1000)) / 1000
digit3 = ((number % 1000) - (number %1000%100)) / 100
digit4 = ((number % 100) - (number %100%10)) / 10
digit5 = (number % 10)
52125 = number
digit1 = (52125 - 2125) / 10000 = 50000 / 10000 = 5
digit2 = (2125 - 125) / 1000 = 2000 / 1000 = 2
etc, etc
Another easier method is to to take it in as a string and cast it to an int piece by piece
Jkrohn |
| |
March 19th, 2002, 01:30 PM
|
#5 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
essentially you're looking to see if you have a palindrome.. correct?
anyway... i would cast it to a string, then use the method
toCharArray() [look in string class for documentation]
then you can just traverse the array forwards and back in a simple for loop to compare the values!
int i,j;
for (i=0,j=data.length();i=j;i++,j--)
...compare the boxes
...break if they aren't the same
something along those lines is what i think would work!
good luck
-Z |
| |
March 19th, 2002, 09:48 PM
|
#6 (permalink)
| | Member
Join Date: Oct 2001 Location: St. Louis, MO
Posts: 463
|
jkrohn,
that was the exact code that I needed. Thanks a million. Everything works out perfectly.
zskillz,
Yeah, I was checking to see if they were a palindrome.
Thanks guys for the help.
Jeff |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |