#cant solve problem

1 messages · Page 1 of 1 (latest)

deep snow
#

Given the integer array dailyPrices with the size of NUM_ELEMENTS, assign numMatches with the number of integers in userValues that are equal to 9.

Ex: If the input is 109 9 38 51 9 9 104 9, then the output is:

Number of 9s in array: 4

import java.util.Scanner;

public class ValueMatcher {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_ELEMENTS = 8;
int[] dailyPrices = new int[NUM_ELEMENTS];
int i;
int numMatches;

  for (i = 0; i < dailyPrices.length; ++i) {
     dailyPrices[i] = scnr.nextInt();
  }

  /* Your code goes here */

  System.out.println("Number of 9s in array: " + numMatches);

}
}

i cant solve this question without changing the given loop, but my code can only go in the comment. Please help

strange swallowBOT
#

<@&987246399047479336> please have a look, thanks.

strange swallowBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

and NUM_ELEMENTS is 5, the output is:
numMatches = 1.

digital compass
#

Have a counter variable and each time you come across 9, increment the counter

sour dune
#

^

#

if(dailyPrices[i] == 9){numMatches += 1;}

plain tree
#

alternatively, stream api: int numMatches = (int) Arrays.stream(dailyPrices).filter(f -> f == 9).count();

sour dune
#

^

hasty halo
sour dune
#

stream would be part of lambda function right? or is that separate? I always mix these up

hasty halo
#

i believe it makes writing streams much more cleaner, but it's a separate concept.

deep snow
hasty halo
deep snow
deep snow
plain tree
#

assign 0 to it

subtle mango
hasty halo
sour dune
plain tree
#

regular old expressions

deep snow
#

It's ok thanks guys, i got it, the program wanted me to make a new variable.

this is the "correct answer"

First, numMatches is initialized with 0. Then, a for loop iterates through all the integers in array dailyPrices. An if statement in the for loop increments numMatches by 1 only if the element matches matchValue.

plain tree
#

lambdas are anonymous implementations of methods

deep snow
#

matchValue never existed in the code or was stated in the prompt

#

so i had to used a hint which revealed

subtle mango
plain tree
#

the actual interface implementation is created by the indy bsm the lambda compiles to

#

the only thing you write is the implementation of the method

deep snow
#

bro what are you guys talking about

#

i have no clue

plain tree
#

lambdas

deep snow
#

this is my first week of java

subtle mango
#

yea ignore all of that

plain tree
#

yeah this is deep into the internals of how the language works, you dont need to worry about it

deep snow
#

when would i need to learn this though?

viral palm
#

they are discussing around your problem, they shouldnt really do that in here 😉

plain tree
viral palm
#

they are all offering solutions, but since your starting , none of those can be used

subtle mango
subtle mango
#

so u solved it?

deep snow
#

yeah i was suppose to make a new interger to assign the values to

subtle mango
#

okay nice

hasty halo