T.Brown68
T.Brown68

Reputation: 11

My program wont go past my first few lines of code

So I'm pretty new to this site, but you guys are my last hope. The goal here is that a user will input 10 temperatures in Fahrenheit (enter -999 to stop while entering) then the program will change those temps into Celsius. Also the user will need to type in their location. I am able to type in the location, but after I hit 'Enter' I get:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Prog1.main(Prog1.java:20)`

Here is my code:

import java.util.Scanner;
public class Prog1 
{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        //asks the user for the location
        System.out.print("Enter Location: ");
        int place = in.nextInt();
        System.out.print(place);

        double[] temp = new double[10];
        int num = 0;
        //prime the loop
        System.out.print("Enter temperature: ");
        int input = in.nextInt();
        //get up to 10 temperatures and store in array "temp"
        while (input >= -998 && num < 10) {
            temp[num] = input;
            num++;

            System.out.print("Enter temperature: ");
            input = in.nextInt();
        }
        //print report
        System.out.println();
        System.out.printf("%5s    %5\n", "Fahrenheit", "Celcius");
        for (int x = 0; x < num; x++) {
            System.out.printf("%5d    %5s\n", temp[x], celsius( temp[x]));
        }

        System.out.printf("\nHigh: %6.2f", max( temp, num));
        System.out.printf("\nLow: %6.2f", min( temp, num));
        System.out.printf("\nAverage: %6.2f", average(temp, num));      
    }

    /**
     * Method to convert farenheit to celsius
     * @param double farenheit temperature
     * @return double celsius temperature 
     */
    public static double celsius(double input) {
        double celcius = 5.0 / 9.0 * ( input - 32);
        return celcius;
    }

    /**
     * Method to calculate average, min and max temperatures
     * 
     * @param double farenheit temperature
     * @return average, min and max temperatures
     */
    public static double average(double[] temp, int num) {
        double sum = 0;
        for (int x = 0; x < num; x++) {
            sum += temp [x];
        }
        return (double) sum / num;
    }

    public static double max( double[] temp , int num){
        double max = temp[0];
        for (double x : temp) {
            if (max > num) {
                max = num;
            }
            return num; 
        }
        return  max;
    }

    public static double min(double[] temp, int num) {
        double min = temp[0];
        for (double x : temp) {
            if (min < num) {
                min = num;
            }
            return min;
        }
        return num;
    }
}

Upvotes: 0

Views: 168

Answers (4)

Praveen Gowthaman
Praveen Gowthaman

Reputation: 24

The solution above is quite a simple one.You have to replace:

 System.out.print("Enter Location: ");
 int place = in.nextInt();
 System.out.print(place);

with

 System.out.print("Enter Location: ");
 String place = in.nextLine();
 System.out.print(place);

since location is a string,when you give something like 'India' in.nextInt() would be expecting a number instead of string.So you were getting that error.Replace it only below enter location sysout.

replace formatting too. try this :

import java.util.Scanner;
public class Prog1 
{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        //asks the user for the location
        System.out.print("Enter Location: ");
        String place = in.nextLine();
        System.out.print(place);

        double[] temp = new double[10];
        int num = 0;
        //prime the loop
        System.out.print("Enter temperature: ");
        int input = in.nextInt();
        //get up to 10 temperatures and store in array "temp"
        while (input >= -998 && num < 10) {
            temp[num] = input;
            num++;

            System.out.print("Enter temperature: ");
            input = in.nextInt();
        }
        System.out.println();
        System.out.printf("%s    %s\n", "Fahrenheit", "Celcius");
        for (int x = 0; x < num; x++) {
            System.out.println(temp[x] + "\t" +celsius(temp[x]) );
        }

        System.out.printf("\nHigh: %6.2f", max( temp, num));
        System.out.printf("\nLow: %6.2f", min( temp, num));
        System.out.printf("\nAverage: %6.2f", average(temp, num));      
    }

    /**
     * Method to convert farenheit to celsius
     * @param double farenheit temperature
     * @return double celsius temperature 
     */
    public static double celsius(double input) {
        double celcius = 5.0 / 9.0 * ( input - 32);
        return celcius;
    }

    /**
     * Method to calculate average, min and max temperatures
     * 
     * @param double farenheit temperature
     * @return average, min and max temperatures
     */
    public static double average(double[] temp, int num) {
        double sum = 0;
        for (int x = 0; x < num; x++) {
            sum += temp [x];
        }
        return (double) sum / num;
    }

    public static double max( double[] temp , int num){
        double max = temp[0];
        for (double x : temp) {
            if (max > num) {
                max = num;
            }
            return num; 
        }
        return  max;
    }

    public static double min(double[] temp, int num) {
        double min = temp[0];
        for (double x : temp) {
            if (min < num) {
                min = num;
            }
            return min;
        }
        return num;
    }
}

Upvotes: 0

A_P
A_P

Reputation: 354

I've run your code just to see what is going on. You were not formatting printf correctly. Replace this part it'll do.

//print report
        System.out.println();
        System.out.printf("%s    %s\n", "Fahrenheit", "Celcius");
        for (int x = 0; x < num; x++) {
            System.out.println(temp[x] + "\t" +celsius(temp[x]) );
        }

I find this quick guide useful : http://web.cerritos.edu/jwilson/SitePages/java_language_resources/Java_printf_method_quick_reference.pdf

Upvotes: 0

Orin
Orin

Reputation: 930

InputMismatch Exceptions are thrown when you put a datatype somewhere that was expected to be a different datatype. Since you are working with degrees, I'm assuming you might be entering floating point values, so in.nextDouble() might be a better option than in.nextInt(). You can then cast it to an integer later if you want that to be your output.

Upvotes: 0

Spencer
Spencer

Reputation: 174

You're getting an InputMismatchException, which means that whatever the user is submitting to you is of a different type then you have programmed for.

You ask for the user to input their location, and then call in.nextInt(). nextInt() will throw an exception if the next token in the standard input is not an integer. You must be entering something other than an integer for the location which is throwing the exception. If you want to enter a text based location (like a String) try in.nextLine()

Upvotes: 0

Related Questions