# how to?

• November 15th, 2011, 03:31 PM
robingeldolf
how to?
i have to make sure the user presses a number between 0 and a 1000 , but i'm not sure how to do it.
For other cases i used while ( !keuze.equals("1") && !keuze.equals("2") && !keuze.equals("3") ); but that doesn't
work in this case,because i'm not using a string but a Long(and there to much numbers to equal.
If the number is not between 0 or 1000 i want the println"how many simulations do you want" to repeat.

System.out.println("how many simulations do you wanna do(maximaal 1000)");

for ( y = 0 ; y < aantalSimulaties ; y ++) {
algoritme();
tellenVanBalletjes();
Any ideas?
• November 15th, 2011, 04:14 PM
Junky
Re: how to?
Why not get user input as an int? Then you can use logical operators.
• November 15th, 2011, 04:20 PM
Junky
Re: how to?
By the way, in future use a more meaningful title to your threads. The title is what forumites use to see if they are interested in reading your post or not. So if you cannot grab their attention you will not get their help.
• November 16th, 2011, 05:26 AM
robingeldolf
Re: how to?
do you mean like this
do {
System.out.println("Hoeveel simulaties wil je uitvoeren?(maximaal 1000)");

for ( y = 0 ; y < aantalSimulaties ; y ++) {
algoritme();
tellenVanBalletjes();
}
}

while ( aantalSimulaties >= 0 && 1000 >= aantalsimulaties );
• November 16th, 2011, 05:52 AM
Mr.777
Re: how to?
And what is the problem in getting Long as input?
Be specific and get specific replies.
• November 16th, 2011, 06:02 AM
robingeldolf
Re: how to?
else if(keuze.equals("2")) {
keuzes2();
AantalSim();
keuzes2a();
}
public void keuzes 2() {
do {
System.out.println("Hoeveel simulaties wil je uitvoeren?(maximaal 1000)");
}
while ( aantalSimulaties >= 0 && 1000 >= aantalSimulaties );

}
public void AantalSim() {
long y;
for ( y = 0 ; y < aantalSimulaties ; y ++) {
algoritme();
tellenVanBalletjes();
}
}

When i pres a number that isn't between 0 and 1000 the do doesn't repeat?
• November 16th, 2011, 06:51 AM
Mr.777
Re: how to?
Quote:

When i pres a number that isn't between 0 and 1000 the do doesn't repeat?
Yeah coz you want while loop to repeat only if it's between 0 and 1000.
Is it a problem? "Sighs"
• November 18th, 2011, 09:21 AM
robingeldolf
Re: how to?
I've changed it to tis but it still won't work.
public void keuzes2() {

do {
System.out.println("how many simulations do you ant to do(maximaal 1000)");
}
while ( aantalSimulaties < 0 && 1000 < aantalSimulaties );
• November 18th, 2011, 12:09 PM
Norm
Re: how to?
Quote:

while ( aantalSimulaties < 0 && 1000 < aantalSimulaties );
What value is both < 0 and > 1000?
Would you want to loop back if either is true vs both being true?
• November 18th, 2011, 01:55 PM
robingeldolf
Re: how to?
if they press -1 or -12 is has to loop and if they press 1010 or 1020 it has the loop ,but you can only give one number at a time.

So better use the OR? Thanks for the help,my problem is solved. Should have found it myself.
• November 18th, 2011, 02:00 PM
Norm
Re: how to?
Quote:

you can only give one number at a time.
That's right. You can use the AND test to see if a number is in a range. A number can be both > 10 and < 20.
There is no number that is both < 0 and > 1000.
You want to repeat the loop if the entered number is either < 0 OR > 1000
• November 18th, 2011, 02:40 PM
robingeldolf
Re: how to?
One more problem now, the loop has to repeat if the number isn't between 0 and 1000, but if the use enters a letter is has to loop aswell. How to do this without including the intire alphabet to the while with OR's
• November 18th, 2011, 02:42 PM
Norm
Re: how to?
How are you reading the input from the user? What datatype does the readInt() method return?
What happens if the user does not enter a valid number?
• November 18th, 2011, 05:10 PM
robingeldolf
Re: how to?
do {
System.out.println("Hoeveel simulaties wil je uitvoeren?(minimaal 0,maximaal 1000)");
}
while ( aantalSimulaties <= 0 || 1000 <= aantalSimulaties );

errors
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Bordvangalton3.keuzes2(Bordvangalton3.java:88)
at Bordvangalton3.keuzes2c(Bordvangalton3.java:123)
at Bordvangalton3.keuzes2a(Bordvangalton3.java:109)
at Bordvangalton3.keuzes2c(Bordvangalton3.java:125)
at Bordvangalton3.keuzes2a(Bordvangalton3.java:109)
at Bordvangalton3.keuzes2c(Bordvangalton3.java:125)
at Bordvangalton3.keuzes2a(Bordvangalton3.java:109)
at Bordvangalton3.keuzes(Bordvangalton3.java:57)
at Bordvangalton3.keuzesWeergeven(Bordvangalton3.java :20)
at Bordvangalton3.main(Bordvangalton3.java:222)
• November 18th, 2011, 05:48 PM
Norm
Re: how to?
Quote:

java.util.InputMismatchException
That's what happens when the user enters the wrong data.
You can handle that exception by using try{<your code here>}catch
• November 18th, 2011, 06:00 PM
robingeldolf
Re: how to?
isn't there a way to repeat the loop so it keeps asking the amount of simulations that is required,the same way i did with the do while.

while ( aantalSimulaties <= 0 || 1000 <= aantalSimulaties || aantalSimulaties ==a || aantalSimulaties ==b ....... );

Because we haven't seen the try catch method and are not allowed to use it.
• November 18th, 2011, 06:33 PM
Norm
Re: how to?
What is the datatype of the variable that contains the data that you must test? Is it a number like an int or is it a String?
That will determine what kind of conditional tests you can do in the while loop.
If you use the readInt() method then I assume that the datatype is int. If the user enters non-numeric data like "A" then the readInt() method throws an exception like in post#14.
I don't know what you have learned and what you are permitted to use for this program.
• November 18th, 2011, 07:00 PM
robingeldolf
Re: how to?
i'm using the int type, i have to make sure any enter the users gives, not between 1 or 1000 gives the question again.
Is it a better way to use a string?And how do you compare al the numbers between 1 and 1000 with the string?
while ( aantalSimulaties <= 0 || 1000 <= !aantalSimulaties );
• November 18th, 2011, 07:12 PM
Norm
Re: how to?
You have a problem with the input. You can input numbers with readInt() or you can input Strings with another method.
But then you have to test that the String is a valid number before you can convert it to a number. You won't be able to do a numeric test with a String.
I don't know if you know how to do test for a valid numeric String yet.
Do you know how to convert a numeric String to an int?
• November 19th, 2011, 05:05 AM
robingeldolf
Re: how to?
int i = Integer.parseInt(s);??
• November 19th, 2011, 05:10 AM
Mr.777
Re: how to?
Quote:

Originally Posted by robingeldolf
int i = Integer.parseInt(s);??

Did you try this?
• November 19th, 2011, 05:35 AM
robingeldolf
Re: how to?
But i don't know what to do if i converted the string
• November 19th, 2011, 06:09 AM
Norm
Re: how to?
Quote:

i don't know what to do if i converted the string
You must have a use for the data in the String or you would not have asked the user to enter it.
Please explain what the data in the String is used for? Why does your program get that data?
• November 19th, 2011, 07:59 AM
robingeldolf
Re: how to?
the data in the string is used to create an amount of simulations. If the string is 100 the simulation has to run 100 times. But i want to make sure the users gives a number that is between 0 and 1000, so no letters to.
• November 19th, 2011, 08:42 AM
Norm
Re: how to?
If the user enters a String, what methods are you allowed to use to determine if that String is all numeric digits or if it contains letters?
One way is to use try{} catch block with an Integer class parse method that throws an exception if the String is not a valid number.
Once you have converted the String to an int then you can test if its value is in the desired range.