Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 06:33 PM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
Help with spacing in java
Hi, I have a completed java program that takes an expression and solves it. The program works fine except i need to print the final answer like this:2+2=4 and it doesn't. It puts the answer on a different line.
Does anyone know what i did wrong?
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; } }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 09:03 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
The readLine inside of Calc1.getInput() gets the line feed as part of the input. Try changing it to this Calc1.getInput().trim()

That will trim off the whitespace.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 06:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org