All pastes #247577 Raw Edit

Unnamed

public text v1 · immutable
#247577 ·published 2006-11-13 17:47 UTC
rendered paste body
// calculator

import java.util.Scanner;

public class CalcX {

     public static void main (String[] args) {

       Scanner keyboard = new Scanner (System.in);

       String sInput;
       char c0perator = 56;
       int indexAdd, indexMin, indexMul, indexDiv, index1, index2, indexOperator;
       double answer = 0, firstNo = 0, secondNo = 0;

       System.out.println("Enter a calcutation: ");
       sInput = keyboard.nextLine();
       System.out.println("");
                    
       indexAdd = sInput.indexOf('+');
       indexMin = sInput.indexOf('-');
       indexMul = sInput.indexOf('*');
       indexDiv = sInput.indexOf('/');
              
       index1 =(indexAdd>indexMin) ? indexAdd: indexMin;            //Index1 = indexAdd if greater...
       index2 =(indexMul>indexDiv) ? indexMul: indexDiv;            //Index2 = indexMul if greater...
       indexOperator = (index1>index2) ? index1: index2;           //IndexOperator = index if greater...

       if (indexOperator < 0) {                                     // if operator was not found then
           System.out.println("ERROR!");
        } else {
            c0perator = sInput.charAt(indexOperator);                  

String[] array;

array = sInput.split("\\+");
System.out.println(array[0]+"+"+array[1]);

//array = sInput.split("-");
//System.out.println(array[0]+"-"+array[1]);
        
//array = sInput.split("/");
//System.out.println(array[0]+"/"+array[1]);
        
//array = sInput.split("\\*");
//System.out.println(array[0]+"*"+array[1]);             
	
//Splits the string where Operator is found
            
            String sFirstNo = array[0];
            String sSecondNo = array[1];
       
            sFirstNo = sFirstNo.trim();
            sSecondNo = sSecondNo.trim();
       
            firstNo = Double.parseDouble(sFirstNo);
            secondNo = Double.parseDouble(sSecondNo);
        }
            
       switch ( c0perator ) {
        case '+':       // Addition
            answer = firstNo+secondNo;
            break;
        case '-':       // Subtraction
            answer = firstNo-secondNo;
            break;
        case '*':       // Multiplication
            answer = firstNo*secondNo;
            break;
        case '/':       // Division
            answer = firstNo/secondNo;
            break;
        case 'E':       // Opperator not found
            System.out.println("Operator Not Found - Error!");
        default:        // Default error
            System.out.println("Format Operator Error: " + c0perator);
        }
        System.out.println("The answer is: " + answer);
 
    }
}