0

I just started learning myself Java. Thought of writing a "Switch case" program like to get a input string from user and thought of displaying the day. Even though it did not show any errors in my Eclipse IDC but my below program is not running.

Could someone tell me what is the error in my below program?

package MyExercies;

import java.util.Scanner;

public class sampleSwitchCase 

{

    public static void main(String[] args) 
    {
        Scanner S = new Scanner(System.in);
        String Day = S.nextLine();
        int weekday = Integer.valueOf(Day);
        S.close();      
        switch(weekday)
        {
        case 1:     
            System.out.println ("The given day is Week begining day - Monday");
            break;

        case 2:
        case 3:
        case 4:
            System.out.println ("The given day is Mid of Weekday");
            break;

        case 5:
            System.out.println ("The given day is Weekend - Friday");
            break;

        case 6:
        case 7:
            System.out.println ("The given day is End of the Week");
            break;

        }

    }   

}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • 3
    You should say more about what “not running” means. – Bob Jacobsen Apr 28 '18 at 17:56
  • Please stick to naming conventions. Package names and variable names (as well as method names) should start with a lower case character. Class names with upper case characters. So `myExercise`, `SampleSwitchCase`, `s` and `day`. – Zabuzard Apr 28 '18 at 17:56
  • 1
    Posted code works here. Inefficient and non-standard variable names. But it works. – Elliott Frisch Apr 28 '18 at 17:56
  • 1
    This code is working – Naresh Bharadwaj Apr 28 '18 at 17:57
  • 2
    Are you sure it doesn't run and not just wait for your input? Try printing something before your scanner so that you see if it ever starts. – JussiV Apr 28 '18 at 17:57
  • Please don't close a something tied to the resource `System.in`. The stream was not opened by you but by the JVM. The one that opens a resource is responsible for closing it, so the JVM will close `System.in`. – Zabuzard Apr 28 '18 at 17:57
  • Everything is good and working with your Code! – Abdo Bmz Apr 28 '18 at 18:05
  • You need to enter a numerical value from 1 to 7 within the **Console Window** for you to get the results you expect. Anything other than a number will generate a **NumberFormatException** since the **Integer.valueOf()** method can only accept string representations of either signed or unsigned numerical characters. In this case it may be better to use **Integer.parseInt()** and [here is why](https://stackoverflow.com/questions/508665/difference-between-parseint-and-valueof-in-java). The difference is `valueOf()` returns an `Integer`, and `parseInt()` returns an `int` (a primitive type). – DevilsHnd - 退職した Apr 28 '18 at 18:28

3 Answers3

-1

Everything is good and working with your Code!

Just add :

 default :
            System.out.println("Write something default  here");
Abdo Bmz
  • 632
  • 1
  • 11
  • 24
  • Thanks for your prompt response. Added default section but still it is running without returning any result like showing red icon/button in my "Eclipse IDC". – srinivasan pal May 03 '18 at 12:06
-1

add default case in your code and try like this :

switch(weekday)
        {
        case 1:     
            System.out.println ("The given day is Week begining day - Monday");
            break;
        default:
            System.out.println("default case");
Abdo Bmz
  • 632
  • 1
  • 11
  • 24
Kaka
  • 1
  • 5
  • Thanks for your prompt response. Added default section but still it is running without returning any result like showing red icon/button in my "Eclipse IDC". – srinivasan pal May 03 '18 at 12:07
-2

Add a default case in your switch block. For example:

switch(exp) {
   case 1:
      break;
   default:
      break;
}
pushkin
  • 9,575
  • 15
  • 51
  • 95