473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

hi all, help needed!

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
10 1749
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
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
r035198x
13,262 8TB
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

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

Similar topics

0
by: System | last post by:
Hello All, Redhat 9.0 Mysql 3.23.56 ==> Running I want to upgarde to 4.0.13 but this is the error it says: # rpm -Uvh MySQL-server-4.0.13-0.i386.rpm warning: MySQL-server-4.0.13-0.i386.rpm: V3...
8
by: Stephen | last post by:
I am trying to add some code to below to include a datatable and fill the datatable. The reason for doing this is so as I can check to see whether there are any rows returned by the stored...
13
by: Joe Feldman | last post by:
This position is located in the South Bay Area in Northern California. If you are interested please send me your resume in a word .doc so that I can review it. If this does not look like a match,...
0
by: Cindy B | last post by:
Please send your resume and position to Cindy@AtlanticResource.com! I CAN NOT accept candidates that ARE OUTSIDE OF THE US! NO PHONE CALLS PLEASE! Email your resume to me! Position:SQL...
3
by: Wade | last post by:
I would like to install the .Net 1.1 framework on a Web Server running W2K to be able to run ASP.NET files, but I'm not sure where to find the files I need for the .Net framework. I have ".NET...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
5
by: Steve | last post by:
Hi, I am sitting down to design our next set of internal apps. I would like to approach this in a way that would allow me to break logical parts of the application that handle specific tasks...
0
by: ultradiv | last post by:
I have a VB.NET application partly built that produces an xml output (just a file at present) I have a .NET webserver and SQLserver 2000 I need to be able to send the xml to the webserver/database...
28
by: Ian Davies | last post by:
Hello I would appreciate some help from someone who has knowledge of working with css, php javascript and how they interact. Ive been working on a task for the last few days and have started to...
37
by: C_guy | last post by:
Does anyone know of a (hopefully free) tool that can traverse a project and determine which "#include"s are not needed or needed in every .C file? This would be helpful in removing header...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.