hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Java array and string question
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Java array and string question

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-14-2011, 09:57 PM
luxx luxx is offline
Junior Guru Wannabe
 
Join Date: May 2008
Posts: 65

Java array and string question


I have a list of test which I want to place into an array. How would I take text from a file, line by line, and then place each line into its own array element? Something like this:
Bob Hall
Cliff Jackson
Casey Waters

myArray[0] = Bob Hall, myArray[1] = Cliff Jackson, myArray[2] = Casey Waters

Reply With Quote


Sponsored Links
  #2  
Old 09-15-2011, 03:00 AM
almanox almanox is offline
Temporarily Suspended
 
Join Date: Sep 2011
Location: UK
Posts: 161
one of possibilites:

import java.io.*;
import java.util.*;

public class FileToArray {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("testfile");
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> linesList = new ArrayList<String>();
String newLine = null;
while ((newLine = bufferedReader.readLine()) != null) { linesList.add(newLine); }
bufferedReader.close();
String[] lines = linesList.toArray(new String[linesList.size()]);
for (String line : lines) { System.out.println(line); }
System.out.println(lines[1]);
}
}

Reply With Quote
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
strpos() expects parameter 1 to be string, array given in: inco Programming Discussion 8 10-09-2010 09:01 PM
String to array Powi Programming Discussion 6 09-27-2008 10:08 PM
[PHP] Need to convert string to Array CLCook Programming Discussion 1 09-27-2008 12:29 PM
Array to string conversion dale1991 Programming Discussion 6 10-20-2006 07:50 PM
java~converting array to array list gracie Programming Discussion 13 04-29-2003 02:19 AM

Related posts from TheWhir.com
Title Type Date Posted
Jelastic Integrates Java and PHP Cloud Platform with NetBeans IDE Web Hosting News 2013-05-06 13:34:29
Jelastic Releases Plugin for Java Development Environment IntelliJ IDEA Web Hosting News 2012-12-05 11:15:46
Jelastic Java Cloud App Deployment Gets Easier with Eclipse Partnership Web Hosting News 2012-12-17 13:50:32
Jelastic Java PaaS Service Now Enables App Building in the Cloud Web Hosting News 2011-11-18 16:57:16
Java Cloud Platform Jelastic Opens US Operations in Palo Alto, California Web Hosting News 2011-11-07 18:12:18


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?