0

Orphaned Case Error The following are the tasks which will be done in the program:-

  1. Accept Deposit from a customer and update the balance.
  2. Display the balance.
  3. Compute and deposit the compound interest.
  4. Permit withdrawal and update the balance.
  5. Check for the minimum balance, impose penalty, if necessary and update the balance

I am getting an "orphaned case" error for

case1: S1.Display();

case1: S2.Display();

Please help. This is the Code:

import java.util.*;

class Account

{
 String custname;

 double accno;

 double bal;


 Account(String c, double a, double b)

 {custname = c;

  accno = a;

  bal = b;
 }



 void Display()

 {System.out.println("Account Holder: "+custname);

  System.out.println("Account Number "+accno);
  System.out.println("Balance : "+bal); 
 }



 void Deposit()

 {double dep;

  Scanner sc = new Scanner(System.in);

  System.out.println("Please Enter the amount your want to deposit:");

  dep = sc.nextDouble();

  bal = bal + dep;

  System.out.println("Updated Details....");

  Display();
 }


 void Withdraw()

 {double wth;

  Scanner sc = new Scanner(System.in);

  System.out.println("Please enter the amount you want to withdraw");

  wth = sc.NextDouble();

  bal = bal - wth;

  System.out.println("Updated details....");

  Display();

 }

}



class SavAccount extends Account

{ String acctype;


  SavAccount(String c, double a, double b)
 {Super(c, a, b);

  acctype = "Savings";

 }



 void ComInt()

 {int months;

  Scanner sc = new Scanner(System.in);

  System.out.println("Please enter the duration of the account in months");

  months = sc.NextInt();

  double rate, inte;
  rate = 0.04;

  inte = months *rate * bal;

  bal = bal + inte;

  System.out.println("Compund Interest : "+inte);
 }



 void Display()

 {System.out.println("Account Holder: "+custname);

  System.out.println("Account Number "+accno);

  ComInt();

 System.out.println("Balance : "+bal);

  System.out.println("Account Type: "+acctype);

 }

}



class CurAccount extends Account

{String acctype;

 CurAccount(String c, double a, double b)

 {Super(c, a, b);

  acctype = "Current";

 } 


 void Penalty()
 { 
  if(bal<5000.00)

  {bal = bal - 100.00;
   System.out.println("Fine deducted Rs.100/-");

  }
 }

 void Display()

 {System.out.println("Account Holder: "+custname);

  System.out.println("Account Number "+accno);

  Penalty();

 System.out.println("Balance : "+bal);

  System.out.println("Account Type: "+acctype);

  if(bal<=5000.00)

  {System.out.println("Warning!! Please maintain balance above Rs.5000/-");

  }
 }


}
class Accmain
{public static void main(Strings args[])

 {SavAccount S1 = new SavAccount("Aniruddha", 134.00, 15000.00)
;
  CurAccount S2 = new CurAccount("Tyrel" , 135.00, 6000.00);

  int num = 2;


  String c = "y";

  int n;

  double temp;
  double accs[] = new double[10];

  accs[0] = S1.accno;

  accs[1] = S2.accno;

  Scanner sc = new Scanner(System.in);

  while (c == "y");

  {System.out.println("Please enter your Account number:");

   temp = sc.nextDouble();

   if(accs[0] == temp)

   {System.out.println("Welcome "+ S1.custname);

    System.out.println("Account Type: "+ S1.acctype);

    System.out.println("1.Display");

    System.out.println("2.Withdraw");

    System.out.println("3.Deposit");

    n = sc.nextInt();

    Switch(n)
;
    {
     case 1 : S1.Display();

     case 2 : S1.Withdraw();

     case 3 : S1.Deposit();

    default :System.out.println("Bad Choice ");

     c = "n";

   }

   }
   else if(accs[1] == temp)

   {System.out.println("Welcome "+ S2.custname);

    System.out.println("Account Type: "+ S2.acctype);

    System.out.println("1.Display");

    System.out.println("2.Withdraw");

    System.out.println("3.Deposit");

    n = sc.nextInt()
;
    Switch(n);
    {
     case 1 : S2.Display();

     case 2 : S2.Withdraw();

     case 3 : S2.Deposit();
     default :System.out.println("Bad Choice ");

     c = "n";
    }

   }

  } 

 }



} 
Anirudh Kulkarni
  • 19
  • 1
  • 1
  • 1

4 Answers4

2

Several problems.

1) That extra ; in the end made the switch completed right away on that stamtement.

Switch(n); <--

Apparently all the cases became orphans.

If you remove the colon after Switch(n) you would be fine.

 Switch(n)
    {
     case 1 : S1.Display();

     case 2 : S1.Withdraw();

2) After that you have another problem. You need to have break after each case. Otherwise your switch executes all the cases even though the match found. To stop that add break after each case

  Switch(n)
        {
         case 1 : S1.Display(); break;

         case 2 : S1.Withdraw();break;
          ...

3) When you write c == "y" That compare references not equality.

you need to use equals() method to check string equality

read How do I compare strings in Java?

4) That line Super(c, a, b); won't compile as S must be lower-case. Java is case sensitive.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

The two problems you're hitting are:

Switch(n) // <-- switch is not spelled with a capital 's'
; // <-- remove the semicolon
    {
     case 1 : S1.Display();

     case 2 : S1.Withdraw();

     case 3 : S1.Deposit();

    default :System.out.println("Bad Choice ");

     c = "n";

   }

Since the cases appear without any relation to a switch - you're getting the "Orphaned Case Error". Further, when you're done handling each case you should break; otherwise the code will continue executing the following cases as well!

I would also advise you to use a good IDE and auto-indent the code because the way it is currently indented makes it very difficult to see where an if or else clauses ends.

A good IDE (IntelliJ/Eclipse/Netbeans) will also show you all the compilation errors you have, for example Super(c, a, b) - the 's' should not be capitalized, and etc.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

This error usually meas that you are trying to use a case keyword outside of a switch statement.

    Switch(n)
;

here after switch(n) remvoe the ;

also use break statement after each case operations. else it will keep executing the next case operations.

other minor issues are mentioned in Sures's answer

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

case 1:System.out.println("1111111111111111"); some example case 1:System no spaces in statement

example case 1:System.out.println("Display the vales");