Simple Array in java

Go To StackoverFlow.com

0

I need to create the following in my main method. Create an array of size 100 doubles. Write a loop to store the number 150.0 - 249.0 in the 100 location.

public class Lab6_2 { 
  public static void main(String[] args) { 
        double[] values; 
        values = new double [100]; 
        double i = 0; 
        for(double i = 150.0; i<249.0;i++){ 
            System.out.println(values[99]); 
        } 
    } 
} 
2012-04-03 23:15
by New2.java
Please show what you've tried so far--I find it impossible to believe that nothing you've found indicates how to create an array of 100 elements, at the very least. Also, please tag this question as homework - Dave Newton 2012-04-03 23:16
Using a loop. - New2.java 2012-04-03 23:18
Lets see your loo - K Mehta 2012-04-03 23:19
Edit the question - Dave Newton 2012-04-03 23:20
why down voting - Sleiman Jneidi 2012-04-03 23:26
I didn't downvote, but most likely because the original was a very poor question and showed neither initiative or research. You've declared the array correctly, but you never store anything in it--the loop should be an array index, and may also be used to help fill the array - Dave Newton 2012-04-03 23:27


2

Firstly, filling the array and printing elements are unrelated concerns, so I would separate them in the code by first filling the array, then printing it. Java offers a way to print an entire array, so remove the current print statement and put this line after the loop:

System.out.println(Arrays.toString(array));

Even if you decide to still print elements one per line (as you are now), you should still do this in a separate loop.

Next, you need to assign a value to each element inside your loop, which you're currently not doing. You must calculate the element value from its index, which in this case is easy:

array[i] = i + 150;
2012-04-03 23:41
by Bohemian
I guess 'Arrays.toString' is too advanced for OP, considering OP is yet to declare & print an array properly - P.P. 2012-04-03 23:46


0

You can do this:

public static void whatever() {
    double value = 150.0d;
    double array[] = new double [100];

    for (int i = 0; i < array.length; i++) {
        array[i] = value;
        System.out.println("Index: " + i + " Value: " + value++);
    }
 }
2012-04-03 23:29
by stzzz1
Thanks for all your help everyone, but It stil doesn't like my print statement. "illegal start of expression and a ")" expected. Also, what is 150.0d does the d represent double - New2.java 2012-04-03 23:38
http://docs.oracle.com/javase/tutorial - stzzz1 2012-04-03 23:43


0

There's only one problem with your code. You're correctly generating the floating point values, but your code doesn't store those values in the array.

What do you need to change in order to store the floating point values in the right position in the array?

2012-04-04 00:25
by Tommy B


0

Couple of things wrong with your code. You're declaring i twice, which you shouldn't do. Also you're using i as an iterator so it should be an int, not a double and you should declare it inside the for statement. It should also start at 0, not 150, and continue as long as it is less than the length of the array, because while the length of the array is 100, the last element is actually 99.

Also your homework isn't entirely clear, but it sounds like you're supposed to store the numbers 150.0 - 249.0 in each element of the array? But all your for loop is doing is printing out the last element of the array 100 times, and that element is empty because you never put anything in there.

To assign a number to the array you would simply write:

myArray[0] = somevalue;

That would assign somevalue to the first element in the array. But you want to iterate through the elements of the array so instead of 0 you would use i.

2012-04-04 03:00
by David DeMar


-2

I'm not totally familiar with java, but I know C++ and they're very similar.

double array[] = new double[100];
double value = 150.0;
for (int i = 0; i < 100; i++) {
    array[i] = value+i;
    System.out.println(value+i);
}

hope that helps.

your problem above was that you started i as 150.0. You need to use a counter variable to increment through your array. You're just printing the 99th element of the array (which you never set) 99 times.

2012-04-03 23:23
by Michael Dorst
Booooooooooooo. IMO spoon-feeding an answer under these circumstances is a poor judgement call, particularly before the OP has had a chance to properly fulfill their responsibility. (And what's the point of providing a non-Java answer? I didn't downvote, but I sure don't blame whoever did. - Dave Newton 2012-04-03 23:24
what are you talking about @DaveNewton? I just answered the question as best I could, knowing that C++ is practically the same thing. the only difference is the print statement here - Michael Dorst 2012-04-03 23:31
@MichaelDorst I'd have to agree with Dave on this. When someone's trying to learn a language, syntax is just as tricky as the logic. The OP would get confused by cout <<. Also, the way you define arrays is different between Java and C++. The OP also wouldn't know why you're returning 0. Not the downvoter either, but your answer does have quite a few flaws - K Mehta 2012-04-03 23:36
Is it better now that I changed those things @KshitijMehta - Michael Dorst 2012-04-03 23:40
Your analysis of what the OP is doing is also incorrect; nothing is being stored in the array at all. Mixing languages in an answer is more confusing than anything else; your edits helped some, but meh. If you insist on spoon-feeding, at least be right - Dave Newton 2012-04-03 23:40
@DaveNewton, he said: "Write a loop to store the number 150.0 - 249.0 in the 100 location[s]." (Not sure what he's putting in the last one though - Michael Dorst 2012-04-03 23:45
Yep. And you said "you're just putting each value into the 99th slot", which he is not doing. And the 99th slot is the last one - Dave Newton 2012-04-03 23:46
You're right, he's just printing the value of the 99th slot, which he never set. My bad, I misread it - Michael Dorst 2012-04-03 23:47
And FWIW, since it's a beginner, it's best to use canonical Java, which puts the [] after the type, not after the variable name - Dave Newton 2012-04-03 23:49
150-249 is 99 values, not 100. I was wondering what the 100th element would be used for, given his spec - Michael Dorst 2012-04-03 23:50
I wasn't aware that one could do that. I always thought it was silly to have the brackets after the name, as it's really denoting the type - Michael Dorst 2012-04-03 23:54
Thanks for everything, really appreciate the fast replys. I think what was really tripping me up was using the int variable inside the for loop. I was in the mindset of only using doubles the entire time - New2.java 2012-04-04 00:15