home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Java Concatenation

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2120
Discussions: 200,950, Posts: 2,379,418, Members: 246,311
Old March 19th, 2002, 12:36 AM   Digg it!   #1 (permalink)
Member
 
jgargac's Avatar
 
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
Talking
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
jgargac is offline   Reply With Quote
Old March 19th, 2002, 12:56 AM     #2 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old March 19th, 2002, 01:29 AM     #3 (permalink)
Member
 
jgargac's Avatar
 
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
jgargac is offline   Reply With Quote
Old March 19th, 2002, 01:42 AM     #4 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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
jkrohn is offline   Reply With Quote
Old March 19th, 2002, 01:30 PM     #5 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Old March 19th, 2002, 09:48 PM     #6 (permalink)
Member
 
jgargac's Avatar
 
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
jgargac is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (175)
Is It Just Me? (2940)
The disrespect of Obama by Russian .. (26)
Wireless Televisions. (12)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
Is the PSU I received dead? (12)
Print spooler problem (15)
radeon x850xt platinum & shader.. (5)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
windows vista security holes (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
Graphics Card Upgrade Question (4)
Happy Thanksgiving: At discount from .. (0)
Multiple Restarts Required at Boot (1)
For Sale BFG GTX285 OC2 with 10 year .. (3)
Point and Shoot Camera Suggestions. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Laptop with wireless problem. (2)
Internet Lost (1)
Hp Artist Edition + Matching Bag (0)
My monitor won't turn on after instal.. (0)
Asus P4G8X Mobo (6)
radeon x850xt platinum & shader 3 (5)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)


All times are GMT -4. The time now is 10:53 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28