5 Replies - 1399 Views - Last Post: 14 October 2012 - 12:52 PM Rate Topic: -----

#1 beatc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 12

Read from file - to Array

Posted 13 October 2012 - 01:18 AM

Sup guys, i need help on storing data read from a txt file to an array. I'm confused right now so i just post what i've done.

Revenue.txt :
Excel IT;50000;55000;65000;60000;54600;47800;67000;55000;58900;54000;60000;63400
Excel Energy;150000; 255000;265000;260000;354600;347800;467000;355OO0;4 58900;4 54OOO;46OOO0;4634O0
Excel Edu;55000;65000;75000;70000;64600;57800;77000;65000; 68900;64000; 68000; 65400
Excel Land;250000;255000;365000;360000;454600;447800;567000; 555000; 658900;654000; 60000;763400


Java Code:
import java.io.*;
import java.util.*;
public class ExcelGroup {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		File Rev = new File("Revenue.txt");
		Scanner inRev = new Scanner(Rev);
		String read = null;
		String name = null;
		double[] revenue = new double[12];
		int i = 0;
		File outRev = new File("FirstHalfYear.txt");
		PrintWriter half = new PrintWriter(outRev);
		while(inRev.hasNext()){
			read = inRev.nextLine();
			StringTokenizer st = new StringTokenizer(read,";");
			System.out.print(st.nextToken());
			name = st.nextToken();
			System.out.print(st.nextToken());
			revenue[i] = Double.parseDouble(st.nextToken());	
			half.print(revenue[i] + " ");
			i++;		
		}
		inRev.close();
		half.close();
	}

}



Additional info, I need to store the number into an array of size 12 excluding the name.

Is This A Good Question/Topic? 0
  • +

Replies To: Read from file - to Array

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Read from file - to Array

Posted 13 October 2012 - 04:29 AM

StringTokenizer is deprecated. The String.split() method is a great tool for what you're trying to do. There are also other ways to accomplish what you're trying to do.

After you've considered split() as an option and made corresponding, if any, changes to your code, come back and explain what you're confused about. We can't read the question mark bubble above your head.
Was This Post Helpful? 1
  • +
  • -

#3 beatc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 12

Re: Read from file - to Array

Posted 13 October 2012 - 06:18 AM

Sorry if my problem was not that clear enough. Well, what im tying to achieve here is to read and store those each line of numbers to array so i can use it to output into firsthalfyear.txt. Unfortunately, i don't how to that. It is inefficient to declare 12 integers just to each number on each line.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Read from file - to Array

Posted 13 October 2012 - 06:22 AM

Quote

It is inefficient to declare 12 integers just to each number on each line.

I'm not sure how you're measuring efficiency, but if you don't want to do it that way, then don't. An obvious alternative is to read each item in the file one at a time and parse/assign to your array as you wish.

What about that confuses you?
Was This Post Helpful? 0
  • +
  • -

#5 beatc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-October 12

Re: Read from file - to Array

Posted 13 October 2012 - 04:14 PM

That thing what you've said in the said line is what im confused about. i dont know how to do it.

sorry, it's the second line.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,169
  • Joined: 20-September 08

Re: Read from file - to Array

Posted 14 October 2012 - 12:52 PM

I'm intrigued to know why you only want to collect the numbers, but if that's really all you need, i would use List<Integer> or even List<List<Integer>>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1