• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to make a grading program loop to start from the beginning

 
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that is meant to display the total, average, student name and letter grade for a student. I'm almost done writing it but I have no idea how to make it loop so it goes back to step 1 from step 4.


 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should only ever create one instance of a Scanner from System.in, and you should never ever close it, no matter what warning messages say.

You have a number of variables declared but not used: mark4, grade, A, B, C, D, F, marks.

The result of this expression may be cast as a double but the actual arithmetic will be operated on as ints.
would be more accurate if written as
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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
Note that after your last call to input.nextInt() you'll have to call input.nextLine() to flush out the new-line that will be left over in the input buffer. You can ignore the returned string from this call to nextLine().
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown

The extra variables like mark 4 and the D were mistakes I made but forgot to delete. The program is only meant to grade A,B and C and anything else is an F.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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

Carey Brown wrote:The result of this expression may be cast as a double but the actual arithmetic will be operated on as ints.

I take this back. I misread where the ending paren was.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Note that after your last call to input.nextInt() you'll have to call input.nextLine() to flush out the new-line that will be left over in the input buffer. You can ignore the returned string from this call to nextLine().




This one didn't work for me. When I run it it tells me "Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at name.isEmpty(name.java:14)
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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 repost the entire code and the entire error message.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Please repost the entire code and the entire error message.




Error Message - "Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at name.isEmpty(name.java:14)
at Averages5.main(Averages5.java:8)"

 
Carey Brown
Saloon Keeper
Posts: 10687
85
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
Your error message says you are trying to use a file called "name.java". What is this?

Your other java file compiles and runs fine for me.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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
You are still constructing two Scanners.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your error message says you are trying to use a file called "name.java". What is this?

Your other java file compiles and runs fine for me.



Oh sorry, this was the code without the stuff you told me. Here is what gave me the error message.

 
Carey Brown
Saloon Keeper
Posts: 10687
85
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
Did you post your current code? The error message mentions "isEmpty" but the code you posted doesn't have that.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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
On line 7 you are using "name" but you don't define "name" until line 10. You'll have to move the line where you define it. Also look and where you defined "input". If it is inside the loop a new one will be created each time around the loop. Remember I said that Scanner should only ever be created once.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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

Carey Brown wrote:Note that after your last call to input.nextInt() you'll have to call input.nextLine() to flush out the new-line that will be left over in the input buffer. You can ignore the returned string from this call to nextLine().


The first line here says "// get user name". This is a hint of what you need to have here in place of this comment.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're still closing the Scanner.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You're still closing the Scanner.



I'm sorry, I'm really new to this. The problem you mentioned with the scanner is what I think is stopping this from running properly.

I got the program to loop but it doesn't ask for a new name, it repeats the same inputted information.

Could you break down for me what I need to do with the scanner?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you make changes to the code you need to repost it so that I see what you see.

Scanner is more an "advanced beginner" subject but seeing as how Java doesn't really have good alternatives you will have to pay close attention to whatever advice is given to you regarding Scanners.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:When you make changes to the code you need to repost it so that I see what you see.

Scanner is more an "advanced beginner" subject but seeing as how Java doesn't really have good alternatives you will have to pay close attention to whatever advice is given to you regarding Scanners.



 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Wrong code again.



I know it's wrong. I need your help!
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, you moved the loop. I didn't see it.

So you prompt for the grades only once because it is outside the loop. Is this what you want to do?

Consider that lines will be executed in order unless there is a loop to tell it otherwise.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Ah, you moved the loop. I didn't see it.

So you prompt for the grades only once because it is outside the loop. Is this what you want to do?

Consider that lines will be executed in order unless there is a loop to tell it otherwise.



I'm attempting to set it up so you can input grades for name x, then once the output is displayed it prompts you to input grades for name y and so on.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You should only ever create one instance of a Scanner from System.in, and you should never ever close it, no matter what warning messages say.

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:I'm attempting to set it up so you can input grades for name x, then once the output is displayed it prompts you to input grades for name y and so on.


Yes but you are only prompting for grades once. Outside the loop.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:


Take this step by step:
1) all the code required to get the name and nothing more.
2) this "while" statement
3) all your other code except for the "get name" code you used in step 1.
4) repeat the get name code here to get the next name
5) closing brace for while loop.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:


