473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Split string calculator

I have this code
Expand|Select|Wrap|Line Numbers
  1.  
  2.     import java.util.Scanner;
  3. public class Calculator {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.           Scanner scan = new Scanner(System.in) ;
  8.           System.out.println("Enter your two numbers and the operation with spaces between e.g 8 9 -");
  9.  
  10.           String calculation=scan.nextLine();
  11.  
  12.           String [] parts = calculation.split(" ");
  13.  
  14.            //Add
  15.             if (parts[2].equals("+")) {
  16.                 String ans = parts[0] + parts[1];
  17.             }
  18.  
  19.             //Subtract
  20.             else if (parts[2].equals("-")) {
  21.                 String ans = parts[0] - parts[1];
  22.             }
  23.  
  24.             //Multiply
  25.             else if (parts[2].equals("*")) {
  26.                 String ans = parts[0] * parts[1];
  27.             }
  28.  
  29.             //Divide
  30.             else if (parts[2].equals("/")) {
  31.                 String ans = parts[0] / parts[1];
  32.             }
  33.  
  34.     }
  35.  
  36. }
  37.  
the error is within the if statements regarding -, * and / operations "The operator / is undefined for the argument type(s) java.lang.String, java.lang.String" can anyone help
Oct 21 '15 #1
1 3717
chaarmann
785 Expert 512MB
"parts" is an array of Strings, not numbers!
You cannot subtract/divide/multiply strings, so you have to convert it to a number before:
Expand|Select|Wrap|Line Numbers
  1. String s="12";
  2. int i = Integer.parseInt(s);
Remark: you can add strings, but this is the concat operation. So "1"+"2" would give you "12" and not 3!

Also, the result of dividing an int with another int gives you an int, not a String.
You can convert the result to a String using the valueOf() method.
So in your case, change line 31 to:
Expand|Select|Wrap|Line Numbers
  1. String ans = String.valueOf(Integer.parseInt(parts[0]) / Integer.parseInt(parts[1]));
Oct 21 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
7
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th...
5
by: shaiful | last post by:
Hi all I have a simple problem with string. I want to split string, such as: dim s as string dim k(3) as string s="aa1,bv1,cc1,dt1" i want to split the value in k, k(0)=aa1 k(1)=bv1...
17
by: Max347 | last post by:
This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers...
4
by: dmitrey | last post by:
hi all, howto split string with both comma and semicolon delimiters? i.e. (for example) get from string "a,b;c" I have tried s.split(',;') but it don't work Thx, D.
4
by: N9 | last post by:
Hi Anyone who can help about split string. string text = "History about a boy, who loves to play baseball with his friends." I like to find indexOf "play" and read the string 10 char left...
3
xarzu
by: xarzu | last post by:
Tab Split String Not Working. Please Help. I wrote a C# program that reads code one line at a time. Each line has data seperated by tabs. Here is what my C# code is supposed to do. The line...
2
by: Mudassir | last post by:
Dear all, i want to split string after each 15 lines, suppose i have a string which contains multiple lines, i want to split that string after each 15 lines.. any help will be greatly appreciated
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.