Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: how to?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)");
    number of simulations = Input.readLong();


    for ( y = 0 ; y < aantalSimulaties ; y ++) {
    algoritme();
    tellenVanBalletjes();
    Any ideas?

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: how to?

    Why not get user input as an int? Then you can use logical operators.
    Improving the world one idiot at a time!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to?

    do you mean like this
    do {
    System.out.println("Hoeveel simulaties wil je uitvoeren?(maximaal 1000)");
    aantalSimulaties = Input.readInt();

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

    while ( aantalSimulaties >= 0 && 1000 >= aantalsimulaties );

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: how to?

    And what is the problem in getting Long as input?
    Be specific and get specific replies.

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)");
    aantalSimulaties = Input.readInt();
    }
    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?

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: how to?

    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"

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)");
    amountofsim = Input.readInt();
    }
    while ( aantalSimulaties < 0 && 1000 < aantalSimulaties );

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to?

    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?

  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by robingeldolf; November 18th, 2011 at 03:01 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to?

    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

  12. #12
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  14. #14
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to?

    do {
    System.out.println("Hoeveel simulaties wil je uitvoeren?(minimaal 0,maximaal 1000)");
    aantalSimulaties = Input.readInt();
    }
    while ( aantalSimulaties <= 0 || 1000 <= aantalSimulaties );

    errors
    Exception in thread "main" java.util.InputMismatchException
    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 Input.readInt(Input.java:104)
    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)

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to?

    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

  16. #16
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  18. #18
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?
    aantalsimulaties=inputreadstring
    while ( aantalSimulaties <= 0 || 1000 <= !aantalSimulaties );

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  20. #20
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to?

    int i = Integer.parseInt(s);??

  21. #21
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: how to?

    Quote Originally Posted by robingeldolf View Post
    int i = Integer.parseInt(s);??
    Did you try this?
    Don't ask us but test it in your application and if got any errors, ask here.

  22. #22
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to?

    But i don't know what to do if i converted the string

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to?

    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?

  24. #24
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.