Results 1 to 4 of 4

Thread: Taking away.

  1. #1
    Bod
    Bod is offline Member
    Join Date
    Aug 2016
    Location
    Watford
    Posts
    1
    Rep Power
    0

    Question Taking away.

    I need to take away two numbers. (you'll see)

    class Health {
    int pokemon;
    private List<String> pokehealth;
    boolean gameComplete;
    public String Health;
    Scanner input;

    public Health() throws FileNotFoundException, InterruptedException {

    generatePokehealth();
    playGame();
    }


    public void generatePokehealth() throws FileNotFoundException {
    Scanner reader = new Scanner(new File("src/Pokehealth.txt"));

    pokehealth = new ArrayList<String>();

    while (reader.hasNext())
    pokehealth.add(reader.next().toLowerCase());
    reader.close();
    }

    private void playGame() throws InterruptedException, FileNotFoundException {

    do {
    Health = pokehealth.get(new Random().nextInt(pokehealth.size()));

    System.out.println("The enemy has " + Health + " hp.");


    Here is where the first number gets picked out of a .txt.


    class Damage {
    int pokemon;
    private List<String> pokedamage;
    boolean gameComplete;
    public String Damage;
    Scanner input;

    public Damage() throws FileNotFoundException, InterruptedException {

    generatePokedamage();
    playGame();
    }


    public void generatePokedamage() throws FileNotFoundException {
    Scanner reader = new Scanner(new File("src/Pokedamgae.txt"));

    pokedamage = new ArrayList<String>();

    while (reader.hasNext())
    pokedamage.add(reader.next().toLowerCase());
    reader.close();
    }

    public void playGame() throws InterruptedException, FileNotFoundException {

    do {
    Damage = pokedamage.get(new Random().nextInt(pokedamage.size()));

    System.out.println("You attack did " + Damage + " damage.");


    Here is where another number gets picked out of a .txt file.


    I tried doing this: int c = Health - Damage;
    But this gets me an error message of:
    'Operator '-' cannot be applied to 'java.lang.String','java.lang.String'.'
    Anyone know why?
    I was told that it was because of the variables where in a different scope. So i tried making them public and even putting them in the same scope but I'm still getting 'Operator '-' cannot be applied to 'java.lang.String','java.lang.String'.'


  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Taking away.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Taking away.

    'Operator '-' cannot be applied to 'java.lang.String','java.lang.String'.'
    Anyone know why?
    The - operator can not be used with String objects. It is used with numeric values. For example: 5 - 3 gives 2.
    What would the results of this be: "apple" - "orange"?

    Convert the Strings to numbers using a parse method from the Integer or Double class.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Taking away.

    Quote Originally Posted by Norm View Post
    What would the results of this be: "apple" - "orange"?
    "banana"; how many times do I have to explain it?

    kind regards,

    Jos ;-)
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Taking input from use
    By leepikamukharji in forum New To Java
    Replies: 1
    Last Post: 11-08-2011, 02:33 PM
  2. taking something from an array.
    By shazakala in forum New To Java
    Replies: 1
    Last Post: 05-10-2011, 01:36 AM
  3. taking number value without sign
    By sara12345 in forum New To Java
    Replies: 5
    Last Post: 06-01-2010, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •