10 Replies - 1474 Views - Last Post: 15 October 2009 - 12:31 PM Rate Topic: -----

#1 Myntal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-October 09

Enhanced for loop help needed!

Post icon  Posted 13 October 2009 - 10:30 AM

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)
Is This A Good Question/Topic? 0
  • +

Replies To: Enhanced for loop help needed!

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

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]

This post has been edited by syfran: 13 October 2009 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 Myntal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-October 09

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++;
		  
	  }
	  
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

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?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#6 Myntal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-October 09

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

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

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).
Was This Post Helpful? 1
  • +
  • -

#8 Myntal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-October 09

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.
Was This Post Helpful? 0
  • +
  • -

#9 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

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...
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Enhanced for loop help needed!

Posted 15 October 2009 - 12:05 PM

View PostAntonWebsters, 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).
Was This Post Helpful? 0
  • +
  • -

#11 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1