Question: Please help!! I keep getting this error and i dont understand. Exception in thread main java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:911) at java.util.Scanner.next(Scanner.java:1534) at java.util.Scanner.nextInt(Scanner.java:2164) at java.util.Scanner.nextInt(Scanner.java:2123) at
Please help!!
I keep getting this error and i dont understand.
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:911) at java.util.Scanner.next(Scanner.java:1534) at java.util.Scanner.nextInt(Scanner.java:2164) at java.util.Scanner.nextInt(Scanner.java:2123) at WithinBounds.main(WithinBounds.java:84)
Below is my code:
// Don't edit the code between here and the next TODO.
import java.util.Scanner;
/* * This class prompts the user for (x, y) coordinates and tests whether the * resulting point is inside or outside bounds. If the point is out of bounds, * the code must assign a point to player 1 or player 2. * The bounds for the game are x=[0..100], y=[0..25]. The boundary lines are * all counted as "in bounds." The line x=50 counts as in bounds, even if y * is out of bounds. * In the example below, player 1 owns the right half, player 2 owns the * left half. * A ball (symbolized by "o") inside the court counts as no player getting a point. * A ball (symbolized by "o") outside the court counts as player 1 or player 2 * getting a point. In the case below, the point at (120, 60) means that player 1 * gets a point. * * player 2 player 1 * o (xCoord, yCoord) == (120, 60) * 25 --+------------|-------------+ * | | | * | | | * | | | * | | o | (xCoord, yCoord) == (65, 5) * 0 --+------------|-------------+ * | | | * 0 50 100 */ public class WithinBounds { /* * Method to check the bounds of the playing surface against the xCoord * and yCoord values the user passes in. * The playing surface goes from x=0 to x=100. * The playing surface goes from y=0 to y=25. * Inbounds and out-of-bounds are as described above. */ public int pointFor(int xCoord, int yCoord) { // Declare result and assign 0 so that initially no player gets a point. int result = 0; // TODO: Put your code below to check whether the point with coordinates // (xCoord, yCoord) lies within the playing surface. Your result must be one of // 1 player 1 gets a point // 2 player 2 gets a point // 0 neither player gets a point // NOTE: Assign the integer result of your computation into the 'result' variable. // You MUST assign a value of 0, 1 or 2 to the 'int result' variable per the rules // above. if (((xCoord < 0) && (xCoord > 50 )) && ((yCoord < 0) && (yCoord > 25))) { result = 1; } else if (((xCoord < 50) && (xCoord > 100 )) && ((yCoord < 0) && (yCoord > 25))) { result = 2; } else { result = 0; }
// TODO: Do not touch the next line. return result; } /* * main() method for the bounds checking code. */ public static void main(String[] args) { // Set up local variables. Do not edit this code until the next TODO. WithinBounds wb = new WithinBounds(); Scanner scan = new Scanner(System.in); final String PROMPT_USER = "Input two int values between -200 and +200."; int xCoord = 0; int yCoord = 0; int player = 0; // Keep going until a point is scored for player 1 or 2. while (player == 0) { // TODO: // 1. Prompt the user with the PROMPT_USER string and read two ints. // 2. Assign the first int to xCoord. // 3. Assign the second int to yCoord. System.out.println(PROMPT_USER); int firstIntValue = scan.nextInt(); int secondIntValue = scan.nextInt(); firstIntValue = xCoord; secondIntValue = yCoord;
// NOTE: Do not edit the remaining lines in this file. // Call the pointFor() method to determine whether the point // was scored for player 1 or player 2. Neither player scores // the point, then 0 is returned. player = wb.pointFor(xCoord, yCoord); if (player != 0) { System.out.println("That's a point for player " + player + "!"); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