Take this step by step:
1) all the code required to get the name and nothing more.
2) this "while" statement
3) all your other code except for the "get name" code you used in step 1.
4) repeat the get name code here to get the next name
5) closing brace for while loop.



Like this?

}
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting closer. Instead of 2nd while repeat lines 19 and 20.

AND REMOVE input.close().
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AND REMOVE
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Getting closer. Instead of 2nd while repeat lines 19 and 20.

AND REMOVE input.close().



lol this is so frustrating. Thank you so much for your help so far by the way. Did as you said and also removed the scanner line you said to. got another error message!

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
Enter Your Name
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.findWithinHorizon(Scanner.java:1670)
at java.util.Scanner.nextLine(Scanner.java:1538)
at Averages5.main(Averages5.java:49)

 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:

Carey Brown wrote:Getting closer. Instead of 2nd while repeat lines 19 and 20.

AND REMOVE input.close().



lol this is so frustrating. Thank you so much for your help so far by the way. Did as you said and also removed the scanner line you said to. got another error message!

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
Enter Your Name
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.findWithinHorizon(Scanner.java:1670)
at java.util.Scanner.nextLine(Scanner.java:1538)
at Averages5.main(Averages5.java:49)



oh scratch that. no error message. i forgot to get rid of the input.close line.

it runs but it prompts me to enter a new name but it doesnt accept anything when you press enter.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Stevens wrote:

Carey Brown wrote:Getting closer. Instead of 2nd while repeat lines 19 and 20.

AND REMOVE input.close().

Did as you said and also removed the scanner line you said to. got another error message!


No you didn't. It's still there on line 33.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the change you'll need to flush the pending new-line as I mentioned earlier. Without it it won't process the next name.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Sean Stevens wrote:

Carey Brown wrote:Getting closer. Instead of 2nd while repeat lines 19 and 20.

AND REMOVE input.close().

Did as you said and also removed the scanner line you said to. got another error message!


No you didn't. It's still there on line 33.



Yeah sorry missed that one. It didnt have an error message without the "input.close" but it also didnt allow me to enter a new name (just displayed (Enter your Name)

I'm using the Netbeans IDE if that is relevant in anyway.
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Here's the change you'll need to flush the pending new-line as I mentioned earlier. Without it it won't process the next name.



Thank you so much, works fine. I know I just seem like some student looking for easy answers but I'm not, I just needed guidance on finishing up this project.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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

Sean Stevens wrote:Thank you so much, works fine. I know I just seem like some student looking for easy answers but I'm not, I just needed guidance on finishing up this project.

We often get students in the Beginner forum so this is sort of expected. Also, as it is the Beginner forum, we try not to just give you the answer but to give you hints and guidance so that you can work out some of this for yourself. I hope that now that it is working that you'll study your code to fully understand how it flows.

You still have unused variables and now with the addition of the loop you'll have to readjust your indentation.

When you're all done post your final result so that others may learn from it.

Cheers.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
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
You need to keep a closer eye on your prompts. Remember the user doesn't know what is expected unless you tell her. In this example you are expecting "three" grades. Otherwise how's a person to know.
Similarly, the prompt for name should include something like " or press Enter to exit".
 
Sean Stevens
Ranch Hand
Posts: 47
Netbeans IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Sean Stevens wrote:Thank you so much, works fine. I know I just seem like some student looking for easy answers but I'm not, I just needed guidance on finishing up this project.

We often get students in the Beginner forum so this is sort of expected. Also, as it is the Beginner forum, we try not to just give you the answer but to give you hints and guidance so that you can work out some of this for yourself. I hope that now that it is working that you'll study your code to fully understand how it flows.

You still have unused variables and now with the addition of the loop you'll have to readjust your indentation.

When you're all done post your final result so that others may learn from it.

Cheers.



Here's the finalized project for others to see. Thanks again for your help, you rock!

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things you still need to do:

* Remove variables A, B, C, and F.  They are not used
* Reformat the code.  In netbeans, as in other IDEs, this is a simple key combination (that I have forgotten).
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Reformat the code.  In netbeans, as in other IDEs, this is a simple key combination (that I have forgotten).


It's Alt-Shift-F, or right-click the mouse pointer to the source code and select Format.
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic