In my program i want to execute same statement on case 3 and 5 in switch() i searched lot on google about this but get nothing please help me

case 3,5:

I know it is wrong but if you have any idea about this than please help me.

it is possible to that by writing the same statements twice under case 3 and 5 separately but that's not what i want.

below is one class of my program i got one error in thid

duplicate case label at line 107
case 3:

code:

import java.util.*;

class current extends customer{
private double fd_amount[]=new double[2];
public byte fixdep_status=0;
private int fd_num[]=new int[2];
private int fd_time[]=new int[2];
static byte current_c=-1;
Scanner obj=new Scanner(System.in);

current(String name,byte type)
{
super(name,type);
}

void deposit(double rs)
{
gateway(2,rs);
}

void withd(double rs)
{
gateway(3,rs);
}

void details()
{
gateway(4,0);
}


void newfd(int month,double rs)
{
fd_amount[fixdep_status]=rs;
fd_num[fixdep_status]=++customer.fd;
fd_time[fixdep_status]=month;
fixdep_status++;

}

void fd_details()
{
for(int i=0;i<fixdep_status;i++)
{System.out.println("\n\n============================");
System.out.println("DETAILS OF F.D"+(i+1));
System.out.println("AMOUNT : "+fd_amount[i]);
System.out.println("TIME PERIOD : "+fd_time[i]);
System.out.println("F.D NUMBER : "+fd_num[i]);
}
}

void breakfd(byte choice)
{
double interest;
byte ch;
if(choice==3)
{
if(fixdep_status==1)
{
System.out.println("\n\nAMOUNT : "+fd_amount[fixdep_status-1]);
System.out.println("TIME PERIOD : "+fd_time[fixdep_status-1]);
System.out.println("F.D NUMBER : "+fd_num[fixdep_status-1]);
if(fd_time[fixdep_status-1]==6)
interest=(fd_amount[fixdep_status-1]*fd_time[fixdep_status-1]*5)/(100*12);
else
interest=(fd_amount[fixdep_status-1]*fd_time[fixdep_status-1]*10)/(100*12);
System.out.println("INTEREST GIVEN :"+interest);
System.out.println("______________________\nTOTAL AMOUNT DISPATCH : "+(fd_amount[fixdep_status-1]+interest));
fixdep_status--;
fd_amount[fixdep_status]=0;
fd_time[fixdep_status]=0;
fd_num[fixdep_status]=0;
}
if(fixdep_status==2)
{
System.out.println("YOUR FIX DEPOSITE DETAILS");
fd_details();
System.out.println("\n\n1 - TO BREAK F.D 1 (ABOVE)");
System.out.println("2 - TO BREAK F.D 2 (BELOW)");
System.out.println("3 - TO BREAK BOTH");
ch=obj.nextByte();


					switch(ch)
					{
					case 3:
					case 1:
					System.out.println("\n\nAMOUNT : "+fd_amount[fixdep_status-2]);
					System.out.println("TIME PERIOD : "+fd_time[fixdep_status-2]);
					System.out.println("F.D NUMBER : "+fd_num[fixdep_status-2]);
					if(fd_time[fixdep_status-2]==6)
					interest=(fd_amount[fixdep_status-2]*fd_time[fixdep_status-2]*5)/(100*12);
					else
					interest=(fd_amount[fixdep_status-2]*fd_time[fixdep_status-2]*10)/(100*12);
					System.out.println("INTEREST GIVEN :"+interest);
					System.out.println("______________________\nTOTAL AMOUNT DISPATCH : "+(fd_amount[fixdep_status-2]+interest));
					
					fd_amount[fixdep_status-2]=fd_amount[fixdep_status-1];
					fd_time[fixdep_status-2]=fd_time[fixdep_status-1];
					fd_num[fixdep_status-2]=fd_num[fixdep_status-1];
					fd_amount[fixdep_status-1]=0;
					fd_time[fixdep_status-1]=0;
					fd_num[fixdep_status-1]=0;
					fixdep_status--;
					break;
					
					case 3:
					case 2:
					System.out.println("AMOUNT : "+fd_amount[fixdep_status-1]);
					System.out.println("TIME PERIOD : "+fd_time[fixdep_status-1]);
					System.out.println("F.D NUMBER : "+fd_num[fixdep_status-1]);
					if(fd_time[fixdep_status-1]==6)
					interest=(fd_amount[fixdep_status-1]*fd_time[fixdep_status-1]*5)/(100*12);
					else
					interest=(fd_amount[fixdep_status-1]*fd_time[fixdep_status-1]*10)/(100*12);
					System.out.println("INTEREST GIVEN :"+interest);
					System.out.println("______________________\nTOTAL AMOUNT DISPATCH : "+(fd_amount[fixdep_status-1]+interest));
					fd_amount[fixdep_status-1]=0;
					fd_time[fixdep_status-1]=0;
					fd_num[fixdep_status-1]=0;
					fixdep_status--;
					}//end of switch
               }}}


}//end of class

i want if user press 3 it executes both cases 1 as well as 2 please help.

Edited 5 Years Ago by aman rathi: n/a

There is not a way for that to fall through both conditions on your switch. Use an if condition instead.

Switch statement is made to reduce if else statment, as u have only few condition ,y dont u use if else rather then switch... Still u want to use switch the in case 3 write both code of case 1 and case 2.

I'd also recommend re-writing the code to put the code you have in those switch cases into a function or use a variable for which array elements to operate on, because the only differece between them seems to be"fixdep_status-1" vs "fixdep_status-2".

There's no reason to repeat that much code.

Yup I agree with @Ezzaral, You must create seperate functions for each of the code which are executing under cases... and call both function in case 3. :)

Edited 5 Years Ago by Majestics: n/a

doing it by using if-else is better. idea of putting code in separate function is awesome. thanks to all for showing interest.

This question has already been answered. Start a new discussion instead.