This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Subtracting strings  RSS feed

 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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')
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "==".
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Hi Bod, welcome to the Ranch. I'll mention some other aspects.
...

Thanks!
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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)
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!