Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java - Take For Loop Variables And Put Into 2 Separate Array

array for loop arrays loop

  • Please log in to reply
10 replies to this topic

#1 900900

900900

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Learning:Java, C#, PHP, Lua

Posted 19 June 2012 - 10:12 AM

In this assignment I have to modify a small little program I made to output two sets of solutions into an array.

The program takes the temperature in Fahrenheit then calculates it's Celcius temperature, then outputs them next to each other. It adds 20 to Fahrenheit after each loop. I used a for loop for this and have it just go up until Fahrenheit hits 300.

What I need to do, is get the Fahrenheit temperatures in one array, and the Celsius ones in another.

The problem I didn't understand is:

1) I know my array will need 15 elements (since Fahrenheit starts at 0 and goes up by 20 until it reaches 300) - should I just go ahead and declare each array to [15] or is there a way to leave the array blank [] so it can "dynamically" size up as elements are added?

2) I know I will need to put each variable into the array each time it runs through the for loop. Could someone maybe point more towards how I should structure my loop? I am 99% sure the array loop would have to run inside the for loop calculating the information (since the variables change each loop) - but I am just really lost on how to structure that.

3) Am I going in the right directions or completely off track?

Then I have to spit the two array back out (which I did figure out how to do with a for loop so that's no problem)

Thanks guys, hope to get some good insight

public class ConversionTable {
public static  void main(String[] args) {

  double fahr;
  double cel;
  double[] tempList1;
  double[] tempList2;

  tempList1 = new double[15];
  tempList2 = new double[15];


  System.out.print("Fahrenhit |");
  System.out.print(" Celsius");

  System.out.println("\n----------------------");

  for(fahr=0;fahr<=300;fahr+=20)
  {
   cel = (5.0/9.0)*(fahr-32.0);
   tempList1[] = fahr;
   tempList2[] = cel;
  }

// Below is a for loop I was trying to make for the array
  for (int i = 0; i > tempList1.length; i++) {

   }

  System.out.println(tempList1.length);
  System.out.println(tempList2.length);

}
}
}

  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 June 2012 - 10:39 AM

What you did is ok. But if you want to use dynamic array, you can think to use List. So the declaration is simple as
List<double> list1 = new ArrayList<double>();
List<double> list2 = new ArrayList<double>();

// To add item,
list1.add(10.);

// To access item
double value = list1.get(0);


Note that, you made two mistakes
1. You should sub-script the array -- wrong: tempList1[] = fahr; -- should be tempList1[0] = fahr;
2. In the loop
for (int i = 0; i > tempList1.Length; i++) {
}
// the condition should be i < tempList1.Length.

  • 0

#3 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 19 June 2012 - 10:41 AM

1) You can either do a static size of the arrays as now, or use an ArrayList inside your program and dynamically add() each value, and in the end, return the arraylist's toArray() to have a proper array returned.

2) I think your for-loop is perfect. It's doing exactly what you're told to do. I just wonder if the []is ok when setting a value to the array, I think you need the index inside the brackets.

3) I think you're absolute on the right way.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#4 Yonatan

Yonatan

    CC Regular

  • Member
  • PipPipPip
  • 37 posts
  • Location:Israel
  • Programming Language:C, Java, C++, C#, JavaScript, PL/SQL, Visual Basic .NET
  • Learning:Python, JavaScript

Posted 19 June 2012 - 01:45 PM

You are missing just a small fix.

when assigning a value to an array object[] (f.e: int[] arr),
you cannot write: arr[] = some_value.
you need to enter an index (int) for example:

double[] arr = new double[15];
int index = 2;
arr[index] = 3.0;

this will enter the value 3.0 into the array called "arr" at the index 2.

I think that in this case I will recommend using arrays and not other data structures such as lists, unless you understand the other data structures concept and usage cases.
  • 0

#5 900900

900900

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Learning:Java, C#, PHP, Lua

Posted 19 June 2012 - 05:53 PM

Thanks for the responses everyone!

I guess for this purpose (since it's a beginning assignment on arrays and my book doesn't note array lists until later) I will keep it static. So I declare my arrays and I have the loop to calculate the different temperatures. I took a bit of what Orjan said and and came up with this (small steps forward)

I have at the end of the code 3 for loops. The last one will print the array data. The two above it should populate each array with the cel/fahr variables at each given loop state for 15 loops.

The problem now is can I make all this happen from within the "Calculate the temperatures" for loop? I mean essentially I would NEED to have the arrays populated in this loop correct? (since the variables change each loop through)

So the trouble now is taking the array filling loops and mushing it in with the calculation loop somehow. Nested loops? Use a different loop? Help!

Thanks again

