0

I recently got assigned a homework to make code in a different language, the only language I have worked with in python. What my code should do is ask the user for an input, and then first set it as the high, low and start calculating an average. However, I'm having 2 problems right now. One, my line 24 says string can't convert to an integer. However, I though I already told the program to allow it. If someone could help me that would be greatly appreciated.

package hgp.pkg13.pkg16;

import java.util.Scanner;
public class HGP1316 {
//Declaration of variables
static int Running_total = 0;
static double Average = 0.0;
static int User_input = 0;
static double counter = 0.0 ;
static int Highest_input = -1;
static int Lowest_input = -1; 
public static void main(String[] args) {
    //Intro Program (Optional)
    {
  System.out.println("This program will give you the highest, lowest, and average of the integers you \n"
          + "enter. To end enter a negative number ");       
}
//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4. Test the input >= 0
while (User_input >=0){
Scanner user_input = new Scanner( System.in );
Integer User_input;
System.out.print("Please enter an integer: ");
 User_input = user_input.nextInt();
//4a. If it is true that input >= 0
if (User_input >= 0)
{

    //4a1Add the input to "running total"
Running_total = Running_total + User_input;
    //4a2. Increment "number of inputs counter"
counter = counter + 1;  
//4a3. Test if "number of inputs counter" is equal to 1
if (counter == 1)
{
     //4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input;
Lowest_input = User_input; }

//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
{
    Highest_input = User_input;
}
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
{
    Lowest_input = User_input;
}
//4b. If false
    //4b1. Goto step 5
 else;
{
 //5. Calculate average (formula: "running total" /"number of inputs counter" )
    Average = (Running_total / counter);
 //6. Display/communicate "highest input", "lowest input" and average
   System.out.println ("The Highest value entered was : "+ Highest_input);
   System.out.println ("The Lowest value entered was : " + Lowest_input);
   System.out.println("The Average of enteries was : "+ Average);
//7. Terminate
}


 }
}
}
}    

The problem that I have now encountered is it goes through the whole loop instead of reasking for the users input. It is probably to do with my {} but I'm not sure

pckofwolfs
  • 83
  • 2
  • 2
  • 9
  • 5
    Use `user_input.nextInt()` instead of `user_input.next()` – dguay Nov 10 '15 at 19:40
  • What's your 2nd problem? – ControlAltDel Nov 10 '15 at 19:41
  • Well that looks like only problem for now. Also, my Integer User_input; It says that local variable hides a field. Is this something I should worry about. From what I read it was just because I am changing the number in which is stored in User_input – pckofwolfs Nov 10 '15 at 19:45
  • @pckofwolfs - It's because you have a class-level static User_Input and the local User-Input. – Dan W Nov 10 '15 at 19:52
  • Don't keep changing your question after you've received an answer. That's very bad form. If you have a new question, post a new question, don't reuse existing posts. – Sotirios Delimanolis Nov 10 '15 at 20:24
  • So I'm suppose to keep creating new post? If some I'm sorry about that, other site I was on wanted you to keep redoing the same post instead of making new ones. – pckofwolfs Nov 10 '15 at 20:25
  • If you're new, please take the tour and read the help center. – Sotirios Delimanolis Nov 10 '15 at 20:35
  • When i tried to create a now topic to get my new question answer it told me I had to wait 90 minutes to post a new question. – pckofwolfs Nov 10 '15 at 20:41

1 Answers1

-1

You're taking in the user input as a User_input = user_input.next(); when instead in java you need to use User_input = user_input.nextInt();. This tell the program that is is expecting an Integer input instead of another datatype/string/parameter.

Hope this helps

colt
  • 116
  • 12
  • Thank you!!!!!!!!!!!! More code to come in a bit. Hitting some more errors, however, I'm trying to find the answer before posting. – pckofwolfs Nov 10 '15 at 19:53
  • No problem. If you hit any other errors- post them and we will continue to help. If this answered your initial question please accept the answer by clicking the check mark :) @pckofwolfs – colt Nov 10 '15 at 19:54
  • Added to the code and yet again hit another error. I though i defined all variables, but apparently a symbol can't be found. @colt – pckofwolfs Nov 10 '15 at 20:02
  • @pckofwolfs what errors are being displayed now. I'll gladly help. Maybe update your Original Post with the error messages for me? – colt Nov 12 '15 at 04:23