Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel excel 2003 firefox hard drive hardware hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem recovery router safe mode screen slow sound spyware trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
More Java beginner's help

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
CaseyRenee87's Avatar
Member with 34 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
09-Jul-2009, 08:33 PM #16
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's Avatar
Member with 34 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
10-Jul-2009, 05:03 AM #17
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's Avatar
Distinguished Member with 12,767 posts.
 
Join Date: Dec 2004
Experience: A little of this...a little of that
10-Jul-2009, 11:26 AM #18
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's Avatar
NeonFx NeonFx is offline NeonFx is authorized to help remove malware.   NeonFx has a birthday soon! NeonFx has a Profile Picture
Senior Member with 1,762 posts.
 
Join Date: Oct 2008
Location: California, USA
10-Jul-2009, 01:00 PM #19
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?
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.
CaseyRenee87's Avatar
Member with 34 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
10-Jul-2009, 03:50 PM #20
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.

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's Avatar
NeonFx NeonFx is offline NeonFx is authorized to help remove malware.   NeonFx has a birthday soon! NeonFx has a Profile Picture
Senior Member with 1,762 posts.
 
Join Date: Oct 2008
Location: California, USA
10-Jul-2009, 03:58 PM #21
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."
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.

Last edited by NeonFx : 10-Jul-2009 04:06 PM.
NeonFx's Avatar
NeonFx NeonFx is offline NeonFx is authorized to help remove malware.   NeonFx has a birthday soon! NeonFx has a Profile Picture
Senior Member with 1,762 posts.
 
Join Date: Oct 2008
Location: California, USA
10-Jul-2009, 04:10 PM #22
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.
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.
NeonFx's Avatar
NeonFx NeonFx is offline NeonFx is authorized to help remove malware.   NeonFx has a birthday soon! NeonFx has a Profile Picture
Senior Member with 1,762 posts.
 
Join Date: Oct 2008
Location: California, USA
10-Jul-2009, 04:20 PM #23
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
}
__________________
Please post the final results, good or bad. Let me know if you won't be responding any longer.
If I have not responded in three days, please feel free to PM me with a friendly reminder.
Please don't send me requests for help. Use the forums instead.
CaseyRenee87's Avatar
Member with 34 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
11-Jul-2009, 02:24 AM #24
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!
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 10:24 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.