This discussion is archived
6 Replies Latest reply: Apr 29, 2009 7:32 PM by 843789 RSS

Need a little help with strings

843789 Newbie
Currently Being Moderated
So I would like to have the user input a month by using the abbreviation ex. (mar or apr) instead. So I want month to be a string right? When I tried that I get an error. What should this look like?

import java.util.*;

public class MonthSwitch
{
  static Scanner console = new Scanner(System.in);
  
  public static void main(String[] args)
  {
    
    int month;
    
    System.out.print("Enter a Month: ex. Jan, Feb, Mar ect...");
    month = console.nextInt();
    System.out.println();
      
    switch (month)
    {
      case 1: System.out.println("First Quarter");
      case 2: System.out.println("First Quarter");
      case 3: System.out.println("First Quarter");
              break;
      case 4: System.out.println("Second Quarter");
      case 5: System.out.println("Second  Quarter");
      case 6: System.out.println("Second  Quarter");
              break;
      case 7: System.out.println("Third Quarter");
      case 8: System.out.println("Third Quarter");
      case 9: System.out.println("Third Quarter");
              break;
      case 10: System.out.println("Fouth Quarter");
      case 11: System.out.println("Fouth Quarter");
      case 12: System.out.println("Fouth Quarter");
              break;
      default: System.out.println("Sorry Input is Invalid");
    }
       
  }
}
Edited by: NewJavaKid on Apr 29, 2009 10:47 PM
  • 1. Re: Need a little help with strings
    843789 Newbie
    Currently Being Moderated
    I'm guessing you tried to do a switch on a string:
    String month = ...
    switch (month) {
    ...
    }
    No can do. Just use ifs or a Map.
  • 2. Re: Need a little help with strings
    843789 Newbie
    Currently Being Moderated
    BTW if you were to do this using ints the code would be as following:
    case 1:
    case 2:
    case 3:
        System.out.println("First Quarter");
        break;
    Using your code for case 1 you would get the following output:
    First Quarter
    First Quarter
    First Quarter
  • 3. Re: Need a little help with strings
    796365 Newbie
    Currently Being Moderated
    If you use an enum construct, then you can create a switch that operates on the abbreviated month value. Note that I made a correction in the Scanner input code, and converted the input to lowercase (to assure a match with the enum) in case capitals were used. The switch was revised to work correctly with enums, which requires the try/catch to catch illegal input.
    import java.util.Scanner;
    
    public class Xy
    {
        static Scanner console = new Scanner(System.in);
    
        enum Month
        {
            jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
        }
    
        public static void main(String[] args)
        {
            System.out.print("Enter a Month: ex. Jan, Feb, Mar etc...");
            String shortMonth = console.next().toLowerCase();
            System.out.println();
            try
            {
                switch (Month.valueOf(shortMonth))
                {
                    case jan:
                    case feb:
                    case mar:
                        System.out.println("First Quarter");
                        break;
                    case apr:
                    case may:
                    case jun:
                        System.out.println("Second  Quarter");
                        break;
                    case jul:
                    case aug:
                    case sep:
                        System.out.println("Third Quarter");
                        break;
                    case oct:
                    case nov:
                    case dec:
                        System.out.println("Fouth Quarter");
                }
            }
            catch (IllegalArgumentException iae)
            {
                System.out.println("Sorry, input is invalid");
            }
        }
    }
  • 4. Re: Need a little help with strings
    843789 Newbie
    Currently Being Moderated
    Tanks for de fish, mon.
  • 5. Re: Need a little help with strings
    843789 Newbie
    Currently Being Moderated
    \me waits to see what culinary delight BDLH makes with the fish.

    Gawd I hope it wasn't a relative!
  • 6. Re: Need a little help with strings
    843789 Newbie
    Currently Being Moderated
    [Fish-head steamboat!|http://www.thelocalking.com/singapore/eat--drink/nan-hua-chong-fishhead-steamboat.html]