By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

hi all, help needed!

P: 10
Hi, I have this code :

Expand|Select|Wrap|Line Numbers
  1.  public class test2 
  2. {
  3.  
  4.  
  5. public static void readAccounts()
  6. {
  7. reader1 r = new reader1();
  8. r.readIt();
  9. }
  10.  
  11. public static void main(String args[])
  12.  
  13.     throws java.io.IOException, java.io.FileNotFoundException
  14.     {
  15. String str = ""
  16. ;
  17. String[] lines = str.split ("\\&");
  18. for (int i=0; i < lines.length; i++)
  19. {
  20.  
  21. }
  22. }
  23. }
  24.  
Ok the readAccounts method links to another class:

Expand|Select|Wrap|Line Numbers
  1.  { 
  2. void readIt()
  3. {
  4. printFile("account.txt");
  5.  
  6.  
  7. }
  8.  
  9. void printFile(String fileName)
  10. {
  11. String[] lines = LineIO.readAllLines(fileName);
  12. for (String l : lines)
  13. {
  14. System.out.println(l);
  15. }
  16. }
  17.  
  18. }
  19.  
which reads a text file. My question is how do I get my 1st piece of code to split the strings in the text file? Im stupidly stuck pon this so any help would be welcome!

Thankyou god like people!
Nov 30 '06 #1
Share this Question
Share on Google+
10 Replies


10K+
P: 13,264
Hi, I have this code :

public class test2
{


public static void readAccounts()
{
reader1 r = new reader1();
r.readIt();
}

public static void main(String args[])

throws java.io.IOException, java.io.FileNotFoundException
{
String str = ""
;
String[] lines = str.split ("\\&");
for (int i=0; i < lines.length; i++)
{

}
}
}

Ok the readAccounts method links to another class:

{
void readIt()
{
printFile("account.txt");


}

void printFile(String fileName)
{
String[] lines = LineIO.readAllLines(fileName);
for (String l : lines)
{
System.out.println(l);
}
}

}

which reads a text file. My question is how do I get my 1st piece of code to split the strings in the text file? Im stupidly stuck pon this so any help would be welcome!

Thankyou god like people!
How do you want to split the strings? eg to split a string on space use str.split(" ");
Nov 30 '06 #2

P: 10
How do you want to split the strings? eg to split a string on space use str.split(" ");

sorry, should have specified. The data is seperated by ampersans (&), some of the strings contain numbers which I need toi be able to work with later.

Thanks
Nov 30 '06 #3

10K+
P: 13,264
sorry, should have specified. The data is seperated by ampersans (&), some of the strings contain numbers which I need toi be able to work with later.

Thanks
Then use str.split("&");
Nov 30 '06 #4

P: 10
Then use str.split("&");

Sorry think I may have said something confusing here. I have the code working as it stands. What I want to know is how I now can use the strings gathered from the text files, for instance there are two different accounts in the text file, say I want to print the account names on screen, how do I get the information from the strings? This is where I am stuck.

Thankyou
Nov 30 '06 #5

10K+
P: 13,264
Sorry think I may have said something confusing here. I have the code working as it stands. What I want to know is how I now can use the strings gathered from the text files, for instance there are two different accounts in the text file, say I want to print the account names on screen, how do I get the information from the strings? This is where I am stuck.

Thankyou
Do you want code for reading a file's lines into a string array?
Nov 30 '06 #6

P: 10
Do you want code for reading a file's lines into a string array?
Im not sure, definitly a noob here! From what I understand an Array is kind of like a form of list in Java?. If you have code for that then it may be helpful. Thanks :)
Nov 30 '06 #7

10K+
P: 13,264
Im not sure, definitly a noob here! From what I understand an Array is kind of like a form of list in Java?. If you have code for that then it may be helpful. Thanks :)
An array stores many objects of the same type. Their disadvantage is that you have to know the size of the array (number of objects to store) before you start using one. There is another type of array in java.util called ArrayList which does not have this dis advantage. Here is a program which reads contents of a file into an arraylist and then prints all the lines to the screen.


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import java.io.*;
  4. public class Splitting {
  5. public static void main(String[] args) {
  6. ArrayList list = new ArrayList();
  7. try {
  8. FileReader fr = new FileReader("test.txt");
  9. BufferedReader br = new BufferedReader(fr);
  10. String line = "";
  11. while((line = br.readLine()) != null) { //str=str+append;
  12.     list.add(line);
  13. }
  14. }
  15. catch(IOException iO) {
  16. iO.printStackTrace();
  17. }
  18. for(int i = 0; i < list.size();i++) {
  19.  
  20. //Perhaps this is where you want to manipulate the file contents
  21. System.out.println(list.get(i));
  22. }
  23. }
The file test.txt should be in the same folder as the source file for this code. Alternatively you could store it elsewhere and use eg
Expand|Select|Wrap|Line Numbers
  1.  FileReader fr = new FileReader("C:\test.txt");
  2.  
Nov 30 '06 #8

P: 10
thankyou, that works perfectly, however I still dont understand how I can use data from the text file, say I wanted to add to numbers from the strings together or display just names from the text? i.e this is the text file its reading:

prepaid&123123&Harry Hill&Norwich&3500
contract&246642&Iris Island&London&21-11-1984&1000&15


thanks
Dec 2 '06 #9

P: 10
thankyou, that works perfectly, however I still dont understand how I can use data from the text file, say I wanted to add to numbers from the strings together or display just names from the text? i.e this is the text file its reading:

prepaid&123123&Harry Hill&Norwich&3500
contract&246642&Iris Island&London&21-11-1984&1000&15


thanks
sorry ^BUMP^
Dec 5 '06 #10

10K+
P: 13,264
sorry ^BUMP^
You did not specify exactly how you want to manipulate the file contents


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import java.io.*;
  4. public class Splitting {
  5.  public static void main(String[] args) {
  6.   ArrayList list = new ArrayList();
  7.   try {
  8.    FileReader fr = new FileReader("test.txt");
  9.    BufferedReader br = new BufferedReader(fr);
  10.    String line = "";
  11.    while((line = br.readLine()) != null) { //str=str+append;
  12.     list.add(line);
  13.    }
  14.   }
  15.   catch(IOException iO) {
  16.    iO.printStackTrace();
  17.   }
  18.   for(int i = 0; i < list.size();i++) {
  19.    //Perhaps this is where you want to manipulate the file contents eg print numbers only
  20.    int a = 0;
  21.     String line = (String)list.get(i);
  22.     String[] tokens = line.split("&");
  23.     for(int j = 0; j < tokens.length; j++) {
  24.      //System.out.println(tokens[j]);
  25.      try {
  26.       a = Integer.parseInt(tokens[j]);
  27.       System.out.println(a);
  28.      }
  29.      catch(NumberFormatException nfE) {
  30.      }
  31.     }
  32.    }
  33.  
  34.  
  35.  }
  36. }
  37.  
Dec 5 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.