• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Scanner Error

 
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is my first time posting on here.
I am doing a coding project and I am required to "Write a sentinel controlled while loop that will allow you to calculate the average low temperature for any month.  The average temperature should be displayed as a properly calculated double value. Explain in comments why you chose the sentinel value.  The code for the input of the initial temperature value is provided."


The problem is every time I enter an input for temperature, the console doesn't change, it doesn't restart the scanner. I've been searching for the problem for over 2 hours.

I will input my code below.



   

 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You need to assign the result of calling scan.nextInt() to a variable. Then you can check if it is equal to the sentinel value at which point you terminate your loop. There should be a statement inside the loop that displays the prompt as well, so that the user knows what is going on. The way you're calling the scanner methods, the value that was entered is never saved anywhere and therefore does not get included in any calculation you're doing.

Here's an example the might help you understand how the scanner works:

 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just edited my code to assign the variable and the the code still does the exact same thing as before.

I included my updated code below.




 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Examine your logic again. It is wrong.

The call to nextInt() on line 9 in the while loop condition is an input that doesn't get used. Then on line 11, you input another int value which you add to the average variable. That variable doesn't represent the average by the way, so that in itself is misleading.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The call to nextInt() on line 9 in the while loop condition is an input that doesn't get used.



That is the scanner input for temp which I use for this code segment:




That variable doesn't represent the average by the way



I don't understand what you mean by that, the average is meant to add with the temp each time the scanner input is run.
 
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

regg jones wrote:. . . the average is meant to add with the temp each time the scanner input is run.

That isn't an average.

Please avoid coloured text, which can be difficult to read; I have taken the colours off.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That isn't an average.



Yeah I just removed it my bad for not recognizing it. Sorry for the colored text as well. What other code could I to get the temp input? That and my scanner for temp won't restart after the initial run.

Updated code


 
Master Rancher
Posts: 3719
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ignore, post was unneeded since you've now already seen the problem.]
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems I am down to one more problem. As I enter the input, I want the program to run the temperature scanner again until the temp input is 0.

Here is my code

 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't really have a problem with Scanner; you have misunderstood how a loop works. What you are doing is testing nextInt() to see whether it is 0, and the reading the next result of nextInt(). You might not have entered anything to read, so your program will wait for the next input. You will have similar problems with line 20.
You only need to test the value once in the loop, and you need to use that value. So you need to read nextInt(), assign it to something, and then text whether you have got 0. You will need some strange‑looking syntax which you would never guess in a month of Sundays. This is what I would do. Note you need an additional pair of () because you want the = operator executed before the !=.
Don't call a variable temp because many people will misinterpret that as meaning “temporary”. Your latest code won't compile because you haven't declared and initialised temp.You can (maybe should) use var instead of int in lines 3‑4, but var won't work in line 2. The reason for multiplying by 1.0 in line 10 is to convert the value to a double because the rounding in integer arithmetic might be too severe and give you an average of 0. See what happens if you enter 0 for the first value and you have no input at all
Forget about nextLine() calls, etc.; you don't need them here. That call will probably remove the first number you enter.
Don't use two Scanners to read System.in. You only ever need one. You will have to think again about different months.
 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

regg jones wrote:As I enter the input, I want the program to run the temperature scanner again until the temp input is 0.


Why would you use 0 as your sentinel value? Since your inputting low temperatures, 0 could absolutely be a valid temperature to be included in your dataset. You could also have negative numbers as well. I would reconsider that choice, especially since you're also supposed to justify your choice of sentinel value.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have removed the second scanner and tweaked different parts of my code. The scanner still won't run after the first one.

Updated code:

 
Junilu Lacar
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You keep insisting that the scanner "won't run after the first one" -- what makes you think that? If you look at your program closely, it will only ever prompt you to enter a temperature one time. After that first time, it will "silently" wait for you to enter more numbers. That's because the statement to display the prompt is outside of the loop. If you want to display the prompt to enter a temperature for each number, you'll need a statement to do that inside the loop.
 
regg jones
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see, I have changed my code now.

It doesn't seem to be registering whenever I put in a 1000 for the input.


   
 
Marshal
Posts: 3257
487
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working, just not the way that you would like it to work:
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, the problem is with how your loops work. Please show us the whole input you are giving the program. Why do you want 1000? You probably don't want that if, and if you do want to use the if, you should get 0 and then ask for input=1000. Make sure to display clear instructions to the user,
As Junilu said, 0 probably isn't a good sentinel value because 0 is a valid temperature. You could try anything < −273 because that wouldn't be a valid temperature

Another way to do it is for the loop to run while (scan.hasNextInt()) ... Then you can enter, “stop,” and the loop will terminate.
 
regg jones
Greenhorn
Posts: 19
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see, thanks to everyone who replied in this thread!!  
Edit - (I figured out the problem, the code now runs as it's supposed to.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic