Entering integers

Hi this is my first yr using java and I am so confice so I need some help.

I need to do a program where the user needs to input an integer of 3 digits but if the user enter less or greater then 3 digits the program needs to return an error message.

I need to do it with out using the command line for the output and input.

I hope some one can help me.

Thanks

[389 byte] By [Maria81a] at [2007-11-26 20:41:37]
# 1
Have you started it yourself?
CaptainMorgan08a at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 2
Not that part. I was thinking that it can be by using a counter in a loop and also an if statment but I dont know how to started.
Maria81a at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 3
Using paper and a pencil, write down the things that need to be done and the order that they need to be done in. Then start converting that to a program. When you have a problem. post back here. We will answer questions and help, but will not do the assignment for you.
ChuckBinga at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 4
The only way an error can be raised for not entering three numbers is if they are all entered on one line eg "1 2 3". If the user enters the numbers one at a time then you just keep prompting for more numbers until three have been entered then stop asking for input.
floundera at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 5

Use the scanner class for your input etc. then use a simple if then else statement which will display the error message etc.

if ((someNumber > 100) && (someNumber <= 999))

{

// Do some stuff

} else {

System.out.println("Error: the integer must be 3 digits long");

System.exit(1);

}

Message was edited by:

jazza_guy

jazza_guya at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 6
Is 000 valid input? If so, you can't require the int value to be in the range [100, 999].
DrLaszloJamfa at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 7
DOH!User must anter a number that is three digits long not three spereate numbers. In that case read the number in as a String and check its length.
floundera at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks,Tthats what I thoght but 000 is a valid input
Maria81a at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 9
000 is valid
mike_jonesa at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 10

//This program needs to ask the user to how may 3 digits integer wants

//to enter and then send an error message if is less then or greater then

//3 digits and output the integers in ascending order.

//So this is what I have so far.

import javax.swing.*;

public class NumbersUp

{

public static void main(String args[])

{

String threeInt, error, num;

int threeDigits, counter = 1, number, ascSeq;

threeInt = JOptionPane.showInputDialog("Welcome!\n How many three digits integers you want to enter? ");

threeDigits = Integer.parseInt(threeInt);

if(args.lenght!=threeDigits)

error = JOptionPane.showInputDialog("Error: Please re-enter.");

else

{

int arrayLength = Integer.parseInt(args[0]);

int array[] = new int [arrayLenght];

int initialValue = Integer.parseInt(args[1]);

int increment = Integer.parseInt(args[2]);

for(int i=0; i<=array.lenght; i++)

array=initialValue+increment*i;

}

while(counter <= threeDigits)

{

num = JOptionPane.showInputDialog("Enter a three digit integer" + counter + ": ");

number = Integer.parseInt(num);

if ((num < 100) && (num > 999))

{

while()

{

ascSeq

}

}

else

{

JOptionPane.showMessageDialog(null, "Error: The interger must be 3 digits. \nPlease try again", "Invalid integer", JOptionPane.ERROR_MESSAGE);

}

JTextArea outputArea = new JTextArea(9, 9);

JScrollPane scroller = new JScrollPane(outputArea);

String ascendingSeq = String.format("The integers that you enter are has follows in a ascending sequence $%.2f.", number);

outputArea.setText(ascendingSeq);

JOptionPane.showMessageDialog(null, scroller, "Integers", JOptionPane.PLAIN_MESSAGE);

counter++;

}

}

}

Message was edited by:

Maria81

Maria81a at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 11
yes 000 is valid
Maria81a at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 12
> So this is what I have so far.Do you have a question or do you just want us to admire your code?
floundera at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 13
No I dont want anybody to admire my code,The question that I have is that how I can do the code that the user can only enter 3 digits if is less or more output an error and 000 is validMessage was edited by: Maria81
Maria81a at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 14

As I said in reply #7, check the length of the String.

if(length of string is 3) {

convert to int;

// do other stuff

} else {

print error message;

}

floundera at 2007-7-10 2:00:14 > top of Java-index,Java Essentials,Java Programming...
# 15

I'm pretty confused, you used args[] and JOptionPane in your program as input. Which is the real input?

In case you don't know what args is, it's the String array that follows your program name when you run it in command line.

> java myClass.class hello all

args[0] is hello

args[1] is all

Edit: if you use an IDE to compile and run, I think you don't get a chance to input the args.

Message was edited by:

Icycool

Icycoola at 2007-7-21 18:00:54 > top of Java-index,Java Essentials,Java Programming...
# 16
Most IDE's will have an option somewhere to input args.P.S. I hope the .class was an oversight.
floundera at 2007-7-21 18:00:54 > top of Java-index,Java Essentials,Java Programming...
# 17
I'm using JOptionPane in my program as input and args[] because I am using an array to store the integers that the user enter. So I can use the array to print them out as an ascending order.Message was edited by: Maria81
Maria81a at 2007-7-21 18:00:54 > top of Java-index,Java Essentials,Java Programming...