6 Replies - 274 Views - Last Post: 29 November 2012 - 04:53 AM Rate Topic: -----

#1 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Best Approach to reading in

Posted 28 November 2012 - 04:56 PM

Hey,I have to read in Integers from a file.I want to pass each integer to a method in turn.The methods that I am passing the integer too has only one parameter.What is the best way to do this.I want to do this so I don't need to type out statements to search for each number from the text file.

//This is inside a main method
Search S = new Search(110, 167);
//Creates my search array and hash arrays

S.find(x);

This post has been edited by RourkeOkeffe: 28 November 2012 - 04:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Best Approach to reading in

#2 jjallenjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 07-May 11

Re: Best Approach to reading in

Posted 28 November 2012 - 05:16 PM

Are you passing them in order? I would use a scanner and nextInt
Was This Post Helpful? 0
  • +
  • -

#3 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Best Approach to reading in

Posted 28 November 2012 - 05:26 PM

I am going to be passing them to the method in the order they appear in the file.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

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

Re: Best Approach to reading in

Posted 28 November 2012 - 07:40 PM

so:

while(scan.hasNextInt()) {
    foo(scan.nextInt());
}



what else do you want ?
Was This Post Helpful? 1
  • +
  • -

#5 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Best Approach to reading in

Posted 29 November 2012 - 04:38 AM

How can I then read the integers from the start to be called by a different method
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

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

Re: Best Approach to reading in

Posted 29 November 2012 - 04:49 AM

What do you mean ?

    for(int firstFive = 0; firstFive < 5; ++firstFive) 
          firstFoo(scan.nextInt());
    while(scan.hasNextInt()) {
          ptherFoo(scan.nextInt());
    }


Was This Post Helpful? 0
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Best Approach to reading in

Posted 29 November 2012 - 04:53 AM

pbl's code is all you need. To read them again, you don't. Store the integers you have read in first to an Array (or ArrayList if you don't know the amount of integers). You are then free to use the integers as you like.

Perhaps something like, extending pbl's code: (Will need to be changed depending on the context of use)

		ArrayList<Integer> myInts = new ArrayList<>();
		while(scan.hasNextInt()) {
		    myInts.add(scan.nextInt());
		}
		for (Integer i : myInts)
			foo(i);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1