1

today i have an issue with this code i wrote. The problem comes when i try and run it with command prompt, it doesnt display the last line of code i wrote "Congratulations, the birth month is April" If anyone understands why it would be helpful!

CODE:

import java.io.*;
import java.util.Scanner;

public class Lab3_5{
// Global variable to hold sales is defined
    static double age, weight, birthMonth;

    public static void main(String[] args){
// Method calls
        getAge();
        getWeight();
        getMonth();     
    }    

// This module takes in the required user input
public static void getAge(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter your guess for age: "); 
    double age = keyboard.nextDouble();
  if (age >= 25){
         System.out.println("Congratulations, the age is 25 or less."); 
    }
}

// This module takes in the required user input
public static void getWeight(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter your guess for weight: ");  
    double weight = keyboard.nextDouble();
    if (weight <= 128){
         System.out.println("Congratulations, the weight is 128 or less.");
    }   
    }
    // This module takes in the required user input
public static void getMonth(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter your guess for birth month: "); 
    String birthMonth = keyboard.next();
    if (birthMonth == "April"){
         System.out.println("Congratulations, the birth month is April.");
    }   
    }
}
2
  • very last bit of code got cut off was supposed to be: if (birthMonth == "April"){ System.out.println("Congratulations, the birth month is April."); } } } Commented Oct 24, 2017 at 18:58
  • Looks like that code is there. The code box scrolls for long sections of code. For future reference, you can also edit the question to add any missing details. Commented Oct 24, 2017 at 19:05

3 Answers 3

2

It has no relation with the command prompt.

The problem is that :

if (birthMonth == "April"){

should be :

if ("April".equals(birthMonth)){

Strings have to be compared with equals().

birthMonth == "April" is true only if these are the same object.
This is not always the case wheras equals() compares the content of the Strings.

Sign up to request clarification or add additional context in comments.

Comments

0

The command prompt is not the issue here.

You can't use "==" with strings, you need to use equals() or equalsignorecase()

public static void getMonth(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter your guess for birth month: "); 
    String birthMonth = keyboard.next();
    if (birthMonth.equalsIgnoreCase("April")){
         System.out.println("Congratulations, the birth month is April.");
    }   
    }
}

This is because String are objects. (note that String is uppercase what is good practice for classes!)

So you are looking at two different String classes and you are asking if they are the same, which they are not off course. So by using equals() or equalsIgnoreCase, you tell the class to compare itself with the other based on what kind of chars it holds.

Comments

0

In java String is a class and hence birthMonth is an object and so you cannot do

if (birthMonth == "April")

Since == tests for reference equality (whether they are the same object). you have to string function .equals() or equalsIgnoreCase() (ignores cases) which tests for value equality (whether they are logically "equal").

Like So

if (birthMonth.equalsIgnoreCase("April"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.