Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 9th, 2015, 08:58 AM
Registered User
 
Join Date: Jan 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile calculator

Hi I'm a big problem:
Write a program that computes the sum and difference of n numbers entered from the terminal ;
the numbers and operators are separated from each other by one or more spaces .
eg : 1 + 3-4 + 12 +2 - 10-1
and must be printed : 3
Now my program is:
Code:
import java.util.StringTokenizer;
import java.util.Scanner;
class Calcolatore{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String numeri=sc.nextLine();
StringTokenizer st = new StringTokenizer(numeri," +");
StringTokenizer sk = new StringTokenizer(numeri," -");
int add=0;
while(st.hasMoreTokens()){
         String f=st.nextToken();
         String g=sk.nextToken();
         int n=Integer.parseInt(f);
         int z=Integer.parseInt(f);
       if(f.equals(" +")){
		  add=add+n; 
	   }else if(g.equals(" -")){
	   add=add-n; 
   }
}
System.out.println(add);
}
}
where am I wrong?
thank you every one





Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Calculator JustSk8n BOOK: Ivor Horton's Beginning Visual C++ 2010 1 March 28th, 2012 10:27 AM
Calculator AdamPembs BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 January 31st, 2010 02:48 PM
calculator mojtaba rashidi Visual Studio 2005 0 March 17th, 2008 07:29 AM
Calculator DweeLer Other Programming Languages 1 November 18th, 2005 08:13 AM
calculator kale_tushar C++ Programming 1 January 28th, 2004 01:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.