-3

I just want to calculate the maximum value and output it when an user inputs it. I am stuck and any help would be appreciated.

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

int totalnum, number, num1, max, min;

System.out.println("How many integers are you entering?");
totalnum = Integer.parseInt(input.readLine());

System.out.println("Enter an integer");
number = Integer.parseInt(input.readLine());

max = 0;
min = 0;
if (number > max){
    max = number;
}

for (int ctr = 2; ctr <= totalnum; ctr++)

    number = Integer.parseInt(input.readLine());
    if (number > max) {
        max = number;
    }

System.out.println("The max is "+ max);
the_storyteller
  • 2,335
  • 1
  • 26
  • 37
  • 3
    Related: _[What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java)_ – Luca Kiebel May 04 '18 at 14:24
  • 4
    Your `for` statement probably needs some `{` and `}`. Indentation is not enough. – Peter B May 04 '18 at 14:26

2 Answers2

1

As mentioned in the comments, you forgot to add curly braces. Try this...

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));


int totalnum, number, num1, max, min;

System.out.println("How many integers are you entering?");
totalnum = Integer.parseInt(input.readLine());

System.out.println("Enter an integer");
number = Integer.parseInt(input.readLine());
max = number;
for (int ctr = 2; ctr <= totalnum; ctr++)
{
    number = Integer.parseInt(input.readLine());
    if (number > max){
        max = number;
    }
}
System.out.println("The max is "+ max);
noName94
  • 3,783
  • 1
  • 16
  • 32
0

IMO, there is no need to use a BufferedReader for this type of input. I'd prefer to use a Scanner.

Anyway, just to provide an alternative answer, you could read up on List and Collections and perhaps impress your colleagues/classmates/lecturer etc.

For example:

List<Integer> list = new ArrayList<>();
int totalNum = 5;*
Scanner sc = new Scanner(System.in);
for (int i = 0; i < totalNum; i++) {
    list.add(sc.nextInt());
}
System.out.println("The max is: " + Collections.max(list));

* I just defaulted it to 5, for simplicity purposes

achAmháin
  • 4,176
  • 4
  • 17
  • 40