reset password
Author Message
jpascua
Posts: 197
Posted 20:04 Oct 30, 2012 |

 

Are the numbers entered supposed to appear on one row with only one enter? Or can the user press enter each input?

redge
Posts: 31
Posted 20:13 Oct 30, 2012 |

As I recall, the Scanner class uses spaces as delimiters, making the following input

3 5 2 5 5 5 0

equal to pressing enter after each entry.

 

If not, you can store each value in an array (which I recommend anyway), then iterate through each element to check for spaces.

jpascua
Posts: 197
Posted 20:14 Oct 30, 2012 |
redge wrote:

As I recall, the Scanner class uses spaces as delimiters, making the following input

3 5 2 5 5 5 0

equal to pressing enter after each entry.

 

If not, you can store each value in an array (which I recommend anyway), then iterate through each element to check for spaces.

I don't understand what you mean. And, we haven't gotten into arrays yet.

redge
Posts: 31
Posted 20:20 Oct 30, 2012 |

I assume I lost you at "delimiter", which refers to the character(s) that mark the ending of one input and the start of another. Again, if I remember correctly, the Scanner class automatically interprets spaces as delimiters, meaning that if you enter "3 2" and tell the scanner to receive nextInt(), it should just read the 3, then the next nextInt() will return the 2. This is functionally equivalent to entering each digit individually, each separated by an enter keystroke.

As for not using arrays, how are you planning on storing the inputted numbers? Perhaps I can come up with an explanation better suited to your implementation.

jpascua
Posts: 197
Posted 20:24 Oct 30, 2012 |
redge wrote:

I assume I lost you at "delimiter", which refers to the character(s) that mark the ending of one input and the start of another. Again, if I remember correctly, the Scanner class automatically interprets spaces as delimiters, meaning that if you enter "3 2" and tell the scanner to receive nextInt(), it should just read the 3, then the next nextInt() will return the 2. This is functionally equivalent to entering each digit individually, each separated by an enter keystroke.

As for not using arrays, how are you planning on storing the inputted numbers? Perhaps I can come up with an explanation better suited to your implementation.

Ah okay. I understand what you mean now. I never knew the Scanner class had that feature.

As for storing the inputted numbers; perhaps by using variables?

redge
Posts: 31
Posted 20:33 Oct 30, 2012 |

The immediate solution that comes to mind is to use variables, but then you're left with a theoretical maximum number of integers you can accept before you run out of variables to store them in. Additionally, the logic to compare them has to be handcoded rather than contained in a loop, which is an inelegant solution at best.

 

Off the top of my head, I suggest adding each integer to a String variable, then using a for loop to iterate through each number. See psuedocode below.

Create Scanner object

Create a String to store the numbers

Write a loop that exits if the input is 0, and adds the number to your string if it isn't 0 (note that I suggest adding spaces between each number so you have a delimiter)

Write another loop that iterates through each number in the string and checks if it is the new max

hpguo
Posts: 139
Posted 21:58 Oct 30, 2012 |

Please read the hint that follows the problem description. Actually it tells you how to do it.