-1

My code:

import java.util.Scanner;

public class MonthMapper{
    static String month;
    static int month_num;

    public static boolean isMonthNumber (String str) { 
        month = str;
        month_num = Integer.parseInt(month);
        return (month_num >= 0 && month_num < 12); 
    } 

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Month: ");
        month = sc.next();
        System.out.println (isMonthNumber (Integer.toString(month_num)));
    }
}

I have to write a static class method boolean isMonthNumber(String str) that takes a String as an input and returns boolean value. The method returns True if the input string represents an integer value between 1 and 12, otherwise the method returns should return False.

Currently for some reason my program always returns true even when i enter a value greater than 12.

resueman
  • 10,572
  • 10
  • 31
  • 45
Win
  • 217
  • 6
  • 13

1 Answers1

2

You pass mounth_num variable to a method, but the month variable has the read value.

Replace with this:

public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter Month: ");
  System.out.println(isMonthNumber (sc.next()));
}
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • I see, but now I have to enter the input twice for the program to work, how can i make it so i only have to input once? Edit: nevermind I realized how silly a question this was. Thank you very much for your swift answer! :-) – Win Sep 18 '15 at 18:08