Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,545 members, 7,809,005 topics. Date: Thursday, 25 April 2024 at 08:59 PM

Java Code Pls Modify This Code Help - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Code Pls Modify This Code Help (1527 Views)

Please Someone Should Help Me With This Java Code / I Need A Good PHP Programmer To Modify My Site (payment Involved) / If You Can Modify This Php Source Code Drop Your Contact (2) (3) (4)

(1) (Reply) (Go Down)

Java Code Pls Modify This Code Help by lincsnuel: 9:32pm On Dec 23, 2018
Hi, Pls I coded a very basic Calculator in Java. The Calculator does basic addition, subtraction, division and multiplication but the problem is:

When I do something like "56+23" the output is normal but when I do something like "23+77-8*45" it throws a "java.lang.NumberFormatException: For input string: "62*7""

I'm so freaking tired of the code. Dunno what to do.

Re: Java Code Pls Modify This Code Help by Simpubozz(m): 10:39pm On Dec 23, 2018
where's the code?
Re: Java Code Pls Modify This Code Help by makavele: 11:57pm On Dec 23, 2018
Well I don't speak Java so don't crucify me. You are getting the NumberFormatException because when parsing the codes, it sees that the string entered is not numeric. If you are are expecting an integer and a float is passed e.g "59.0" in your case, it will throw an exception.
Or you are passing a float, parser isn't seeing it as numeric too.
You need to remove the "decimal float" by using Double.parseDouble function and type-hint it as an integer.
and vice-versa if working for a float.

Use:
variable = (int) Double.parseDouble(............) // where variable and (....) are editable


Or handle the exception:

try{
int variable = Integer.parseInt(input);
}catch(NumberFormatException ex){
//***some line(s) of code***
}


Cheers.

1 Like

Re: Java Code Pls Modify This Code Help by lincsnuel: 8:23am On Dec 24, 2018
makavele:
Well I don't speak Java so don't crucify me. You are getting the NumberFormatException because when parsing the codes, it sees that the string entered is not numeric. If you are are expecting an integer and a float is passed e.g "59.0" in your case, it will throw an exception.
Or you are passing a float, parser isn't seeing it as numeric too.
You need to remove the "decimal float" by using Double.parseDouble function and type-hint it as an integer.
and vice-versa if working for a float.

Use:
variable = (int) Double.parseDouble(............) // where variable and (....) are editable


Or handle the exception:

try{
int variable = Integer.parseInt(input);
}catch(NumberFormatException ex){
//***some line(s) of code***
}


Cheers.

Yes but I already used the parseDouble(String s) method to parse the string to double. I did something like: if ((result%1) == 0) { int newResult = (int) result;
txtField.setText(newResult);
else { txtField.setText(result); /**display the result as "double" if result has decimal else if result is integer, display as int to remove the .0 in say 51.0**/

I want it to do multiple calculations at once like multiple addition, subtraction etc. Without displaying that NumberFormatException

Thanks tho
Re: Java Code Pls Modify This Code Help by Kamxin(m): 11:35am On Dec 24, 2018
Why not paste the code for the input string so we can see how you declared it.

Because to me, the problem here is that the calculation code is seeing string as one of your input and string cannot be used for calculation in Java. perhaps your conversion is belated
Re: Java Code Pls Modify This Code Help by etoluw: 5:06pm On Dec 24, 2018
it is very difficult to help you without seeing the full code

e.g i don't know what jString is but i assume is something like

String[] jString ={"+", "-", "*", "/" );

also your from ur code it seems it can only calculate two variables(i.e num1 & num2) anything else will cause an exception

But just as i said earlier until we see d codes there is really nothing we can do to help
Re: Java Code Pls Modify This Code Help by lincsnuel: 6:03pm On Dec 24, 2018
etoluw:
it is very difficult to help you without seeing the full code

e.g i don't know what jString is but i assume is something like

String[] jString ={"+", "-", "*", "/" );

also your from ur code it seems it can only calculate two variables(i.e num1 & num2) anything else will cause an exception

But just as i said earlier until we see d codes there is really nothing we can do to help

Yea you're correct abt the jString... It's an array that contains those symbols. I extracted the first number before +,-,*,/ symbol through substring and parsed it as double which is Num1 then the next number after the symbol is assigned to Num2. It only solves a math operation involving 2 numbers and a single math symbol eg +,- etc., be it decimal or integer. But I want to extend it to solve for multiple number inputs with multiple math operators like "2+5-3" i.e for it capture the third number and calculate...

How do I send the code?
Re: Java Code Pls Modify This Code Help by etoluw: 9:12pm On Dec 24, 2018
lincsnuel:


Yea you're correct abt the jString... It's an array that contains those symbols. I extracted the first number before +,-,*,/ symbol through substring and parsed it as double which is Num1 then the next number after the symbol is assigned to Num2. It only solves a math operation involving 2 numbers and a single math symbol eg +,- etc., be it decimal or integer. But I want to extend it to solve for multiple number inputs with multiple math operators like "2+5-3" i.e for it capture the third number and calculate...

How do I send the code?

I used to think coding a caculator was a very trivial thing but now thinking about what u are doing i realize it is a bit complicated.

u can add me on whatsapp - 0812 961 5641 so we can chat
Re: Java Code Pls Modify This Code Help by asalimpo(m): 10:20pm On Jan 19, 2019
First,use a more oop approach to d issue. Create a number object nd run queries on it. This will result in a more modular design that helps u isolate nd trap errors.
E.g
Num num = new Num(<string argumnt>wink;
when about to display an answer you ask what number type it has.
You can use a boolean or an Enum.
1) boolean option.
Num.isInt(); if yes do as you like
if no,then it's a float/double.
You may also query to know,if it has trailing zeros ,then convert to int.
num.hasTrailingZeros(); e.g 2.0000 etc

2) Enum approach:
numberType nType = num.type();
checktype:
if(nType == numberType.INT)
... do somth
else
... do sth else.

The parsing should b done with a parser object, that's robust to handle all legal expressions. It's work creating one. Better if it is reusable so that u can use it in other string parsing programs e.g csv files,web scraping etc

(1) (Reply)

Books On Python / Please Help Me Solve This C++ Question / Hw Can Someone Create A Video Streaming Map in java

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 27
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.