Forums Register Login

Subtracting strings

+Pie Number of slices to send: Send

String j is a random number

String g is another random number.

Here i am trying to subtract one random number from another (j-g)

From doing this i am getting an error of:

Any help?
+Pie Number of slices to send: Send
At line 55 you have

Here 'j' is defined as a String. The calling of parseInt() here will always thrown an exception because j has been initialized to null.

'j' is local to this block of code and would not be visible outside of the block.
+Pie Number of slices to send: Send
 

Carey Brown wrote:At line 55 you have

Here 'j' is defined as a String. The calling of parseInt() here will always thrown an exception because j has been initialized to null.

'j' is local to this block of code and would not be visible outside of the block.


Thanks for the help!
But now I have an error of:
Operator '-' cannot be applied to 'java.lang.string','java.lang.string'.
(This is after making it into one code block and needing to finalise the variable 'j')
+Pie Number of slices to send: Send
 

Bod McLeon wrote:
But now I have an error of:
Operator '-' cannot be applied to 'java.lang.string','java.lang.string'.
(This is after making it into one code block and needing to finalise the variable 'j')



The "-" operator can't be applied to two strings. There is no such a thing as "subtracting strings". What are you trying to do exactly?

Henry
+Pie Number of slices to send: Send
 

Henry Wong wrote:

Bod McLeon wrote:
But now I have an error of:
Operator '-' cannot be applied to 'java.lang.string','java.lang.string'.
(This is after making it into one code block and needing to finalise the variable 'j')



The "-" operator can't be applied to two strings. There is no such a thing as "subtracting strings". What are you trying to do exactly?

Henry


So in this, a Pokemon (Yes I know so original) is assigned a random value from 10 to 500. That number is its health.
It then asks you to pick an attack using a JOptionPane.
Then after picking another random number for the amount of damage your attack is going to do,
it subtracts that number from the health and prints the present health (The one with the damage from the attack).
Basically,
It is trying to do this:

j = Health - g = Attack Damage = Prints the health number now.

+Pie Number of slices to send: Send
 

fred rosenberger wrote:I locked your other thread where you posed this question.  Please don't post the same question more than once.


I accidentally clicked submit when I was not ready (because I thought I was ready).
Sorry.
1
+Pie Number of slices to send: Send
 

Hi Bod, welcome to the Ranch. I'll mention some other aspects.

1. There is another potential risk to get undesired result. Remind yourself how Strings for their equality needs to be checked.
or better probably
2. Make all your instance variables private.
3. Also you have variable name which starts with an Upper case (line 14), this really suppose to start with lower case. By the way, what it holds? Pokemon number? (consider then using pokemonNumber name).
4. Line 33. Probably there are no benefits to cram everythin in a one line, that reduces readability.
5. Line 52. Just keep it as or otherwise you're risking to reassign gameComplete to true by mistake if you ever manage to write
6. Your class "hi" got some wrong constructor, which seemed to be a copy paste from Pokemon class, as the constructor name and the class names doesn't match. Nevertheless, class name suppose to start with an upper case and be more descriptive.
7. this line got some magic number 1.3 which isn't clear what it means. Try to describe it as a variable.
8. You got more places where you checking strings equality with "==".
+Pie Number of slices to send: Send
 

Liutauras Vilda wrote:

Hi Bod, welcome to the Ranch. I'll mention some other aspects.
...


Thanks!
+Pie Number of slices to send: Send
[moderation comment]I removed unecessary huge quote, so the thread would be easier to follow.

Also I'm a big newbie so can you please give me a list of where i need 'Private' or an instance variable
Meaning:
Line 'x' needs one
so does Line 'z'.
(Did not know how to word it)
+Pie Number of slices to send: Send
 

where i need 'Private' or an instance variable  


I would not worry too much about using private in your code.  Wait until the time comes that you need it.  It's not too important with beginning programs.

Define a variable as an instance variable (as opposed to one that is local to a method) when the code needs access to the variable from more than one method.
+Pie Number of slices to send: Send
 

Norm Radder wrote:

where i need 'Private' or an instance variable  


I would not worry too much about using private in your code.  Wait until the time comes that you need it.  It's not too important with beginning programs.

Define a variable as an instance variable (as opposed to one that is local to a method) when the code needs access to the variable from more than one method.


Ok thanks!
+Pie Number of slices to send: Send
Instance variables are almost always private, so I would get in the habit of making them private now unless there is a very good reason not to.  If you need to have access to the instance variable from outside of the class, write a getter and a setter:
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 798 times.
Similar Threads
Whats wrong with this java code?
Can't understand where the NullPointerException is coming from
I useDelimiter \t(ab) with my Scanner, but it splits around spaces
Multiplication table from a file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:59:10.