Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java charAt() help

    hello guys,
    im basically trying to get the user to enter a function in the following format:

    operator-number1-operator2-number2-number3

    then my programs supposed to build it into the following format:

    number1-operator1-(-number2-operator2-number3-)-=answer

    i get the user to enter a string variable called function.

    the problem occurs when i try and retrive the number values by using the
    function.charAt(0-4) method. instead of giving me the numerical methods i am receiving the ASCII or hex (not quite sure) for each number,.

    when i enter the function:*9-72
    i get : 57*(55-50)

    can anyone help.
    my code is below:
    Code:
    //assignment 1
    //Computer Architecture
    //create a program which simulates a microprogram
    import java.util.*;
    public class Assignement_1
    {
        public static void main(String[] args)
        {   
           //declaring variables
           String function = new String();
       
           
           System.out.println("Please enter the required function: ");
           function = EasyIn.getString();
           
          char operator1 = function.charAt(0);
          char firstnum = function.charAt(1);
          char operator2 = function.charAt(2);
          char secondnum = function.charAt(3);
          char thirdnum = function.charAt(4);
         
         int num1=0,
         num2=0,
         num3=0;
         
         num1 = firstnum;
         num2 = secondnum;
         num3 = thirdnum;
         
         System.out.print("The function you entered is: ");
         System.out.print(firstnum);
         System.out.print(operator1);
         System.out.print("(");
         System.out.print(secondnum);
         System.out.print(operator2);
         System.out.print(thirdnum);
         System.out.println(")");
         
         System.out.println(num1 +" "+num2+" "+num3);
         
         double result1 = 0;
        
        if (operator2 == '+'){
            result1 = num2 + num3;
        
        }
        if (operator2 == '-'){
            result1 = num2 - num3;
             
        }
        if (operator2 == '*'){
            result1 = num2 * num3;
             
        }
        if (operator2 == '/'){
            result1 = num2 / num3;
             
        }
        
        double result2=0;
         if (operator1 == '+'){
            result1 = num1 + result1;
        }
        if (operator1 == '-'){
            result1 = num1 - result1;
        }
        if (operator1 == '*'){
            result1 = num1 * result1;
        }
        if (operator1 == '/'){
            result1 = num1 / result1;
        }
        System.out.println(result2);
        System.out.println(result1);
         }
     }
    c:\dos...
    c:\dos\run...
    run dos run

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Look into the Integer.parseInt() method.
    If your numbers are always single digits you could also just subtract '0' from the value you have (num1 = firstnum - '0' which will subtract the ASCII value of '0' from the number and leave you with the digit's value.

    shmoove

  • #3
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cool,
    i kindof did the same thing as the zero thing but instead took 48 (ASCII value of 0) away from each of the numbers.
    thanks for the help though.
    c:\dos...
    c:\dos\run...
    run dos run


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •