5 Replies Latest reply: Mar 15, 2008 11:13 PM by 807601 RSS

    Help with string and int mismatch???

    807601
      I wrote a text based calulator for class and now for extra credit I'm supposed to give it a GUI "Look". Input/Output boxes is acceptable and thats the route i chose. I am having trouble with a string and int mismatch, I cant get past the first input box cuase if i name it operation i get an error. The way it is written now i get the first input box and then the output and it kips the case switch altogether. Thanks
       while(operation!=999)
               {
               inputStr=JOptionPane.showInputDialog
                     ("Enter 0 for help,9 to exit,or enter the number for the operation in which to be processed");
                 
                  
                  
               
                  switch(operation)
                  {
        • 1. Re: Help with string and int mismatch???
          807601
          Could you post a bit more of the code, or an error message? As it is, I don't see that there is enough information to solve your problem.
          • 2. Re: Help with string and int mismatch???
            807601
            Here is the entire program. Written like this there are no errors. IF i were to change expression to operation, which i think would make it work I get the following error.




            calculatorGUI.java:37: incompatible types
            found   : java.lang.String
            required: int
             
              import java.util.*;
               import javax.swing.JOptionPane;
               import java.io.*; 
               import javax.swing.*;
                 
                
                public class calculatorGUI   extends JFrame
               {
                  static Scanner console=new Scanner(System.in);
                  private static final int WIDTH=1200;
                  private static final int HEIGHT=1600;
                  
                  {
                     setSize(WIDTH,HEIGHT);
                     setVisible(true);
                     setDefaultCloseOperation(EXIT_ON_CLOSE);
                    
                  }
               
                   public static void main(String[]args)
                  {
                     calculatorGUI calcGUI=new calculatorGUI();
                       
                  
                     int num=0,num1=0,num2=0,num3=0,result=0;
                     int operation=0;
                     int counter=0,current;
                     String Soperation;
                     String expression;
                     String inputStr;
                     String outputStr;
                  
                  
                     while(operation!=999)
                     {
                     expression=JOptionPane.showInputDialog
                           ("Enter 0 for help,9 to exit,or enter the number for the operation in which to be processed");
                       
                        
                        
                     
                        switch(operation)
                        {
                        
                           case'1':
                              
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the first number");
                              num1=Integer.parseInt(inputStr);
                           
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the second number");
                              num2=Integer.parseInt(inputStr);
                                
                              result=num1+num2;
                              break;
                            
                           case'2':
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the first number");
                              num1=Integer.parseInt(inputStr);
                           
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the second number");
                              num2=Integer.parseInt(inputStr);
                             
                              result=num1-num2;
                              break;
                             
                           case'3':
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the first number");
                              num1=Integer.parseInt(inputStr);
                           
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the second number");
                              num2=Integer.parseInt(inputStr);
                                                      
                              result=num1*num2;
                              break;
                           
                           case'4':
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the first number");
                              num1=Integer.parseInt(inputStr);
                           
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the second number");
                              num2=Integer.parseInt(inputStr);
                           
                              if(num2==0)
                              {
                                 outputStr="Cannot Divide by Zero";
                                 
                                 JOptionPane.showMessageDialog(null,outputStr,
                                           "ERROR",
                                               JOptionPane.ERROR_MESSAGE);
                              }
                           
                              result=num1/num2;
                              break;
                          
                           case'5':
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter a number");
                              num1=Integer.parseInt(inputStr);
                              java.lang.Math.sqrt(num1);
                             
                              result=(num1);
                           
                              break;
                             
                           case'6':
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the first number");
                              num1=Integer.parseInt(inputStr);
                           
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the second number");
                              num2=Integer.parseInt(inputStr);
                              
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter the third number");
                              num3=Integer.parseInt(inputStr);
                                
                                
                              if(num3==1)
                                 result=num1;
                              else if(num3==2)
                                 result=num2;
                              else
                              {
                                 counter=3;
                                 while(counter<=num3)
                                 {
                                    result=num2+num1;
                                    num1=num2;
                                    num2=result;
                                    counter++;
                                 }
                              }
                              break;
                           
                           case'7':
                              int fact=1;
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter a number for factoring");
                              num=Integer.parseInt(inputStr);
                              for(num1=1;num1<=num;num1++)
                              {
                                 fact=fact*num1;
                              }
                           
                              result=fact;       
                              break;
                             
                           case'8':
                              inputStr=JOptionPane.showInputDialog
                                 ("Please enter a number for calculation");
                              num=Integer.parseInt(inputStr);
                              num=-num;
                              result=num;
                              break;
                                  
                           case'0':
                           
                           
                              outputStr=("Enter 0 for help, 9 to exit, 1 for addition, 2 for subtraction"+
                                                 "3 for multiplication, 4 fordivision, 5 for square root" +
                                                           "6 for fibonacci, 7 for factorial, and 8 for square root");
                                                           
                                                               
                              JOptionPane.showMessageDialog(null,outputStr,
                                           "Calculator",
                                                 JOptionPane.INFORMATION_MESSAGE);
                           
                              break;
                                       
                           case'9':
                              System.exit(0);
                              break;
                         
                        }
                        
                          
                                        
                        outputStr="The result is:"+result+"\n";
                        
                        JOptionPane.showMessageDialog(null,outputStr,
                                           "Calculator",
                                                     JOptionPane.INFORMATION_MESSAGE);
                        System.exit(0);
                     
                     }
                            
                         
                  }
               }
            • 3. Re: Help with string and int mismatch???
              807601
              You are looking to change the first letter of a String into a char. Have a look at the String API and see if there is a method that returns the char at a given location, a given index on the string. Use this to translate your expression into the operation char.

              (hint: look here:
              http://java.sun.com/javase/6/docs/api/java/lang/String.html#charAt(int)
              )

              Edited by: Encephalopathic on Mar 15, 2008 8:50 PM
              • 4. Re: Help with string and int mismatch???
                807601
                Im confused wouldnt i need to convert the string to an int or vice versa
                • 5. Re: Help with string and int mismatch???
                  807601
                  rmabrey wrote:
                  Im confused wouldnt i need to convert the string to an int or vice versa
                  Look closely: What is your switch statement using? ints or chars? That's what you need to translate your String to.