5 Replies - 408 Views - Last Post: 05 May 2012 - 11:17 AM Rate Topic: -----

#1 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Can someone show an example of how to load data into an array

Posted 04 May 2012 - 04:51 PM

Can someone show an example of how to load data into an array using a txt file?

I am having trouble loading data into an array.

This post has been edited by bank217: 04 May 2012 - 04:52 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Can someone show an example of how to load data into an array

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Can someone show an example of how to load data into an array

Posted 04 May 2012 - 04:54 PM

Ok. I create an array of int, like so:
int numbers[] = new int[10];
for (int i=0, i < 10, i++){
numbers[i] = 5;
}

for (int i=0, i < 10, i++){
System.out.println(numbers[i];
}


Unless I've done it wrong, output should be:
5
5
5
5
5
and so on.
Was This Post Helpful? 0
  • +
  • -

#3 omfgkittenz  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-May 11

Re: Can someone show an example of how to load data into an array

Posted 04 May 2012 - 06:01 PM

Ok so here we go,
File f = new File("text.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
ArrayList<String> list = new ArrayList<String>();
String s = "";
while(b.ready())
 s += b.readLine();

//after this just use s.substring & s.index of to incrementally move through your string
//while storing each substring in your array
//i recommend using ArrayList and then using .toArray()


hope this helped
-kittenz
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

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

Re: Can someone show an example of how to load data into an array

Posted 04 May 2012 - 07:16 PM

You mean an array of lines ?

File f = new File("text.txt");
Scanner scan = new Scanner(new File("myFile.txt"));
ArrayList<String> list = new ArrayList<String>();
while(scan.hasNextLine())
 list.add(scan.nextLine());


Was This Post Helpful? 0
  • +
  • -

#5 omfgkittenz  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-May 11

Re: Can someone show an example of how to load data into an array

Posted 04 May 2012 - 11:43 PM

View Postpbl, on 04 May 2012 - 07:16 PM, said:

You mean an array of lines ?

File f = new File("text.txt");
Scanner scan = new Scanner(new File("myFile.txt"));
ArrayList<String> list = new ArrayList<String>();
while(scan.hasNextLine())
 list.add(scan.nextLine());



yeah assuming that your data was stored line by line and not seperated by commas etc
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

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

Re: Can someone show an example of how to load data into an array

Posted 05 May 2012 - 11:17 AM

We are really in hypotithecal situations and the OP didn't mentionned what it really wants
If it is a coma separated data in each line:

File f = new File("text.txt");
Scanner scan = new Scanner(new File("myFile.txt"));
ArrayList<String> list = new ArrayList<String>();
while(scan.hasNextLine()) {
  String[] token = scan.nextLine().split(",");
  for(String str : token)
     list.add(str);
}


or if he wants the array per line
File f = new File("text.txt");
Scanner scan = new Scanner(new File("myFile.txt"));
ArrayList<String[]> list = new ArrayList<String[]>();
while(scan.hasNextLine())
  list.add(scan.nextLine().split(",");



but until the OP specifies more clearly what he wants this discussion is useless
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1