Hi, for the life of me I can't solve this problem. I've solved problems far harder, but this is just baffling me.

The problem:

Write a program which reads a sequence of integers from the standard input (the
keyboard) and prints the largest value read to the standard output (the screeen).

My current code is as follows:

import java.util.*;
public class Largest 
{
	public static void main (String [] args)
	{
		System.out.println("Enter a sequence of numbers");
		Scanner in = new Scanner(System.in);
		int count = 0;
		while(in.hasNextLine() )
		{
			int nums = in.nextInt();
			if(nums > count)
			{
				count = nums;
				count++;
				System.out.println(count);
			}
		}
		
	}	
}

But this doesn't seem to work.

If anyone can offer some help that'd be great!

Having a variable called "count" is confusing - is it left over from a previous exercise?
To find the largest you need a variable called "largest" which is initialised to a small value.
Then as you process each input you ask "is this bigger than largest" and if so, save that value as the new value of largest.
When all the numbers have been processed print out largest.

Thank you for the reply. I took into account what you said, and I still am slightly confused. The code has been changed to this:

import java.util.*;
public class Largest 
{
	public static void main (String [] args)
	{
		System.out.println("Enter a sequence of numbers");
		Scanner in = new Scanner(System.in);
		int largest = 0;
		while(in.hasNextLine() )
		{
			int nums = in.nextInt(); //read numbers until user stops
			if(nums > largest)//is one of the numbers, greater than largest?
			{
				nums = largest;//assign that number to largest
				System.out.println(largest);//print it out
			}
		}
		
	}	
}

But still no luck!

Well the code above simply prints out a zero after every number entered. The output I expect is as follows:

Enter numbers: 1 2 3 4 5 6
6

Because 6 is the biggest, I want just 6 to be printed.

Even when I move the "System.out.println(largest);" outside the if/while I get the same output :\

Thank you for pointing it out :) Seems that was what the problem was. This still didn't work because the compiler threw up some exceptions, but I realised this was a problem with Scanner. I decided to just use an external class to read user input and now it works! Thanks again.

This question has already been answered. Start a new discussion instead.