0

I need to create an array of random numbers using user inputted parameters(size, highest random number, lowest random number) and also include a boolean that dictates weather the highest number will be included in the random numbers or not. The boolean is whats causing me runtime errors. I cant figure out whats going wrong. i've tried if statements as well as switch statements but i dont think the problem is there. If you guys could help me id really appreciate it. I come here for many questions but this is my first time posting so sorry if my etiquette is wrong. here is my code thus far:

import  java.util.*;

public class Question1 {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    boolean yesNo = false;
    String yesOrNo;
    int highest;

    System.out.print("Please enter array size: ");
    int size = scan.nextInt();

    System.out.print("Please enter highest number: ");
    int high = scan.nextInt();

    System.out.print("Please enter lowest number: ");
    int low = scan.nextInt();

    System.out.print("Would you like to include " + high +" into the list of random numbers(Yes or No)?");
    yesOrNo = scan.nextLine();

    switch (yesOrNo.toUpperCase()) {
    case "YES":
        yesNo = true;
        break;
    }
    if (yesNo==true)
        high=high+1;

    randomNumberGenerator(size,high,low);
    }


public static void randomNumberGenerator(int size, int high, int low) {
    int[] randomNums = new int [size];
    for(int count = 0; count<randomNums.length; count++) {
        Random generator = new Random();
        int num = generator.nextInt(high) + (low);
        randomNums[count] = num;
    }
    System.out.print(Arrays.toString(randomNums));
 }  
 }

Please enter array size: 10
Please enter highest number: 5
Please enter lowest number: 0
Would you like to incloud 5 into the list of random numbers(Yes or No)?[0, 0, 2, 1, 2, 4, 3, 3, 0, 2]
-----------------------------------------------------------

it seems like the array is skipping over the boolean section of my code and im not sure why.
Tony Ruiz
  • 1
  • 1
  • What's runtime error? – Tix Feb 18 '20 at 00:02
  • i think a runtime error is an error that occurs when i run the program. That sounds like a wise ass response but thats the best way i can explain it. I'm using eclipes IDE and it catches any compile errors i make but it cant catch runtime errors. everything in the code is supposed to work but something is going wrong. the program is ignoring the boolean part of the program. It doesn't wait for the yes or no response it just automatically spits out the array – Tony Ruiz Feb 18 '20 at 00:14
  • Try .next() instead of nextLine() – Tix Feb 18 '20 at 00:21
  • You need to add this line of code: `scan.nextLine();` directly **after** this line of code: `int low = scan.nextInt();` so as to consume then newline (\n) character provided by the ENTER key being hit by the User entry. If you don't then the following prompt which utilizes the nextLine() method will consume it and because it's a newline character your prompt is automatically filled with a Null String ("") giving the impression that it was skipped over (bypassed). – DevilsHnd Feb 18 '20 at 00:24
  • i'll try that. thanks – Tony Ruiz Feb 18 '20 at 00:24
  • There is no need to instantiate Random upon each iteration of your **for** loop. It would be better to have `Random generator = new Random();` **above** your **for** loop. – DevilsHnd Feb 18 '20 at 00:42
  • My last comment was wrong about the curlly brace. You're actually missing the closing curly ( } ) brace for the **main()** method code block. Although curly braces are option for **if** stements I think it's always better to have them. It's easier to locate errors (as you can tell). ;) **Proper indentation** is hugely helpful as well! – DevilsHnd Feb 18 '20 at 00:47
  • Thank you guys. I'm still getting some kind of error but i cant figure out what it is. My question was shut down cause it's too similar to another issue but read that page and used the Integer.parseInt(scan.nextLine()) to no avail. Something is going wrong with the boolean part of the problem. any idea as to how i should title this question to keep it from getting shut down? – Tony Ruiz Feb 18 '20 at 01:01
  • Also thank you for the tip about the Random generator = new Random();I like to keep my codes as clean as possible. – Tony Ruiz Feb 18 '20 at 01:03

2 Answers2

0

write "scan.nextLine();" after "YesOrNo = scan.nextLine" and it will show u the yes/no line

deniscns
  • 27
  • 4
  • Wrong. It should be directly after the `int low = scan.nextInt();` line. – DevilsHnd Feb 18 '20 at 00:25
  • yes, true. still works after yesOrNo, but yes. – deniscns Feb 18 '20 at 00:34
  • `yes, true. still works after yesOrNo, but yes.` What does that mean? Are you under the impression your idea still works? A simple test: `String yesOrNo = scan.nextLine(); scan.nextLine(); System.out.println("The yesOrNo variable = " + yesOrNo);`. You tell me if the console displays a boolean value after `The yesOrNo variable =`. The **yesOrNo** variable will always hold a null string ("") if your answer is used. This means then that the switch statement will never be satisfied and the boolean **yesNo** variable will always fall to the default false. – DevilsHnd Feb 18 '20 at 02:31
-1

Try as below -

System.out.print("Would you like to include " + high + " into the list of random numbers(Yes or No)?");
yesOrNo = scan.next();

Because of previous nextInt() method, nextLine() is getting skipped.

More explanation here - https://stackoverflow.com/a/13102066

kann
  • 493
  • 9
  • 15
  • So, it should be marked as a duplicate – Scary Wombat Feb 18 '20 at 00:37
  • I changed it but now nothing happens after i hit enter on the yes/no question. no array is printed – Tony Ruiz Feb 18 '20 at 00:37
  • works for me `Please enter array size: 5 Please enter highest number: 10 Please enter lowest number: 3 Would you like to include 10 into the list of random numbers(Yes or No)?yes [10, 13, 3, 13, 12]` – Scary Wombat Feb 18 '20 at 00:47
  • ahh.. I'm so frustrated. I've changed the scan.input so many times but i cant figure it out. I'll copy and past the original code i posted and just make the change you suggested. hopefully that will do the trick. Thank you – Tony Ruiz Feb 18 '20 at 01:06
  • It does work now but i'm still getting a runtime error. There is a bug in the program even though it runs – Tony Ruiz Feb 18 '20 at 01:14
  • @ScaryWombat I don't have option to mark as duplicate. Hence provided link with little explanation. – kann Feb 18 '20 at 02:45
  • I feel very dumb. The error message i was getting was from a different class that i had open at the same time..... i wasted 5 hours hunting nothing. – Tony Ruiz Feb 18 '20 at 06:29