2

I am working on an assignment and am stuck on one part. Searching my array to see if it contains the number 4. I am also having the problem of that answer repeating 10 times. I know why that is happening I just don't know how to fix it. I have been working on this problem since yesterday and it was smooth until I got to this section. Any bits of advice to point me in the right direction would be great. Below is the program;

import java.util.*;


public class Array {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {


    int[] running = new int[10];

    int x;
    int sum = 0;
    int number;



    for(x = 0; x < 10; x++)
        {
            running[x] = (int) (x*2.6 + 2.6);
            System.out.println(running[x]);
        }

    for(x = 0; x < running.length; x++)
    {
        sum = sum + running[x];
    }

        System.out.println("The value for index 2 is: " + running[2]);
        System.out.println("The list length is: " + running.length);
        System.out.println("The total number of miles ran is: " + sum);
        System.out.println("The average number of miles ran is: " +(double)sum/running.length);

    for(x = 0; x < running.length; x++)
    {
        if (x == 4)
            System.out.println(4 + " Does not exist.");

        else
            System.out.println(4 + " does exist.");

    }
}

}
4
  • 4
    At least your username is honest :) Commented Jun 11, 2015 at 14:52
  • Your code looks fine. Commented Jun 11, 2015 at 14:52
  • Use a boolean variable to store, if you have found a 4 and print that result after the loop. Commented Jun 11, 2015 at 14:54
  • Thanks for the help everyone. I am slowly working my way into learning the language and the rules of programming. Getting into the right frame of thinking is big for me and when I get stuck I get frustrated. It's nice to have this website to check for answers and help. Commented Jun 11, 2015 at 15:04

3 Answers 3

1

Its printing many times because its inside a loop. Also, your print statement is printing wrong information.

for(x = 0; x < running.length; x++)
{
    if (running[x] == 4) {
        System.out.println(4 + " does exist.");
        return;
    }

}
   System.out.println(4 + " does not exist.");
Sign up to request clarification or add additional context in comments.

8 Comments

Your code will always execute "does not exist" regardless if it is true or not.
@Error404 you missed the return statement inside the loop.
but I don't really know why you use there a return. If it is not a method. I'm learning at Java too so I'm not secure about that, I though it was only used on methods.
Yes you are right... And main() is a method too... right??? And this code is inside main if you see the OP.
Yes, you are true. But what exactly does this return here? Finish the program?
|
1

You are checking against your loop index, instead of the actual array.

Change this:

if (x == 4)

to this:

if (running[x] != 4)

EDIT:

If what you want is to see if the array contains the number 4 at least once, and then print out the result once, then you can do something like this instead:

boolean found = false;
for(x = 0; x < running.length; x++)
{
    if (running[x] == 4) {
        found = true;
        break;
    }
}

if (found) {
    System.out.println("4 does exist.");
} else {
    System.out.println("4 does not exist.");
}

2 Comments

Thanks, that helped. Although I had to change the == to !=. Also, I am still stuck on the answers printing out 10 times. How would I go about changing it to just show up once?
@LostandConfused If you don't want to print 10 times, you need to move your print statement outside of your for-loop.
0

You should make your loop like this:

for(x = 0; x < running.length; x++)
{
    if (running[x] != 4)
       System.out.println(4 + " Does not exist.");
    else
       System.out.println(4 + " does exist.");
}

You have to make reference to the items of the array, not to the index of it.

If you want to have the answer just one time you will have to do:

boolean correct = false;

    for(x = 0; x < running.length && correct == false; x++)
    {
      if (running[x] == 4)
          correct = true;
    }

    if(correct == true)
       System.out.println(4 + " does exist.");
    else
       System.out.println(4 + " Does not exist.");

You just have to take away your System.out.println from your loop.

I expect it will be helpful for you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.