I posted a code on this link http://www.daniweb.com/forums/post915333.html#post915333 and the thread was solved, but I'm looking forward for another method using switch case.... but an error says :
incompatible types
found: java.lang.String
required: int

is the re a possible way that forces switch case method to use string instead of int???

this was the code that I want to experiment:

import java.io.*;
 public class ifmdas3{
 public static void main(String[] args) throws IOException{
  BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  String option, firstnum, secondnum;// trims leading & trailing spaces
  int num1, num2, prod, quo, sum, diff;
  
  System.out.print("Choose An Operation");
  System.out.print("\nPress1 for Multiplication");
  System.out.print("\nPress2 for Division");
  System.out.print("\nPress3 for Addition");
  System.out.print("\nPress4 for Subtraction");
  System.out.print("\n\nOperation: ");
  option=stdin.readLine();
  String choice = option.trim();  
  /*if(choice.equalsIgnoreCase("m")){

  }else if(choice.equalsIgnoreCase("d")){
  
  }
  else if(choice.equalsIgnoreCase("a")){
  
  }
  else if(choice.equalsIgnoreCase("s")){
  
  }
  else
  {
 
  }*/
  switch("choice")
{

case 'm':
case 'M':
  System.out.print("\nEnter the first number");
  firstnum=stdin.readLine();
  System.out.print("\nEnter the second number");
  secondnum=stdin.readLine();
  num1=Integer.parseInt(firstnum);
  num2=Integer.parseInt(secondnum);
  prod=num1*num2;
  System.out.print("\nThe product is: "+prod);
break;
case 'd':
case 'D':
  System.out.print("\nEnter the first number");
  firstnum=stdin.readLine();
  System.out.print("\nEnter the second number");
  secondnum=stdin.readLine();
  num1=Integer.parseInt(firstnum);
  num2=Integer.parseInt(secondnum);
  if(num2==0){
  System.out.print("cannot perform the operation");
  }else{
  quo=num1/num2;
  System.out.print("\nThe quotient is: "+quo);
  }
  break;
case 'a':
case 'A':
  System.out.print("\nEnter the first number");
  firstnum=stdin.readLine();
  System.out.print("\nEnter the second number");
  secondnum=stdin.readLine();
  num1=Integer.parseInt(firstnum);
  num2=Integer.parseInt(secondnum);
  sum=num1+num2;
  System.out.print("\nThe sum is: "+sum);
break;
case 's':
case 'S':
  System.out.print("\nEnter the first number");
  firstnum=stdin.readLine();
  System.out.print("\nEnter the second number");
  secondnum=stdin.readLine();
  num1=Integer.parseInt(firstnum);
  num2=Integer.parseInt(secondnum);
  diff=num1-num2;
  System.out.print("\nThe diference is: "+diff);
break;




default:
 System.out.print("Invalid input for the choice of operation");
 break;
}
}
}

Recommended Answers

All 5 Replies

Use,

switch(choice.charAt(0)) {
..
}

Here you are confusing the code.
You are taking input as Integer.

System.out.print("Choose An Operation");
	System.out.print("\nPress1 for Multiplication");
	System.out.print("\nPress2 for Division");
	System.out.print("\nPress3 for Addition");
	System.out.print("\nPress4 for Subtraction");
	System.out.print("\n\nOperation: ");
	option = stdin.readLine();

User gonna press 1 to 4 as input. You will not get character.
If you want to do only with this way then put one if-else and set one char variable with appropriate value as per the input. else change the style to take input. But whole String will not work with switch statement.

System.out.print("\nEnter the first number");
	firstnum = stdin.readLine();

As well you are taking all input as string instead take as integer itself. AS you are taking numbers for calculation.

private String getDate(String setDate){
		String Date = "";
		int month = Integer.parseInt(setDate.substring(5, 7));
		int year =  Integer.parseInt(setDate.substring(0, 4));
		String day =  setDate.substring(8, 10);
		switch(month){
		case 1:
			Date = "January "+day+", "+year;
			break;
		case 2:
			Date = "February "+day+", "+year;
			break;
		case 3:
			Date = "March "+day+", "+year;
			break;
		case 4:
			Date = "April "+day+", "+year;	
			break;
		case 5:
			Date = "May "+day+", "+year;
			break;
		case 6:
			Date = "June "+day+", "+year;
			break;
		case 7:
			Date = "July "+day+", "+year;
			break;
		case 8:
			Date = "August "+day+", "+year;
			break;
		case 9:
			Date = "September "+day+", "+year;
			break;
		case 10:
			Date = "October "+day+", "+year;
			break;
		case 11:
			Date = "November "+day+", "+year;
			break;
		case 12:
			Date = "December "+day+", "+year;
			break;
		}
		return Date;
	}

And what are these last two post suppossed to mean?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.