-1

I need to create a way for the user to enter the weather (snowy, rainy, or sunny) and after they do this it effects the MPG (Miles Per Gallon) of the car. Snowy minuses 4, rainy minuses 2 and sunny has no effect. The hint I had was to use in.equals but I know my code is far off. So my question is how do I ask the user for a input that effects another variable with three possible answers?

 import java.util.Scanner;

public class AdvancedTripCalc {

    public static void main(String[] args) {
        // New Trip Calc.
        String firstname;
        int mpg;
        int miles;
        double price;
        double totalcost;

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter your First Name");
        firstname = in.nextLine();
        System.out.println("Please enter the MPG of your car");
mpg = in.nextInt();
boolean city;
System.out.println("Enter true if trip is in the city false if the trip is on the highway");
city = in.nextBoolean();
if (city){
    mpg = mpg - 2;
}
else { 
    mpg = mpg + 5;
}
boolean weather;
double snowy = 0;
double rainy = 0;
double sunny = 0;
System.out.println("What is the weather like?");
weather = in.equals(snowy);
{
    mpg = mpg - 4;
}
weather = in.equals(rainy);{
    mpg = mpg - 1;
}
weather = in.equals(sunny);{
    mpg = mpg;
}
System.out.println("Please enter the Miles to be traveled");
miles = in.nextInt();
System.out.println("Please enter the price per gallon of gas");
price = in.nextDouble();

totalcost = miles * price / mpg;
System.out.println("Your name is " +firstname);
System.out.println("Your MPG is " +mpg);
System.out.println("Your miles to be traveled is " +miles);
System.out.println("Your price per gallon of gas is " +price);
System.out.println("Your total cost is " +totalcost);

    }

}

This is what I have so far.

Flimzy
  • 60,850
  • 13
  • 104
  • 147
Slowwork
  • 9
  • 2

5 Answers5

0

Comparing Strings in java is done with a.equals(b) if a and b are Strings.

a == b would test if a and b are the same Object. So what you need to do is: Read weather as a String (String weather = in.readLine() for example) and then you can compare it with:

if(weather.equals("snowy"){..}

else if(weather.equals("rainy"){..}

and so on

Thomas D.
  • 3
  • 3
0

What you can do is take the user's input and compare it using a case statement (or even an if statement) like so:

System.out.println("What is the weather like?");
Scanner read = new Scanner(System.in);
string weather = read.next()
if (weather.equals("snowy")){
    mpg -= 4;}
else if (weather.equals("rainy")){
    mpg -= 2;}
theGirrafish
  • 1,475
  • 1
  • 16
  • 27
-1

There is a pretty good example here Reading in from System.in - Java which I dont have time to write out for you. But essentially you need to create a Scanner for system.in this will block the application at that point and wait for some input on System in, before continuing. It is general a good idea to do a System.out.print before this so that the user knows they need to enter something.

Community
  • 1
  • 1
steves165
  • 314
  • 1
  • 3
-1

You can just use if statements after reading the user input with next() on the Scanner object you have (in):

String weather = in.next();
if (weather.equals("snowy"))
    mpg -= 4;
else if (weather.equals("rainy"))
    mpg -= 1;
...
else System.out.println("Wrong input");
Idos
  • 14,036
  • 13
  • 48
  • 65
-1

You can't use in.equals, but you can read a string and then to .equals:

String weather = in.next();
if("sunny".equals(weather)) {
    mpg = mpg - 4;
} else if("rainy".equals(weather)) {
    mpg = mpg - 1;
}
weston
  • 51,132
  • 20
  • 132
  • 192