public class ConversionTable {
public static  void main(String[] args) {
  double fahr;
  double cel;
  double[] tempList1;
  double[] tempList2;
  tempList1 = new double[15];
  tempList2 = new double[15];

  System.out.print("Fahrenhit |");
  System.out.print(" Celsius");
  System.out.println("\n----------------------");
  //Calculate the temperatures
  for(fahr=0;fahr<=300;fahr+=20)
  {
   cel = (5.0/9.0)*(fahr-32.0);
  }
  // Fill first array with fahrenhit temperature values
  for (int i = 0; i < tempList1.length; i++) {
   tempList1[i] = fahr;
   }

  //Fill second array with celsius temperature values
for (int i = 0; i < tempList2.length; i++) {
   tempList2[i] = fahr;
  }
//Print out the array values
  for (int i = 0; i < tempList2.length; i++) {
   System.out.print(tempList`[i] + " | " + tempList2[i]);
   }


}
}

  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 June 2012 - 06:55 PM

As I said, what you wrote in your original post is perfect. Note, cel and fahr variable is changing in each iteration of the loop of the loop -- this is what you said and this is right. That's why you can use the calculation and filling the array in a single loop. The only problem was with your array sub-scripting.
int i = 0;
for(fahr=0;fahr<=300;fahr+=20, i++)
  {
   cel = (5.0/9.0)*(fahr-32.0);
   tempList1[i] = fahr;
   tempList2[i] = cel;
  }


Now you just need to print it. You can do that in an extra loop (though you can do that even in the same loop).
for (int i = 0; i > tempList1.length; i++) {
  System.out.println(tempList1[i]);
  System.out.println(tempList2[i]);
}

Or, am I missing something?
  • 0

#7 900900

900900

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Learning:Java, C#, PHP, Lua

Posted 19 June 2012 - 07:07 PM

As I said, what you wrote in your original post is perfect. Note, cel and fahr variable is changing in each iteration of the loop of the loop -- this is what you said and this is right. That's why you can use the calculation and filling the array in a single loop. The only problem was with your array sub-scripting.

int i = 0;
for(fahr=0;fahr<=300;fahr+=20, i++)
  {
   cel = (5.0/9.0)*(fahr-32.0);
   tempList1[i] = fahr;
   tempList2[i] = cel;
  }


Now you just need to print it. You can do that in an extra loop (though you can do that even in the same loop).
for (int i = 0; i > tempList1.length; i++) {
  System.out.println(tempList1[i]);
  System.out.println(tempList2[i]);
}

Or, am I missing something?


Ah okay. No you're fine. I was confused because I thought if that was right I would have needed to done

tempList1[i] = fahr;
   tempList2[i] = cel;

But then I noticed how would the "i" increase with each loop if the for loop has nothing to to do it with. So it makes sense now.

The array isn't stopping at 15 based on a loop control "i".. the calculation loop for the temperature is the actual kind of "counter" for the array (stopping once it hits 300, which fits 15 perfectly)

What would happen if I set:

for(fahr=0;fahr<=350;fahr+=20)

would the program refuse to run? Or index out of bounds (i think) error?
  • 0

#8 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 June 2012 - 07:14 PM

Yeah, in for first for loop, the loop is not stopping based on i variable but 'fahr <= 300' condition. Note that you need 16 cells in the array -- not 15. So the right code will be as follows if you use array.
double[] tempList1 = new double[16];
double[] tempList2 = new double[16];

int i = 0;
for(fahr=0;fahr<=300;fahr+=20, i++)
  {
   cel = (5.0/9.0)*(fahr-32.0);
   tempList1[i] = fahr;
   tempList2[i] = cel;
  }

for (i = 0; i > tempList1.length; i++) {
  System.out.println(tempList1[i]);
  System.out.println(tempList2[i]);
}

But if you use List instead of array, it will be as follows.
List<double> tempList1 = new ArrayList<double>();
List<double> tempList2 = new ArrayList<double>();

int i = 0;
for(fahr=0;fahr<=300;fahr+=20, i++)
  {
   cel = (5.0/9.0)*(fahr-32.0);
   tempList1.add(fahr);
   tempList2.add(cel);
  }

for (i = 0; i > tempList1.size(); i++) {
  System.out.println(tempList1.get(i));
  System.out.println(tempList2.get(i));
}

  • 0

#9 Yonatan

Yonatan

    CC Regular

  • Member
  • PipPipPip
  • 37 posts
  • Location:Israel
  • Programming Language:C, Java, C++, C#, JavaScript, PL/SQL, Visual Basic .NET
  • Learning:Python, JavaScript

Posted 19 June 2012 - 11:30 PM

---EDIT:
Missed the i++ in kernelcoder first for increment section (Too early here :]).
That should work fine, although for a beginner assignment do not use Lists.
  • 0

#10 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 June 2012 - 11:35 PM

Note that we are increasing i in the for-loop's 3'rd section -- for(fahr=0;fahr<=300;fahr+=20, i++). So you don't need to increment again at the end of loop.
  • 0

#11 900900

900900

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Learning:Java, C#, PHP, Lua

Posted 24 June 2012 - 08:57 AM

Cool thanks guys! Helped me understand it more!
  • 0





Also tagged with one or more of these keywords: array, for loop, arrays, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download