Counting matches in array algorithm for song ratings
I've spent the last six hours trying to solve this small piece of code, and I really would appreciate learning how to do this.
I have this array so far:
public static void main ()
{Song topTenSongs = {new Song("The Twist"),
new Song ("Smooth"),
new Song ("Mack the Knife"),
new Song ("How Do I Live"),
new Song ("Party Rock Anthem"),
new Song ("I Gotta Feeling"),
new Song ("Macarena (Bayside Boys Mix)"),
new Song ("Physical"),
new Song ("You Light Up My Life"),
new Song ("Hey Jude")
};
String tenSongNames = {"The Twist",
"Smooth",
"Mack the Knife",
"How Do I Live",
"Party Rock Anthem",
"I Gotta Feeling",
"Macarena (Bayside Boys Mix)",
"Physical",
"You Light Up My Life",
"Hey Jude"};
int songRatings = {2,8,10, 7,1,6,2,4,3,5};
My goal is to use a for-each loop to count the number of songs that are tied for second, and then to use a String variable and string concatenation to keep track of the Song titles tied for second.
How would I go about doing this? I currently have something along the lines of:
for (Song s : topTenSongs) {
int count = 0;
if(s.getRating() == 2) {
System.out.println(count + 1);
}
}
but I have the feeling that this is wrong.
Thank you!
java arrays
|
show 2 more comments
I've spent the last six hours trying to solve this small piece of code, and I really would appreciate learning how to do this.
I have this array so far:
public static void main ()
{Song topTenSongs = {new Song("The Twist"),
new Song ("Smooth"),
new Song ("Mack the Knife"),
new Song ("How Do I Live"),
new Song ("Party Rock Anthem"),
new Song ("I Gotta Feeling"),
new Song ("Macarena (Bayside Boys Mix)"),
new Song ("Physical"),
new Song ("You Light Up My Life"),
new Song ("Hey Jude")
};
String tenSongNames = {"The Twist",
"Smooth",
"Mack the Knife",
"How Do I Live",
"Party Rock Anthem",
"I Gotta Feeling",
"Macarena (Bayside Boys Mix)",
"Physical",
"You Light Up My Life",
"Hey Jude"};
int songRatings = {2,8,10, 7,1,6,2,4,3,5};
My goal is to use a for-each loop to count the number of songs that are tied for second, and then to use a String variable and string concatenation to keep track of the Song titles tied for second.
How would I go about doing this? I currently have something along the lines of:
for (Song s : topTenSongs) {
int count = 0;
if(s.getRating() == 2) {
System.out.println(count + 1);
}
}
but I have the feeling that this is wrong.
Thank you!
java arrays
1
Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.
– CertainPerformance
Jan 2 at 3:57
does your Song object contains rating as well ?
– mkjh
Jan 2 at 4:03
String variable and string concatenation to keep track of the Song titles tied for second.
this is not clear, where do you need to save for tracking? and what?
– Deadpool
Jan 2 at 4:04
@Deadpool I need to find the songs that both have rating of "2", which in this case would be songs in index 0 and index 7 in the array, and System.print.ln the titles of those songs. Those songs would be "The Twist" and "Macarena (Bayside Boys Remix).
– Mathy Person
Jan 2 at 4:07
@mkjh No, the Song object just includes the titles. I think that the rating is in a separate array called songRatings. Sorry if my terminology is off!
– Mathy Person
Jan 2 at 4:08
|
show 2 more comments
I've spent the last six hours trying to solve this small piece of code, and I really would appreciate learning how to do this.
I have this array so far:
public static void main ()
{Song topTenSongs = {new Song("The Twist"),
new Song ("Smooth"),
new Song ("Mack the Knife"),
new Song ("How Do I Live"),
new Song ("Party Rock Anthem"),
new Song ("I Gotta Feeling"),
new Song ("Macarena (Bayside Boys Mix)"),
new Song ("Physical"),
new Song ("You Light Up My Life"),
new Song ("Hey Jude")
};
String tenSongNames = {"The Twist",
"Smooth",
"Mack the Knife",
"How Do I Live",
"Party Rock Anthem",
"I Gotta Feeling",
"Macarena (Bayside Boys Mix)",
"Physical",
"You Light Up My Life",
"Hey Jude"};
int songRatings = {2,8,10, 7,1,6,2,4,3,5};
My goal is to use a for-each loop to count the number of songs that are tied for second, and then to use a String variable and string concatenation to keep track of the Song titles tied for second.
How would I go about doing this? I currently have something along the lines of:
for (Song s : topTenSongs) {
int count = 0;
if(s.getRating() == 2) {
System.out.println(count + 1);
}
}
but I have the feeling that this is wrong.
Thank you!
java arrays
I've spent the last six hours trying to solve this small piece of code, and I really would appreciate learning how to do this.
I have this array so far:
public static void main ()
{Song topTenSongs = {new Song("The Twist"),
new Song ("Smooth"),
new Song ("Mack the Knife"),
new Song ("How Do I Live"),
new Song ("Party Rock Anthem"),
new Song ("I Gotta Feeling"),
new Song ("Macarena (Bayside Boys Mix)"),
new Song ("Physical"),
new Song ("You Light Up My Life"),
new Song ("Hey Jude")
};
String tenSongNames = {"The Twist",
"Smooth",
"Mack the Knife",
"How Do I Live",
"Party Rock Anthem",
"I Gotta Feeling",
"Macarena (Bayside Boys Mix)",
"Physical",
"You Light Up My Life",
"Hey Jude"};
int songRatings = {2,8,10, 7,1,6,2,4,3,5};
My goal is to use a for-each loop to count the number of songs that are tied for second, and then to use a String variable and string concatenation to keep track of the Song titles tied for second.
How would I go about doing this? I currently have something along the lines of:
for (Song s : topTenSongs) {
int count = 0;
if(s.getRating() == 2) {
System.out.println(count + 1);
}
}
but I have the feeling that this is wrong.
Thank you!
java arrays
java arrays
edited Jan 2 at 4:08
Mathy Person
asked Jan 2 at 3:57
Mathy PersonMathy Person
1024
1024
1
Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.
– CertainPerformance
Jan 2 at 3:57
does your Song object contains rating as well ?
– mkjh
Jan 2 at 4:03
String variable and string concatenation to keep track of the Song titles tied for second.
this is not clear, where do you need to save for tracking? and what?
– Deadpool
Jan 2 at 4:04
@Deadpool I need to find the songs that both have rating of "2", which in this case would be songs in index 0 and index 7 in the array, and System.print.ln the titles of those songs. Those songs would be "The Twist" and "Macarena (Bayside Boys Remix).
– Mathy Person
Jan 2 at 4:07
@mkjh No, the Song object just includes the titles. I think that the rating is in a separate array called songRatings. Sorry if my terminology is off!
– Mathy Person
Jan 2 at 4:08
|
show 2 more comments
1
Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.
– CertainPerformance
Jan 2 at 3:57
does your Song object contains rating as well ?
– mkjh
Jan 2 at 4:03
String variable and string concatenation to keep track of the Song titles tied for second.
this is not clear, where do you need to save for tracking? and what?
– Deadpool
Jan 2 at 4:04
@Deadpool I need to find the songs that both have rating of "2", which in this case would be songs in index 0 and index 7 in the array, and System.print.ln the titles of those songs. Those songs would be "The Twist" and "Macarena (Bayside Boys Remix).
– Mathy Person
Jan 2 at 4:07
@mkjh No, the Song object just includes the titles. I think that the rating is in a separate array called songRatings. Sorry if my terminology is off!
– Mathy Person
Jan 2 at 4:08
1
1
Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.
– CertainPerformance
Jan 2 at 3:57
Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.
– CertainPerformance
Jan 2 at 3:57
does your Song object contains rating as well ?
– mkjh
Jan 2 at 4:03
does your Song object contains rating as well ?
– mkjh
Jan 2 at 4:03
String variable and string concatenation to keep track of the Song titles tied for second.
this is not clear, where do you need to save for tracking? and what?– Deadpool
Jan 2 at 4:04
String variable and string concatenation to keep track of the Song titles tied for second.
this is not clear, where do you need to save for tracking? and what?– Deadpool
Jan 2 at 4:04
@Deadpool I need to find the songs that both have rating of "2", which in this case would be songs in index 0 and index 7 in the array, and System.print.ln the titles of those songs. Those songs would be "The Twist" and "Macarena (Bayside Boys Remix).
– Mathy Person
Jan 2 at 4:07
@Deadpool I need to find the songs that both have rating of "2", which in this case would be songs in index 0 and index 7 in the array, and System.print.ln the titles of those songs. Those songs would be "The Twist" and "Macarena (Bayside Boys Remix).
– Mathy Person
Jan 2 at 4:07
@mkjh No, the Song object just includes the titles. I think that the rating is in a separate array called songRatings. Sorry if my terminology is off!
– Mathy Person
Jan 2 at 4:08
@mkjh No, the Song object just includes the titles. I think that the rating is in a separate array called songRatings. Sorry if my terminology is off!
– Mathy Person
Jan 2 at 4:08
|
show 2 more comments
2 Answers
2
active
oldest
votes
So first iterate the songRatings
array the keep the track of index when value==2
, an get song from topTenSongs
array of that index
for-loop
for(int i=0; i< songRatings.length; i++){
if(songRatings[i]== 2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
// In the same way you can get songs from `topTenSongs` array of specific index `topTenSongs[i]`
}
}
If interested to learn java-8, by using IntStream
java-8 IntStream
IntStream.range(0, songRatings.length).filter(i->songRatings[i]==2).forEach(s->System.out.println(tenSongNames[s]));
forEach
int count =0;
for(int i: songRatings){
count++;
if(i==2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
}
}
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
add a comment |
I think the problem is in
int count =0
i should before the for. Because the count always come back to 2
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001032%2fcounting-matches-in-array-algorithm-for-song-ratings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
So first iterate the songRatings
array the keep the track of index when value==2
, an get song from topTenSongs
array of that index
for-loop
for(int i=0; i< songRatings.length; i++){
if(songRatings[i]== 2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
// In the same way you can get songs from `topTenSongs` array of specific index `topTenSongs[i]`
}
}
If interested to learn java-8, by using IntStream
java-8 IntStream
IntStream.range(0, songRatings.length).filter(i->songRatings[i]==2).forEach(s->System.out.println(tenSongNames[s]));
forEach
int count =0;
for(int i: songRatings){
count++;
if(i==2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
}
}
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
add a comment |
So first iterate the songRatings
array the keep the track of index when value==2
, an get song from topTenSongs
array of that index
for-loop
for(int i=0; i< songRatings.length; i++){
if(songRatings[i]== 2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
// In the same way you can get songs from `topTenSongs` array of specific index `topTenSongs[i]`
}
}
If interested to learn java-8, by using IntStream
java-8 IntStream
IntStream.range(0, songRatings.length).filter(i->songRatings[i]==2).forEach(s->System.out.println(tenSongNames[s]));
forEach
int count =0;
for(int i: songRatings){
count++;
if(i==2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
}
}
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
add a comment |
So first iterate the songRatings
array the keep the track of index when value==2
, an get song from topTenSongs
array of that index
for-loop
for(int i=0; i< songRatings.length; i++){
if(songRatings[i]== 2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
// In the same way you can get songs from `topTenSongs` array of specific index `topTenSongs[i]`
}
}
If interested to learn java-8, by using IntStream
java-8 IntStream
IntStream.range(0, songRatings.length).filter(i->songRatings[i]==2).forEach(s->System.out.println(tenSongNames[s]));
forEach
int count =0;
for(int i: songRatings){
count++;
if(i==2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
}
}
So first iterate the songRatings
array the keep the track of index when value==2
, an get song from topTenSongs
array of that index
for-loop
for(int i=0; i< songRatings.length; i++){
if(songRatings[i]== 2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
// In the same way you can get songs from `topTenSongs` array of specific index `topTenSongs[i]`
}
}
If interested to learn java-8, by using IntStream
java-8 IntStream
IntStream.range(0, songRatings.length).filter(i->songRatings[i]==2).forEach(s->System.out.println(tenSongNames[s]));
forEach
int count =0;
for(int i: songRatings){
count++;
if(i==2){
System.out.println("The song with rating 2 is :"+ tenSongNames[i]);
}
}
edited Jan 2 at 4:31
answered Jan 2 at 4:12
DeadpoolDeadpool
6,5542629
6,5542629
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
add a comment |
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
I'm curious about what this would look like as a for-each loop. How could I translate this for loop into that?
– Mathy Person
Jan 2 at 4:23
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
In foreach loop you will get value directly, but to keep index tracking you need a variable, i will add that also @MathyPerson
– Deadpool
Jan 2 at 4:25
add a comment |
I think the problem is in
int count =0
i should before the for. Because the count always come back to 2
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
add a comment |
I think the problem is in
int count =0
i should before the for. Because the count always come back to 2
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
add a comment |
I think the problem is in
int count =0
i should before the for. Because the count always come back to 2
I think the problem is in
int count =0
i should before the for. Because the count always come back to 2
answered Jan 2 at 4:07
agustin Lujanagustin Lujan
32
32
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
add a comment |
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
fixed it to int count = 0. I'm not sure what to put between the if statement and the System.print.ln statement now, though.
– Mathy Person
Jan 2 at 4:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001032%2fcounting-matches-in-array-algorithm-for-song-ratings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.
– CertainPerformance
Jan 2 at 3:57
does your Song object contains rating as well ?
– mkjh
Jan 2 at 4:03
String variable and string concatenation to keep track of the Song titles tied for second.
this is not clear, where do you need to save for tracking? and what?– Deadpool
Jan 2 at 4:04
@Deadpool I need to find the songs that both have rating of "2", which in this case would be songs in index 0 and index 7 in the array, and System.print.ln the titles of those songs. Those songs would be "The Twist" and "Macarena (Bayside Boys Remix).
– Mathy Person
Jan 2 at 4:07
@mkjh No, the Song object just includes the titles. I think that the rating is in a separate array called songRatings. Sorry if my terminology is off!
– Mathy Person
Jan 2 at 4:08