LiveWire Peer Support Network

Printable Version of Topic "JAVA help please"

- LiveWire Teen Forums & College Forums (http://www.golivewire.com)
-- (http://www.golivewire.com/forums/support-technical.html)
--- Programming & Application Development (http://www.golivewire.com/forums/forum-211-s-0.html)
---- JAVA help please (http://www.golivewire.com/forums/peer-yyyiasy-support-a.html)


-- Posted by crazyjenn791 at 4:56 pm on Sep. 15, 2008

this is the end of my program...:
import java.util.Scanner;

public class EnglishLanguage

{
public static void main(String[]args)
{
Scanner kb = new Scanner(System.in);

//Prompt user for a single digit, an operand and another simgle digit combination.
System.out.println("Enter an operation: ");

int operand1 = kb.nextInt();
// validate operand1
// if operand1 is not a single digit display an error message and exit
if( operand1 < 0 || operand1 > 9)
{
System.out.println("Error: Operand must be a single digit");
System.exit(0);
}
// operand1 is valid

// read an operator
String operatorStr = kb.next();
char operator = operatorStr.charAt(0);
// validate the operator
if( operator != '+' && operator != '-' && operator != '*' && operator != '/' && operator != '^')
{
System.out.println("Error: illegal operator");
System.exit(0);
}

int operand2 = kb.nextInt();

// validate operand2
// if operand2 is not a single digit display an error message and exit
if( operand2 < 0 || operand1 > 9)
{
System.out.println("Error: Operand must be a single digit");
System.exit(0);
}
// operand1 is valid

//Translate each operator and operand into the english language.

String operand1English = "unknown number";
switch(operand1)
{
case 0: operand1English = "zero";  break;
case 1: operand1English = "one";   break;
case 2: operand1English = "two";   break;
case 3: operand1English = "three"; break;
case 4: operand1English = "four";  break;
case 5: operand1English = "five";  break;
case 6: operand1English = "six";   break;
case 7: operand1English = "seven"; break;
case 8: operand1English = "eight"; break;
case 9: operand1English = "nine";  break;
default: System.out.println("Error: Operands must be single digits and positive.");
System.out.println("Try again later");
System.exit(0);
}

String operatorEnglish = "unknown number";
switch(operator)
{
case '+': operatorEnglish = " plus ";            break;
case '-': operatorEnglish = " minus ";           break;
case '*': operatorEnglish = " times ";           break;
case '/': operatorEnglish = " divided by ";      break;
case '^': operatorEnglish = " to the power of "; break;
default: System.out.println("Error: Illegal operator");
System.out.println("Try again later");
System.exit(0);
}

String operand2English = "unknown number";
switch(operand2)
{
case 0: operand2English = "zero";  break;
case 1: operand2English = "one";   break;
case 2: operand2English = "two";   break;
case 3: operand2English = "three"; break;
case 4: operand2English = "four";  break;
case 5: operand2English = "five";  break;
case 6: operand2English = "six";   break;
case 7: operand2English = "seven"; break;
case 8: operand2English = "eight"; break;
case 9: operand2English = "nine";  break;
default: System.out.println("Error: Operands must be single digits and positive.");
System.out.println("Try again later");
System.exit(0);
}

if (operand2 == 0 && operator == '/')
{
System.out.println("Error. Operation cannot be divided by zero.");
System.exit(0);
}

int result = -999;
switch(operator)
{
case '+': operatorEnglish = " plus ";            break;
case '-': operatorEnglish = " minus ";           break;
case '*': operatorEnglish = " times ";           break;
case '/': operatorEnglish = " divided by ";      break;
case '^': operatorEnglish = (int) Math.pow(operand1, operand2) ; break;
default: System.out.println("Error: Illegal operator");
System.out.println("Try again later");
System.exit(0);
}

// compute results of the operation
System.out.println((operand1English + operatorEnglish + operand2English) + "= " + result);


}

}

when i compile it, it says:  

incompatible types  
found : int
required: java.lang.String
case '^': operatorEnglish = (int) Math.pow(operand1, operand2) ; break;

what do i do,


-- Posted by steven7 at 4:57 pm on Sep. 15, 2008

go to w3schools.com
best help anywhere


-- Posted by h a t t at 4:58 pm on Sep. 15, 2008

nevermind.

change

case '^': operatorEnglish = (int) Math.pow(operand1, operand2) ; break;

to

case '^': operatorEnglish = Integer.toString( (int) Math.pow(operand1, operand2) ) ; break;


-- Posted by crazyjenn791 at 5:03 pm on Sep. 15, 2008

dude if you dont know what i`m talking about, bud out!


www.golivewire.com