0
import java.util.Scanner;
public class SwitchCase {

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        String ch="NULL";
        do

        {
            System.out.println("Enter your choice:");
            String str=sc.next();
            switch(str)
          {

            case "Add":
            {
            System.out.println("Enter 2 nos:");
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=a+b;
            System.out.println("Adiition is:"+c);
            break;
            }

            case "Sub":
            {
                System.out.println("Enter 2 nos:");
                int a1=sc.nextInt();
                int a2=sc.nextInt();
                int a3=a1-a2;
                System.out.println("Sub is:"+a3);
                break;

            }
            case "Multiply":
            {
                System.out.println("Enter 2 nos:");
                int a1=sc.nextInt();
                int a2=sc.nextInt();
                int a3=a1*a2;
                System.out.println("Sub is:"+a3);
                break;
            }

            case "Divide":
            {
                System.out.println("Enter 2 nos:");
                int a1=sc.nextInt();
                int a2=sc.nextInt();
                int a3=a1/a2;
                System.out.println("Sub is:"+a3);
                break;
            }
            case "Fib":
            {
                System.out.println("Enter the range:");
                int n=sc.nextInt();

                 if (n == 0)
                 {
                     System.out.println("0");
                 } 
                 else if (n == 1) 
                 {
                       System.out.println("0 1");
                 }
                 else 
                 {
                       System.out.print("0 1 ");
                       int a = 0;
                       int b = 1;
                       for (int i = 1; i < n; i++)
                       {
                           int nextNumber = a + b;
                           System.out.print(nextNumber + " ");
                           a = b;
                           b = nextNumber;
                       }            
                 }
        }
    }
        System.out.println("Do you want to continue? y or n");
      ch=sc.next();
        }           while(ch=="y");

    }

}

After the while loop,when "y" is entered by user,it should ask "Do you want to continue" but the same does not happen.I debugged the code,it runs fine till "Do you want to continue?" But after that :

source not found error comes, when debugged in eclipse

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Ribs
  • 21
  • 2
  • @piet.t Nice catch, but this code shouldn't even compile from what I can see. `source not found` ... this sounds like the code in your IDE is out of sync with what you are running. – Tim Biegeleisen Sep 05 '16 at 12:09
  • There is probably another bug hidden in here. `sc.nextInt()` will pull integers from STDIN, but will leave the trailing newline character to be eaten immediately after by the `sc.next()` call after your "Do you want to continue?" question. – Phylogenesis Sep 05 '16 at 12:12
  • @Phylogenesis This is wrong. `next()` doesn't read delimiters. What you mean is `nextLine()`. – Tom Sep 05 '16 at 12:13
  • @Tom Yeah. I am thinking of `sc.nextLine()` that always seems to pop up on questions here when combined with `sc.nextInt()`. – Phylogenesis Sep 05 '16 at 12:17
  • @Phylogenesis Btw `next()` also leaves the trailing delimiter in the stream, so `next()` -> `nextLine()` also causes problems :D. – Tom Sep 05 '16 at 12:18

2 Answers2

2
while(ch.equals("y"));

Use equals instead of operator "==".

Check Oracle tutorial: Comparing Strings

Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
kunpapa
  • 367
  • 1
  • 9
  • 1
    Don't post answers to obivous duplicates. This kind have been asked thousands of time and voting to close as duplicate is the correct way to handle it. – Tom Sep 05 '16 at 12:12
0

In the while condition you are comparing ch=="y". When comparing strings you should use the equals method like this "y".equals(ch). The equality operator will test if the two values have the same reference value, which obviously, they don't.

Slimu
  • 2,301
  • 1
  • 22
  • 28
  • 2
    Don't post answers to obivous duplicates. This kind have been asked thousands of time and voting to close as duplicate is the correct way to handle it. – Tom Sep 05 '16 at 12:12