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. |
|
|
![]() ![]() |