0

I know it's very basic java but I am trying to learn. Can someone help me to understand my errors and what should I do to solve it?

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int teamA = 0;
        int teamB = 0;


        //asks for the team selection
       System.out.println("Would you like to be in Team A or Tema B? Write A for team A and B for team B");
        Scanner input =  new Scanner(System.in);
        int result = input.nextInt();

        public String scoreBoard() {
            String displayScoreBoard = "No Score";
            if (result.toString == "A" || result.toString == "a"){
                teamA++;
                displayScoreBoard = "Score of TeamX is" + teamA;
            }  else if (result.toString == "B" || result.toString == "b"){
                teamB++;
                displayScoreBoard = "Score of TeamY is" + teamB;
            } System.out.println(displayScoreBoard.toString);
        }


        // write your code here
    }
}
Markus
  • 1,141
  • 1
  • 9
  • 25
Hima Chhag
  • 401
  • 7
  • 22
  • 1
    Don't compare `Strings` with `==` use `equals` instead or in this case `equalsIgnoreCase`. Also your `result.toString()` can never be "A" or "B" since you expect an integer. – Eclipse Mar 15 '17 at 20:15
  • Thank you so much for your help. It solved my problem. :-) – Hima Chhag Mar 15 '17 at 20:27

1 Answers1

0

You made several mistakes. For example you wrote a function within a function. You better don't do that. Just look at my code. If you have any questions, just ask ;)

UPDATE: Due to the comments that there should be a loop, I've adapted the code.

import java.util.Scanner;
public class Main{

    public static void main(String[] args) {
        int teamA = 0;
        int teamB = 0;


        //asks for the team selection
        while(true) {
            System.out.println("Would you like to be in Team A or Tema B? Write A for team A and B for team B. Type anything else to quit.");
            Scanner input = new Scanner(System.in);
            String result = input.next();


            if (result.toUpperCase().equals("A")) {
                System.out.println("Score of TeamX is" + ++teamA);
            } else if (result.toUpperCase().equals("B")) {
                System.out.println("Score of TeamY is" + ++teamB);
            }
                else {
                    break;
                }

            }

        }

}
Markus
  • 1,141
  • 1
  • 9
  • 25
  • Thank you so much. Got your point and also solve my problem. Just to understand it better, to compare two strings, we need to use ".equals". == is only available for int, long or double data type, correct? – Hima Chhag Mar 15 '17 at 20:26
  • It is better to use equals. Have a look at http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java there it is very good explained. And please upvote my answer (and if you want to, mark it as solution). Thanks – Markus Mar 15 '17 at 20:29
  • I have marked your answer. But unfortunately, I am unable to upvote as my reputation is less than 15. I have updated this file little bit. It works fine but starts score from 1 every time rather than incrementing it. I know I need a loop but doesn't know where! – Hima Chhag Mar 15 '17 at 21:52
  • there is a better way than using `true` as loop check and breaking the loop. Just use a boolean and make it `false` if no player has to be assigned. By this you can have better control (such as introducing a confirmation) – KarelG Mar 16 '17 at 07:33
  • @Markus.. You are awesome! It works great. I had no idea about ++variable pre-increment operator, it's really a good learning. Again, thanks a lot for your help! – Hima Chhag Mar 16 '17 at 13:47