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 1750
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| | |