The Mother Board

Motherboards.org forums. Free tech support, motherboard ID, and more.
It is currently Wed Sep 19, 2018 9:05 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: spacing in java
PostPosted: Sun Feb 13, 2005 1:52 pm 
Offline
Black Belt
Black Belt

Joined: Mon Apr 12, 2004 2:32 pm
Posts: 851
Location: Chicago
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);

       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;
   }
  }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group