1

When I set an object array such as:

Player[] player = new Player[amountOfPlayers];

I use:

amountOfPlayers = br.read();

To get the variable amountOfPlayers. Whenever I run my program I would type in 3 when asked to set the amountOfPlayers but my output says there are 51 players.

Though when I set the new Player array to: new Player[3]; it works.

Anyone know why this would be?

2
  • 1
    Can you post the actual full code, as it is hard to guess what is going on here? Commented Aug 7, 2013 at 10:11
  • 1
    can you explain this? what is player here? Commented Aug 7, 2013 at 10:12

3 Answers 3

8

The problem is that you read a char and interpret it as an integer. The character 3 has ASCII code 51.

It is probably easier to use a Scanner than a BufferedReader to read the input, as suggested by Prasad.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for mentioning :) upvote for the word ASCII which I forgot to mention.
3

Use this to read input instead of BufferedReader Scanner in = new Scanner(System.in); amountOfPlayers = in.nextInt();

This could be because br.read() reads the character value whichis 51 for 3.

using in.nextInt() will ensure it is converted into int.

Comments

1

Try this,

Integer.parseInt(bufferedReader.readLine());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.