Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsWhy is my return not working, and my randomNumber generator?
Hey guys,
I've got my code to run so far, but it is not running correctly. For example, I've tried to set my fillJar() method to fill a jar with a random number of items between 0 and the max number of items that the user entered. However, the random number is generating a number outside of the range. Additionally, my return statement at the end of my do while loop is not working. I'm also not able to call 'count' which I need to do in the return. I want to print "You got it in X attempt(s)"; with X being the count.
Thank you in advance!! :))
Main Class
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
System.out.println("ADMINISTRATOR SETUP\n********************");
System.out.println("GUESSING GAME: Guess how many items are in the jar.\n\nSelect the type of items, and how many can reasonably fit in a jar.\nSound good? Okay let's begin!\n");
if(args.length != 2) {
Jar jar = new Jar();
jar.fillJar();
}
}
}
import java.util.Scanner;
import java.util.Random;
public class Jar {
private String itemName;
public int maxItems;
public Jar() {
Scanner scanner = new Scanner(System.in);
System.out.println("What type of item?");
this.itemName = scanner.nextLine();
System.out.println("What is the maximum amount of " + itemName + " ?");
this.maxItems = Integer.parseInt(scanner.nextLine());
System.out.println("\nPLAYER\n********************\nHow many " + itemName + " are in the jar?\nPick a number between 1 and " + maxItems);
}
public String fillJar() {
Random randomNumber = new Random();
int rand = randomNumber.nextInt(maxItems);
Scanner scanner = new Scanner(System.in);
int guess = Integer.parseInt(scanner.nextLine());
do {
System.out.println("\nHow many " + itemName + " are in the jar? Pick a number between 1 and " + maxItems);
int count = 0;
Scanner scanner1 = new Scanner(System.in);
guess = Integer.parseInt(scanner1.nextLine());
count++;
} while(guess != rand);
System.out.println("You got it in X attempt(s)");
return "You got it in X attempt(s)";
}
}
1 Answer
Craig Lovell
5,752 PointsI'm not near a computer right now, but I would try printing out guess to see what type of number the parseInt function is giving you back.
If I had to guess, parseInt is giving you an incorrect number because you are using nextLine (which has a new line character, and could result in a number that is not what you expected).
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsJennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsUPDATE***
I rewrote my code to allow for my message to print with how many attempts it took, but I had to put it in the block before the return statement.. However, my count is off, and my randomNumber generator is not creating a random number within the scope of 'maxItems' like I intended for it to.