• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java trouble on netbeans

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
I'm taking my first stab at  Java. I'm working on an assignment for school. My instructor gives 4 different tasks on the same program. I'm working on a program called Random number game. I'm sure you have heard of it. I will past my code, but I cannot seem to get past the 2nd task when I run the program.  Please help, ive been working on this assignment for the last 5 days and I'm stuck on the this.

This is the task:
Random Number Guessing Game
Write a program that generates a random number from 1 - 10 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a while or do-while loop that repeats until the user correctly guesses the random number.


Here is my code:
 
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D neezy wrote:Hi all!
I'm taking my first stab at  Java



Nice first stab. Is this a complete program? I took the liberty of adding the requisite libraries, adding an outer class and a main, etc...

 
Saloon Keeper
Posts: 11057
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the 2nd task? And what is your question?

Note that random.nextInt( 11 ) returns values from 0-10(inclusive). If you want 1-10 use random.nextInt(10)+1.
 
Carey Brown
Saloon Keeper
Posts: 11057
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if( userGuessedNumber < randomNumber ){
There's a comparison error here.
 
Marshal
Posts: 80647
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for adding code tags, but you didn't quite get them right. Don't worry; I corrected that and you can see how much better it is. Please check the indentation; I think the first line being too far to the left is a quirk of the code tags. Don't put spaces around the . operator. Please also check the lengths of lines; they are too long and yu have to scroll left and right to read them. Break up the lines as follows, and as shown in the old (1999!) Sun style suggestions.Don't use System.exit(). It is a vicious instruction. You won't notice any problems in such a simple program, but if you are doing several things simultaneously in a multi‑threaded program, it is possible to stop all the tasks irrespective of whether they have completed, and you can get nasty errors from an incomplete process. Even worse, you might not find those errors until much later. Remove System.exit() and the program will simply complete and terminate.
That looks like an exercise from Head First Java by Sierra and Bates. Which edition is it? JOptionPane is a very old‑fashioned way to get keyboard input, but it still works.

I did not correct the error Carey noticed.
 
Campbell Ritchie
Marshal
Posts: 80647
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, did they say what the null in showInputDialog(...) means? It means you don't put the dialogue window in front of any particular part of the display (there aren't any other parts anyway), so the dialogue defaults to appearing in the centre of the screen. I have known it to go to the back and you have to click on the icon on the bottom panel to get it to appear.
I shall go back to my last post and remove a few spaces to improve the formatting.
Are you sure you are displaying the right number on the option pane?
Any idea how you could refactor that code so there is only one showMessageDialog(...) call?
 
Campbell Ritchie
Marshal
Posts: 80647
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Chike wrote:. . . I took the liberty of adding the requisite libraries, adding an outer class and a main, etc...

That's very helpful But there is another way to run that code; you open jshell, which has come with every version of Java® since Java9 automatically. You will need the import import javax.swing.JOptionPane; and you can feed the rest of the code straight to JShell to execute. Some imports for commonly‑used types, e.g. andom, ArrayList, IntStream, are already assumed by JShell. Like this, the option pane appears at the centre of the screen.

jshell
|  Welcome to JShell -- Version 21.0.1
|  For an introduction type: /help intro

jshell> import javax.swing.JOptionPane;

jshell> Random random = new Random();
  ...> int randomNumber = random.nextInt( 101 );
  ...> boolean userCorrect = false;
  ...> String userInputString;
  ...> int userGuessedNumber;
  ...>    
  ...> while( !userCorrect ) {
  ...>     userInputString = JOptionPane.showInputDialog(
  ...>             "Guess the number: ");
  ...>     userGuessedNumber = Integer.parseInt( userInputString);
  ...>     if( userGuessedNumber < randomNumber ) {
  ...>         JOptionPane.showMessageDialog( null,
  ...>                "(" + randomNumber +
  ...>                ")Too,high,please try again" );
  ...>     } else if ( userGuessedNumber < randomNumber ){
  ...>         JOptionPane.showMessageDialog( null,
  ...>                "(" + randomNumber +
  ...>                ") Too low, please try again: ");
  ...>     } else {
  ...>         JOptionPane.showMessageDialog( null,
  ...>                "(" + randomNumber +
  ...>                ") Great Job, you guessed the number. ");
  ...>         userCorrect = true;
  ...>     }
  ...> }
random ==> java.util.Random@4fca772d
randomNumber ==> 4
userCorrect ==> false
userInputString ==> null
userGuessedNumber ==> 0
... etc ...

 
D neezy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help! I ended  up not using  Joptionpane.  I did get it to run!  But now I am having the same trouble with the last task.  Here is the assigned task:
Weekday or Weekend
Write a program using a switch statement to show whether day of the week is a weekday or weekend. The day of the week is read in from the user as string: Monday through Friday are assumed to be weekdays and Saturday and Sunday are weekend days. Assume the day of the week will be lowercase.

Sample output, The user’s input is shown in bold:
Enter a day of the week (e.g. monday, tuesday, etc): wednesday [Enter]
wednesday is a weekday.

Here is my code:

int day = 2;
String dayType;
String dayString;

switch (day) {

   case 1:
       dayString = "Monday";
       break;
   case 2:
       dayString = "Tuesday";
       break;
   case 3:
       dayString = "Wednesday";
       break;
   case 4:
       dayString = "Thursday";
       break;
   case 5:
       dayString = "Friday";
       break;
   case 6:
       dayString = "Saturday";
       break;
   case 7:
       dayString = "Sunday";
       break;
   default:
       dayString = "Invalid day";
       break;
}
   switch (day) {
       case 1:
       case 2:
       case 3:
       case 4:
       case 5:
           dayType = "Weekday";
           break;
       case 6:
       case 7:
           dayType = "Weekend";
           break;
           
           
       default:
           dayType = "Invalid dayType";
   }
   
   
   System.out.println(dayString + " is a " + dayType);
   
               }
}

I appreciate your expertise!
 
Carey Brown
Saloon Keeper
Posts: 11057
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nowhere in your requirements does it mention or imply the use of int's. The only type mentioned is String which is how the input will be obtained.

Hint: you can switch on strings.
 
Carey Brown
Saloon Keeper
Posts: 11057
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read UseCodeTags (<-a link).

I see you are making an attempt but you need to read the documentation a little closer.
 
D neezy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my apologies, I will pay closer attention.
 
Carey Brown
Saloon Keeper
Posts: 11057
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies also,  the UseCodeTags has a link "select  all text" which  is broken.

Here's a brief example:
BEFORE clicking the "Code" button, highlight the code you want tagged.

To highlight code you can place the cursor  at the beginning of the code and drag it down to the end,  and release. Your code should then be highlighted in blue.

Alternatively you can click the cursor once just before the first character of the code, then scroll down to the end of the code and SHIFT-Click just to the right of the last  character. Again, selection should be in blue.

NOW click the "Code" button.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic