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 2 of 2

Thread: spacing in java

  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    spacing in java

    I have a completed java program that takes an expression and solves it. The program works fine expept i need to print the final answer like this:2+2=4 and it doesnt. It puts the answer on a different line. Does anyone know what i did wrong?
    Here is the program:
    Code:
    import java.util.*;
    import java.io.*;
    
    public class Calc1
    {
    
      //prompts the user for input and calls necessary methods
      public static void main(String args[])
      {
        String cont = "";
    
        do
        {
          System.out.println("Please enter a numeric expression.");
          StringTokenizer exp = new StringTokenizer(Calc1.getInput(), "+-*/%", true);
    
           //int result = Calc1.performOperation(exp);
           System.out.println("" + Calc1.getInput() + "=" + Calc1.performOperation(exp));
          System.out.print("Do you want to enter another expression? (y/n): ");
          cont = Calc1.getInput();
        }while(cont.equalsIgnoreCase("y"));
    
         System.out.println("Goodbye");
      }
    
       //gets a single input from the user, and returns it as a String
      static String getInput()
      {
        java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
        java.io.BufferedReader console = new java.io.BufferedReader(input);
    
        try
        {
          return console.readLine();
        }
        catch(java.io.IOException e)
        {
          System.err.println("Bad NEWS!!!");
          return "";
        }
      }
    
      //performs requested operation
      static int performOperation (StringTokenizer exp)
      {
        int num1 = 0;
        int result = 0;
        String check = "";
        String sve = "+";
    
        do
        {
          check = exp.nextToken();
          if(!check.equals("+") && !check.equals("-") && !check.equals("*") && !check.equals("/") && !check.equals("%"))
          {
            num1 = Integer.parseInt(check);
    
            if(sve.equals("+"))
            {
              result = result + num1;
              num1 = 0;
            }
    
            else if(sve.equals("-"))
            {
              result = result - num1;
            }
    
            else if(sve.equals("*"))
            {
              result = result * num1;
            }
    
            else if(sve.equals("/"))
            {
              result = result / num1;
            }
    
           else if(sve.equals("%"))
           {
             result = result % num1;
           }
          }
    
          else
          {
            sve = check;
          }
        }while(exp.hasMoreTokens());
    
        return result;
       }
      }

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    That's happening because you call Calc1.getInput several times, when you should only call it once. I changed it so that it gets the input in a string (s) and then s is used throughout the rest of the code where you were using getInput().

    Hope this helps,
    Kevin


    import java.util.*;
    import java.io.*;

    public class Calc1
    {

    //prompts the user for input and calls necessary methods
    public static void main(String args[])
    {
    String cont = "";

    do
    {
    System.out.println("Please enter a numeric expression.");

    String s = Calc1.getInput();

    StringTokenizer exp = new StringTokenizer(s, "+-*/%", true);

    //int result = Calc1.performOperation(exp);

    System.out.println("" + s + "=" + Calc1.performOperation(exp));
    System.out.print("Do you want to enter another expression? (y/n): ");

    cont = Calc1.getInput();

    }while(cont.equalsIgnoreCase("y"));

    System.out.println("Goodbye");
    }

    //gets a single input from the user, and returns it as a String
    static String getInput()
    {
    java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
    java.io.BufferedReader console = new java.io.BufferedReader(input);

    try
    {
    return console.readLine();
    }
    catch(java.io.IOException e)
    {
    System.err.println("Bad NEWS!!!");
    return "";
    }
    }

    //performs requested operation
    static int performOperation (StringTokenizer exp)
    {
    int num1 = 0;
    int result = 0;
    String check = "";
    String sve = "+";

    do
    {
    check = exp.nextToken();
    if(!check.equals("+") && !check.equals("-") && !check.equals("*") && !check.equals("/") && !check.equals("%"))
    {
    num1 = Integer.parseInt(check);

    if(sve.equals("+"))
    {
    result = result + num1;
    num1 = 0;
    }

    else if(sve.equals("-"))
    {
    result = result - num1;
    }

    else if(sve.equals("*"))
    {
    result = result * num1;
    }

    else if(sve.equals("/"))
    {
    result = result / num1;
    }

    else if(sve.equals("%"))
    {
    result = result % num1;
    }
    }

    else
    {
    sve = check;
    }
    }while(exp.hasMoreTokens());

    return result;
    }
    }


  •  

    Posting Permissions

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