# More Java beginner's help



## CaseyRenee87 (May 4, 2009)

hey everyone, I have another assignment i am working on, and I am a little stumped..not as bad as last time but I need a little help with writing a program.
anyways here is some code i have written so far, this assignment is meant to input 10 integers from a user and then determine the largest of those 10. feel free to correct any "bugs" i may have written.
//LargerInteger.java
import java.util.Scanner

public class LargerInteger
{
public void determineLargerInteger()
{
scanner input-new Scanner(System.in);

int largest; //the largest number found so far
int numberCounter;//counts to 10 and keeps track of numbers entered so far
int number;//variable stores each integer entered by user

Largest =0;
numberCounter=1;
//while loop/nested If statements?
while (numberCOunter <=10)//loop 10 times
{
System.out.print("Enter first integer: ");
number= input.nextInt();....//I think this continues like this for each integer input, though I am not completely sure, because with while loops you can write minimal code to perform extensive tasks..i am just not sure how to go about it with this particular program. 
..}//end while loop
How would I edit the while loop with nested if or if...else statements to input 10 integers and determine and print the largest those 10 integers?
are there any special timesaving tips i can use to accomplish this task?
I am kind of behind on my assignments trying to figure this one out. I got it in the back of my mind i am just kind of stumped on a few areas.
thanks in advance for your help!


----------



## DoubleHelix (Dec 10, 2004)

You can't determine the largest number until they've all been entered. That functionality needs to be handled outside the input loop.

There must have been a bit more in the lesson to help you with this. You haven't written any code for sorting or searching.


----------



## CaseyRenee87 (May 4, 2009)

Ohhh okay, hmmm I can post the whole lesson assignment directions here, the numbers that are supposed to be entered are random numbers and could be any integer. here is the problem:

The process of finding the largest value (ie the maximum in a group of values) is used frequently in computer appliactions. for example a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The person who sells the most units wins the contest. write a pseudocode program(I have written half of one so far) and then a Java application that inputs a series of 10 integers and determines and prints the largest integer.
the program should include at least the following three variables:
counter= to keep track of how many numbers have been input and to count to 10
number: the integer most recently input by the user
largest: the largest number found so far

and here is some of the pseudocode i have written so far:
_initialize variables_

_while number is less than ten _
_prompt user to enter the next number_
_input next number_
_add number to the total_
_compare number with all recently input integers to determine the largest so far_
_print the largest integer_

that's the basic pseudocode i have written out to help me accomplish the task, I am just unsure of what steps to take to input 10 integers then determine the largest so far.


----------



## DoubleHelix (Dec 10, 2004)

You're unsure about the crux of the assignment. Do you have an instructor? A tutor? These are basic programming operations. You have to work through this on your own if you want to learn.


----------



## CaseyRenee87 (May 4, 2009)

Yes actually, I do though this is a distance learning course..so I have to email them. Anyways I will try to work it out tomorrow..and I'll come back here if I have bugs in my code, because those are the hardest to figure out. I'm also in kind of a hurry because this course expires July 30th, and if i dont complete it by then i have to buy another extension. anyways i will try and work it out on my own and come back here if i shall have a problem.

thanks!


----------



## burnthepc (Aug 12, 2007)

CaseyRenee87 said:


> _compare number with all recently input integers to determine the largest so far_
> _print the largest integer_


Why compare all input to determine the largest?

You have two vairiables there: largest and number.

Largest is the biggest number so far, number is the current input.

so

if largest is less than than number,
then largest should be the current input.


----------



## NeonFx (Oct 22, 2008)

burnthepc said:


> if largest is less than than number,
> then largest should be the current input.


exactly

int inputed = 0;
int highest;

while(inputed < 10){
//ask for int and assign int to current
if(current > highest) highest = current;
total += current;
inputed++;
}


----------



## CaseyRenee87 (May 4, 2009)

Thanks neonfx and burnthepc!!!
that's exactly what I was needing to know I will write up the code, and try it out some and if i run into problems..i will come back here with them.


----------



## CaseyRenee87 (May 4, 2009)

here is some code I have gotten done so far, using the tips you guys submitted. there seems to be a few bugs in it though..one error says it cannot access LargestValue because it is a non static class.. what could be causing that error message?

also: I havent yet entered a termination phase..I am not sure what to put.

//LargestValue.java Assignment 6946
//casey clark student no.: 
import java.util.Scanner; //scanner class import declaration 
class LargestValue
{
public void determineLargestValue()
{
//create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);

int total;
int intCounter;
int number=0;//currently input number
int largest;//largest number found so far

//initialization
total=0;
intCounter=1;
largest=0;

while (number<10){
System.out.print("Enter number: ");
number = input.nextInt();
total= total + number;
intCounter = intCounter ++;

if (number>largest)
largest=number;
total += number;
intCounter++;

}//end while

//termination phase

//display total
System.out.printf("\nThe largest of all 10 integers is:%d\n", largest);
}//end method LargestValue
}//end class
//LargestValueTest.java
//invoke the determine largest value method in LargestValue class
public class LargestValueTest{
public void main(String args[])
{
LargestValue.determineLargestValue();//find the largest integer in 10
}//end main
}//end class


----------



## burnthepc (Aug 12, 2007)

That error is because you haven't created an object.
LargestValue.determineLargestValue() is trying to call a static version of the method.

Don't worry yourself over static methods (although that's a better solution to the problem). Just create a vairiable for your largest value class and call the method on that


----------



## NeonFx (Oct 22, 2008)

Use burnthepc's advice to edit the main class

Here's a few things you need to learn before you get the LargestValue class right:


Go over your loop; you seem to have misunderstood how it works. You want to do something over and over again as long as the number of times you do that something is less than 10.
If you are using an if statement over more than one line you will need brackets around the code
total += number; is the same thing as saying total = total + number;
intCounter++ is the same thing as intCounter = intCounter + 1;
Considering these, how would you change your code?


----------



## CaseyRenee87 (May 4, 2009)

ok guys I edited my code...it seems to look right, or match the other class example in my textbook..but when I try and execute it, it gives the "No such Method Java.Lang" error. so we've moved from a compile time error to a fatal runtime error.


//LargestValue.java Assignment 6946
//casey clark student no.: MA2008030031
import java.util.Scanner; //scanner class import declaration 
public class LargestValue
{
public void determineLargestValue()
{
//create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);

int total;
int intCounter;
int number=0;//currently input number
int largest;//largest number found so far

//initialization
total=0;
intCounter=1;
largest=0;

while (number<10)
{
System.out.print("Enter number: ");
number = input.nextInt();
total+= number;
intCounter ++;

if 
(number>largest)
{
largest=number;
total += number;
intCounter++;
}//end if

}//end while

//termination phase

//display total
System.out.printf("\nThe Total is: %d\n", total);
System.out.printf("\nThe largest of all 10 integers is: %d\n", largest);
}//end method LargestValue
}//end class

class LargestValueTest
{
public void main(String args[])
{
LargestValue LargestValue2 = new LargestValue();//create object of LargestValue class to call method 
LargestValue2.determineLargestValue();//find the largest integer in 10
}//end main
}//end class


----------



## NeonFx (Oct 22, 2008)

Hi there.

Change



> public void main(String args[])


to this:

public static void main(String args[])

Your program should compile and run just fine after that.

When you run your program, try entering the number 10, see what happens.

The reason your program stops asking for numbers there has to do with:



> while (number<10)


What is wrong with this line? How do "while loops" work?

If you can't answer that, try creating a different kind of loop you might be more familiar with.


----------



## CaseyRenee87 (May 4, 2009)

Ah!!! ok i changed that method main back to a static method...i forgot i had changed it because of that previous error.

ok..now the while loop is what is giving me the most trouble LOL..I am new to While loops, plus they are the only loop i have started to learn so far, and not to mention this program requires Iteration ..actually this lesson is teaching beginner control statements. 

I found out though my program DOES have a logic error. it has a sort of Infinite Loop. I changed the < to =<, and now it keeps asking for integers AS LONG as they are single digit. now if they are double or more digits it stops asking for them and outputs the total..and largest. It dosent help that i am kind of bad at math.


----------



## NeonFx (Oct 22, 2008)

It is only infinite as long as you don't enter a number 10 or above 

Why is that? because you are checking the state of the wrong control value. 

The control value for the while loop should be whatever you increment by one every time the code runs. 

For example, the control value for the while loop is count. This will print "Hello World!" ten times. 

while(count < 10)
{
System.out.println("Hello World!");
count++;
}


you are checking if number is less than 10 where you should be checking if IntCounter is less than ten  IntCounter should only increment by one every time the loop is run.


----------



## CaseyRenee87 (May 4, 2009)

Ohhhhh! LOL ok so, instead of if (number) it should be If (IntCounter)?

so sorry..if it's taking me forever to get this, I have slight ADD/OCD and I go over things in my head millions of times until they dont make sense..I tend to overthink!

I'll revise the code again and see if we can finally get this show on the road


----------



## CaseyRenee87 (May 4, 2009)

Alrighty!
I got that latest loop issue fixed...now I have got another problem to work out. here is the revised code:

import java.util.Scanner; //scanner class import declaration 
public class LargestValue
{
public void determineLargestValue()
{
//create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);

int total;
int intCounter;
int number;//currently input number
int largest;//largest number found so far

//initialization
total=0;
intCounter=1;
largest=0;
number=0;

while (intCounter<10)//loop 10 times
{
System.out.print("Enter number: ");
number = input.nextInt();
total+= number;
intCounter ++;

if//if statement 
(number>largest)
{
largest=number;
total += number;
intCounter++;
}//end if

}//end while

//termination phase

//display total
System.out.printf("The total is : %d\n", total);
System.out.printf("\nThe largest of all 10 integers is: %d\n", largest, number);
}//end method LargestValue
}//end class

class LargestValueTest
{
public void main(String args[])
{
LargestValue LargestValue2 = new LargestValue();//create object of LargestValue class to call method 
LargestValue2.determineLargestValue();//find the largest integer in 10
}//end main
}//end class
class LargestValueTest
{
public static void main(String args[])
{
LargestValue LargestValue2 = new LargestValue();//create object of LargestValue class to call method 
LargestValue2.determineLargestValue();//find the largest integer in 10
}//end main
}//end class

now the issue here is when I execute and test the class, the While loop only Iterates 5 times instead of 10.  I am about 95%+ percent finished with this program because it does what it was intended to do, it determines the largest value, only in 5 integers. Now it should be ready for submission once I get this latest(and hopefully final) logical bug taken care of.

So what is my program missing now? another If statement? while loop? I'm not sure how to fix it to loop 10 x instead of 5.


----------



## DoubleHelix (Dec 10, 2004)

You're adding the number to the total twice. You did see then when you ran the code, right?

You're also initializing the largest value to 0. Maybe the largest number someone enters is negative. Then you're not really capturing the largest number entered.


----------



## NeonFx (Oct 22, 2008)

int intCounter = 0;
int total = 0;
int number;
int largest;

System.out.println("Enter number: ");
largest = input.nextInt();

while (intCounter < 9)
{
System.out.println("Enter number: ");
number = input.nextInt();
if(number > largest)
{
largest = number;
}
total += number;
intCounter++;
}

Now explain to me step by step what happened in this code. Starting with why I used IntCounter = 0 instead of IntCounter = 1.

Why did I ask for a value before the loop?
Why were you getting 5 instead of ten loops (actually, 5 instead of 9 because you initiated it at 1 instead of 0 ?

Also... two LargestValueTest classes?


----------



## CaseyRenee87 (May 4, 2009)

ahhh....okay haha I get it now, I think the Java concepts were giving me a brain freeze.. 

okay I get it now! by initializing it at 1, i was actually decrementing the loop because it would start counting at 2 instead of 1! so i would have gotten 9 anyway. It's clear to me now.

the reason I put the two largest value classes is I accidentally likely hit paste twice (or CTRL+V) so that isn't actually in the working program..the second LargestValueTest class.

neonfx:
So I was initializing the values wrong? I read that there are certain circumstances it's appropriate to initialize to 1 instead of 0, this class must not have been one of them hehe.

I noticed you incremented number to total after the If statement brackets, because they are two seperate statements right? another mistake I made.:down:

Thank you all alot for the help..I mean i couldnt' have done this without your assistance. I'd drive my instructor crazy with all my mistakes.

now back to the JCreator to fix this.


----------



## NeonFx (Oct 22, 2008)

Well, the code is still lacking. You need to add the first time I ask for a number to the total.

Also, you need to understand the difference between comparing two values with < (less-than) and <= (less-than or equal to)

If you were to count from 0 to 10 you would get 11 numbers, because you have to include the number zero in your counting. Go ahead, try it with your fingers. This would be (counter <= 10) where counter starts at 0.

if you start counter at 1, then you get ten digits. go ahead, try it with your fingers. this would be (counter <= 10) where counter starts at 1.

if you use counter = 1 and counter < 10 you then you cannot include the number 0 nor can you include the number 10 because the number has to be LESS-THAN the number ten, not equal to it. so if you start from 0 and count to LESS-THAN 9 you only get 9 numbers, so your code runs nine times. That's what I did in my code, because you start with a number before the loop so we only need the loop to run nine times.

Just make sure you add in total=largest; before the loop. I hope you realize why you need to do this.


It's not about getting the formula right for each exercise. It is about thinking about the problem logically and solving the problem logically. What you had to do for this assignment can be done in many different ways there is no one way of doing something when it comes to programming. 

You could have used any other loop sequence, or asked for numbers over and over again and then create a sorting algorithm to sort the numbers in an array and then print out the last int in the array to know the largest. 

The point is, there are many different ways of getting to an answer logically. There are no "formulas to follow."


----------



## NeonFx (Oct 22, 2008)

The reason you got 5 runs instead of 10 (9 lol) was that you were incrementing the number twice. It has nothing to do with whether or not you did it inside an if statement or not. It is how many times you incremented the number. 

If that number is what is counting how many times you have run the While loop, then it should only increment once every time the loop is run.


----------



## NeonFx (Oct 22, 2008)

Here's the code in plain english for you. Maybe it will help to understand why this makes complete logical sense.

int intCounter = 0;
//Make an integer variable called intCounter that is equal to 0 because I want to start counting from zero, not one
int total = 0;
//Make an integer variable called total that is equal to 0 because I want to add all the numbers I am given, starting at 0
int number;
int largest;
//Make two more integers that I will need in my calculations. One called number to keep track of the current number I am working with and One called largest to keep track of the largest number i have used so far.

System.out.println("Enter number: ");
//Print out this text
largest = input.nextInt();
//Make whatever the user types in the largest number so far

total += largest;
//Make the total addition of numbers used so far equal to the number just typed in plus total's last number (0)

while (intCounter < 9)
//While my counter is less 9 (the first run it is equal to 0, the second run it is equal to 1, the third run it is equal to 2) run this code. When the counter is equal to 9, move on to the code after the while loop's closing bracket. 
{
System.out.println("Enter number: ");
//print this out
number = input.nextInt();
//ask for a number
if(number > largest)
//if that number is bigger than my largest number so far, run this code inbetween the brackets
{
largest = number;
//largest is now equal to the number I am currently working with
}
total += number;
//add the current number to the total
intCounter++;
//the while loop has run one more time
}


----------



## CaseyRenee87 (May 4, 2009)

Hey thank you so much!
the code is finished and works! + I learned a few things along the way! sometimes it helps to learn by example. now that i have studied more about control statements I can write bug free code in the future!

everyone your help was greatly appreciated!:up:


----------

