Java/ApComputerSci., User input programs |
![]() ![]() |
Java/ApComputerSci., User input programs |
![]()
Post
#1
|
|
![]() HOY!!!!! ![]() ![]() ![]() ![]() Group: Member Posts: 268 Joined: Oct 2004 Member No: 55,098 ![]() |
{OK i'm pretty much new to Java and the computer language thing, but I do understand the fundamentals.}
Due:Wednesday, 21,2005 Problem: Write an application program that inputs one number consisting of four digits from the user; separates the number into its individual digits and prints the separated digits from one another. For example, if the user types in the number 4239, the program should print 4 2 3 9.(hint:Use Mod(%) and Division(/). Ok this is what I got so far: import javax.swing.JOptionPane; public class Separate { public static void main(String[] args) { String number; int numberOne; double mod; double division; number=JOptionPane.showInputDialog("Enter 4 numbers"); numberOne= Integer.parseInt(number); System.exit(0); } } (I tried putting a code box but it screwed up the code.) Its just the foundation. I cant figure out the rest to separate the numbers. Thanks you all for your help. |
|
|
*mipadi* |
![]()
Post
#2
|
Guest ![]() |
Okay, well, I didn't feel like building an interface to test this, but I built it as a command-line applet. Here's what I did:
CODE public class SplitNum { public static void main(String[] args) { TextReader keyboard = new TextReader(); //this is just a class i built for easily getting keyboard input System.out.print("Enter number: "); int num = keyboard.readInt(); String num_string = new String(); num_string = num_string.valueOf(num); //read number as a string int i = 0; for (i = 0; i < num_string.length(); i++) { System.out.print(num_string.charAt(i) + " "); //output specific character of string } System.out.println(); } } Here's what the output looks like: CODE athena{~/csci203/practice}% java SplitNum Enter number: 4329 4 3 2 9 It shouldn't be too hard to convert it into a Swing applet. It doesn't use division or mod, but hey, it works fine. |
|
|
![]()
Post
#3
|
|
![]() HOY!!!!! ![]() ![]() ![]() ![]() Group: Member Posts: 268 Joined: Oct 2004 Member No: 55,098 ![]() |
hmm. thats nice. thank you.but there is no other way than that? If there is, I want something that strictly uses Division and Mod (at least that what my teacher says).
But other than that, you must be good at these stuff! I'm so jealous ![]() |
|
|
*mipadi* |
![]()
Post
#4
|
Guest ![]() |
Okay. Well, here's a rough algorithm you could work with:
CODE 4239 1. Divide by 1000 .4239 2. Multiply by 10 4.239 3. Truncate 4 4. Multiply old value by 1000 239 5. Divide by 100 2.39 6. Truncate 2 7. Multiply old value by 100 39 8. Divide by 10 3.9 9. Truncate 3 10. Multiply old value by 10 9 Edit Okay...here's a program I whipped up really quickly that should work: CODE public class SplitNum2 { public static void main(String[] args) { TextReader keyboard = new TextReader(); System.out.print("Enter number: "); int num = keyboard.readInt(); double tmp; int thousands, hundreds, tens, ones; tmp = num / 1000.0; tmp = tmp * 10.0; thousands = (int) tmp; tmp = tmp * 1000.0; tmp = tmp / 100.0; hundreds = (int) tmp; tmp = tmp * 100.0; tmp = tmp / 10.0; tens = (int) tmp; tmp = tmp * 10.0; ones = (int) tmp; System.out.println(thousands + " " + hundreds + " " + tens + " " + ones); } } It's probably not necessary to copy num into tmp, but I didn't know if I'd want to use num later (I ended up not using it) so I wanted to retain a copy of the original number. Also, again, this is a command-line applet, but you can just convert it for use in something with a Swing interface. The algorithm is noted above, and the implementation here; if you have any additional questions about how the program functions, feel free to ask. Also, the program isn't optimized; for instance, in the algorithm, lines 4 and 5 call for multiplying by 1000 and dividing by 100; clearly they could be combined into one line and just multiply the old value by 10. Doing this could probably allow you to put everything into a loop, so it could handle numbers bigger or smaller than 4 digits (but since you know you are inputting a 4 digit number, this application will work in this instance). Again, if you really want to impress your teacher, you should probably go through and optimize the code I demonstrated. ![]() This post has been edited by mipadi: Sep 15 2005, 10:23 PM |
|
|
![]()
Post
#5
|
|
![]() HOY!!!!! ![]() ![]() ![]() ![]() Group: Member Posts: 268 Joined: Oct 2004 Member No: 55,098 ![]() |
lol. thanks -ALOT. i pretty much get it now.
EDIT u know that 4239/1000 is not ".4239" its "4.239. EDIT(again) OK some can close this now. I figured it out! yay! But it was mostly from your help. Thanks though. This is what I got. CODE import javax.swing.JOptionPane; public class Split { public static void main(String[] args) { String number; double numberOne; int thousands; double numberTwo; double numberThree; int hundreds; double numberFour; double numberFive; int tens; double numberSix; double numberSeven; int ones; double numberEight; double numberNine; number=JOptionPane.showInputDialog("Enter a four digit number"); numberOne= Integer.parseInt(number); numberTwo=numberOne%10000.0; numberThree=numberTwo/1000.0; thousands=(int)numberThree; numberFour=numberOne%1000.0; numberFive=numberFour/100.0; hundreds=(int)numberFive; numberSix=numberOne%100.0; numberSeven=numberSix/10.0; tens=(int)numberSeven; numberEight=numberOne%10.0; numberNine=numberEight/1.0; ones=(int)numberNine; System.out.println("Here you go!\n"+thousands+" "+hundreds+" "+tens+" "+ones); System.exit(0); } } so yea someone can close this now. ![]() |
|
|
*mzkandi* |
![]()
Post
#6
|
Guest ![]() |
^ Ok.
Topic Closed. |
|
|
![]() ![]() |