Jump to content

Help with Java Assignment

Sdot

I have a  .java file that I have written so far. There is one last requirement that I have to create. I am having difficulty creating it so I am here looking for assistance.

 

My code reads in numbers, these numbers are then put into an array named values. I have a for loop, if the number entered is bigger than any of the previous "values", it becomes the largest value. I named that variable "largest".

Can anyone help me keep track of how many times the largest value was typed? I'm guessing I would use another for loop.

 

Please help, thanks guys. I have attached my code to this post.

Assignment.java

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, after the first loop runs you will need to make a second loop that goes through the list again and counts the times that "largest" shows up.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

It's been a long time since I've used Java, but I think this will work.  Replace:

 

if (values[i] > largest)
{
	largest = values[i];
	occurance++;
}

with

if (values[i] > largest)
{
	largest = values[i];
	occurance = 1;
} else if (values[i] == largest) {
	occurance++;
}

 

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, JoostinOnline said:

It's been a long time since I've used Java, but I think this will work.  Replace:

 


if (values[i] > largest)
{
	largest = values[i];
	occurance++;
}

with


if (values[i] > largest)
{
	largest = values[i];
	occurance = 1;
} else if (values[i] == largest) {
	occurance++;
}

 

Works perfectly

Thanks guys, You the man Joostin :)

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sdot said:

Works perfectly

Thanks guys, You the man Joostin :)

 

Since it's homework I should explain why it works.  A higher number will always reset the value to 1, because it means it's never been input before.  Every time it finds it after that, it will increase occurance (which is misspelled, btw) by one.

 

The "else" is important.  If it didn't exist, then occurance would always be at least 2.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, JoostinOnline said:

Since it's homework I should explain why it works.  A higher number will always reset the value to 1, because it means it's never been input before.  Every time it finds it after that, it will increase occurance (which is misspelled, btw) by one.

 

The "else" is important.  If it didn't exist, then occurance would always be at least 2.

Occurrence, got it lol

I'm testing it now, I put in 10 10 10 then q to stop it and it says the largest number has ocured 2 times. I am trying to figure out why. Should be 3.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Sdot said:

Occurrence, got it lol

I'm testing it now, I put in 10 10 10 then q to stop it and it says the largest number has ocured 2 times. I am trying to figure out why. Should be 3.

Like I said, my Java is rusty, but I think you're skipping the first entry.  Try entering "3,10,10,10,Q" and see if it says 3 times.

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, JoostinOnline said:

Like I said, my Java is rusty, but I think you're skipping the first entry.  Try entering "3,10,10,10,Q" and see if it says 3 times.

Your rusty java > my little to no java knowledge lol. Yes, it worked

 

Please enter values, Q to quit:
3
10
10
10
q
10 <== largest value
The array has been increased 4 times.
Largest number has occurred 3 times.
Press any key to continue . . .

 

OH, I think I understand why it did that. The if statement if (values > largest) cannot run because it is the first integer. There is nothing for it to be greater than. Basically it doesn't start counting yet.

 

Link to comment
Share on other sites

Link to post
Share on other sites

It's because you start i at 1, completely skipping values[0].  Change

for (int i = 1; i < currentSize; i++)

to

for (int i = 0; i < currentSize; i++)

 

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, JoostinOnline said:

It's because you start i at 1, completely skipping values[0].  Change


for (int i = 1; i < currentSize; i++)

to


for (int i = 0; i < currentSize; i++)

 

Correct, that solved it. Thank you so much Joostin! Thank you, mostly for helping me to understand it instead of just saying "plug this in, it should work". This work is cumulative so this information will help me at the end of the semester. We have to write a big program for the final exam that will utilize everything we have learned.

Link to comment
Share on other sites

Link to post
Share on other sites

Just a tip: You can actually skip that entire last for loop and replace it with a single line, since you already have the largest value stored in largest. ;)

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, JoostinOnline said:

Just a tip: You can actually skip that entire last for loop and replace it with a single line, since you already have the largest value stored in largest. ;)

Good eye

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×