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!
10 1749
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(" ");
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
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("&");
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
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?
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 :)
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. -
-
import java.util.*;
-
import java.io.*;
-
public class Splitting {
-
public static void main(String[] args) {
-
ArrayList list = new ArrayList();
-
try {
-
FileReader fr = new FileReader("test.txt");
-
BufferedReader br = new BufferedReader(fr);
-
String line = "";
-
while((line = br.readLine()) != null) { //str=str+append;
-
list.add(line);
-
}
-
}
-
catch(IOException iO) {
-
iO.printStackTrace();
-
}
-
for(int i = 0; i < list.size();i++) {
-
-
//Perhaps this is where you want to manipulate the file contents
-
System.out.println(list.get(i));
-
}
-
}
-
}
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 - FileReader fr = new FileReader("C:\test.txt");
-
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
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^
sorry ^BUMP^
You did not specify exactly how you want to manipulate the file contents -
-
import java.util.*;
-
import java.io.*;
-
public class Splitting {
-
public static void main(String[] args) {
-
ArrayList list = new ArrayList();
-
try {
-
FileReader fr = new FileReader("test.txt");
-
BufferedReader br = new BufferedReader(fr);
-
String line = "";
-
while((line = br.readLine()) != null) { //str=str+append;
-
list.add(line);
-
}
-
}
-
catch(IOException iO) {
-
iO.printStackTrace();
-
}
-
for(int i = 0; i < list.size();i++) {
-
//Perhaps this is where you want to manipulate the file contents eg print numbers only
-
int a = 0;
-
String line = (String)list.get(i);
-
String[] tokens = line.split("&");
-
for(int j = 0; j < tokens.length; j++) {
-
//System.out.println(tokens[j]);
-
try {
-
a = Integer.parseInt(tokens[j]);
-
System.out.println(a);
-
}
-
catch(NumberFormatException nfE) {
-
}
-
}
-
}
-
-
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| | |