Question

I am currently doing homework for my java class and i am getting an error in...

I am currently doing homework for my java class and i am getting an error in my code.

import java.util.Scanner;
import java.util.Random;
public class contact {

  

   public static void main(String[] args) {
       Random Rand = new Random();
       Scanner sc = new Scanner(System.in);
       System.out.println("welcome to the contact application");
       System.out.println();
      
       int die1;
       int die2;
       int Total;
      
       String choice = "y";
while (choice.equalsIgnoreCase("y")) {
       System.out.println("roll the dice? (y/n): ");
       choice = sc.next();
       die1= Rand.nextInt(6) + 1;
      
       if (sc.hasNextDouble()) {
die1 = sc.hasNextInt();
sc.nextLine();
} else {
System.out.println("Error! number. Try again.\n");
sc.nextLine();
}
       System.out.println("Die 1 : " + die1);
       die2 = Rand.nextInt(6) + 1;
       System.out.println("Die 2 : " +die2);
       Total= die1 + die2;
       System.out.println("Total : " +Total);
       System.out.println("roll again? (y/n):");
       choice = sc.next();
   }

   }
  
}

i am trying to create validate the data that the user inputs and when i try to use sc.hasNextInt(); i keep getting an error from eclipse saying cannot convert from boolean to int.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the updated code. Hope this is hat you where looking for.

========================================================================

import java.util.Scanner;
import java.util.Random;

public class contact {

    public static void main(String[] args) {
       Random Rand = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to the Contact Application");
        System.out.println("==================================");

        int die1;
        int die2;
       int Total;

        String choice = "y";
        do {
            System.out.print("Roll the dice? (y/n): ");
            choice = sc.nextLine();
            if (choice.equalsIgnoreCase("y")) {

                die1 = 1 + Rand.nextInt(6);
                die2 = 1 + Rand.nextInt(6);
                Total = die1+die2;
                System.out.println("Die 1: " + die1);
                System.out.println("Die 2: " + die2);
                System.out.println("Total: " + Total);
            }
        } while (choice.equalsIgnoreCase("y"));
    }

}

========================================================================

thanks a lot !

Add a comment
Know the answer?
Add Answer to:
I am currently doing homework for my java class and i am getting an error in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • This is what I have so far. I'm getting an error on the ... case 3:...

    This is what I have so far. I'm getting an error on the ... case 3: System.out.println("Enter the rank of the card you want removed"); cards.remove(); System.out.println(); break; -------------------------------------- package PlayingCards; import java.util.Scanner; public class Driver { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean done = false; int menuInput; DeckOfCards cards = new DeckOfCards(); do { System.out.println("Enter the number of one of the choices below:"); System.out.println("1: Shuffle The Deck."); System.out.println("2: Print The Cards Remaining In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT