How would one use an enhanced for loop to make an array from imported data in a .txt file?
Please and thank you.
(Examples would be nice)
Enhanced for loop help needed!For-each loops
Page 1 of 1
10 Replies - 1248 Views - Last Post: 15 October 2009 - 12:31 PM
Replies To: Enhanced for loop help needed!
#2
Re: Enhanced for loop help needed!
Posted 13 October 2009 - 10:32 AM
That is a very specific question and would require a lot more than a for each loop. Look into file reading to start.
[rules][/rules]
[rules][/rules]
This post has been edited by syfran: 13 October 2009 - 10:33 AM
#3
Re: Enhanced for loop help needed!
Posted 13 October 2009 - 11:12 AM
So I need to be able to read doubles from a .txt file and put it into an array using a for-each loop. I thought this would be the start, but there is a possible loss of precision. Any help would be nice.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class HeatIndex
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
Scanner LAtemp = new Scanner (new File("LATempTest.txt"));
double LAtempA[] = new double[13];
for (double i : LAtempA)
{
LAtempA[i] = LAtemp.nextDouble();
i++;
}
}
}
#4
Re: Enhanced for loop help needed!
Posted 13 October 2009 - 11:23 AM
Why do you need a for-each loop here? why not the plain old for loop?
#5
Re: Enhanced for loop help needed!
Posted 13 October 2009 - 09:48 PM
for (double i : LAtempA)
{
i = LAtemp.nextDouble();
}
i++ is just incrementing your Double by 1
and i is the Double not an index to it within the array
#6
Re: Enhanced for loop help needed!
Posted 14 October 2009 - 10:14 AM
How am I supposed to assign that counter as the index?
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.InputMismatchException;
public class HeatIndex
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
Scanner LAtemp = new Scanner (new File("LATempTest.txt"));
double LAtempA[] = new double[11];
for (double i : LAtempA)
{
i = LAtemp.nextDouble();
System.out.println(LAtempA[1]);
}
}
}
This post has been edited by Myntal: 14 October 2009 - 10:24 AM
#7
Re: Enhanced for loop help needed!
Posted 14 October 2009 - 02:48 PM
for (double i : LAtempA)
{
i = LAtemp.nextDouble();
}
Think of the variable i in this loop as an intermediary. In case you didn't know, the enhanced for loop (or foreach loop for short) automates a for loop iterating through a collection. The variable declared to the left of the colon, in this case i, obtains the value of the next element, starting at the beginning, from each iteration. So by manipulating/assigning or refferencing i, you are inherently manipulating or refferencing each element in the array (one element per iteration).
#8
Re: Enhanced for loop help needed!
Posted 15 October 2009 - 10:14 AM
So, would you reccomend using a normal for loop. I am still pretty unclear about the foreach loops. I have been researching and I can't find a basic explanation.
#9
Re: Enhanced for loop help needed!
Posted 15 October 2009 - 10:40 AM
A normal for loop can do what a foreach loop can do...so if you're not used to using foreach loop, then just use a normal for loop...
#10
Re: Enhanced for loop help needed!
Posted 15 October 2009 - 12:05 PM
AntonWebsters, on 15 Oct, 2009 - 11:40 AM, said:
A normal for loop can do what a foreach loop can do...so if you're not used to using foreach loop, then just use a normal for loop...
Kind of. A foreach loop basically iterates right through a collection, not pausing for anything, and you can't see the indices. With a regular for loop, you have access to the indices in the array and you have more flexibility in how you iterate (ie., you can increment by 2, 3, 4, etc.; in a foreach loop, you go to the next element on each iteration).
#11
Re: Enhanced for loop help needed!
Posted 15 October 2009 - 12:31 PM
foreach is a special case of a for loop, implemented as sugar. Hence a normal loop can do what a foreach can.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote









|