Read text file and split up. How???
Can anyone tell me how to split up a string into words.
The string is: "me and my cat both like milk and cookies"
I need read through the file and diplay the biggest word(s) and the smallest word(s).
I would appriciate help on this as i am really stumped
Thanks
..::M::..
p.s. I also need to be able to do this in C++ but if i know it in java i think the C++ will be simple
String[] words = "your sentencehere".split("\\s+");
and now you have array of strings, each containigwords from text.
this is helpful, is there a version in C++ tooThe short answer for C++ is "no". Unless of course you install one of the regexp libraries available. But there is no native language way to do it like in Java.
However if they words always have spaces and always will have spaces you can write your C++ code like this:
vector <string> words;
ifstream inFile( "file.txt" );
string temp;
inFile >> temp;
words.push_back( temp );
PS I have not tested this code in anyway but it should be simple enough
Similar Messages
-
Read text file and insert into MySQL
Dears,
I need to read text file and then insert the data in the correct column in the MySQL database
example
I have the following text file:
field1=1234 field2=56789 field3=444555
field1=1333 field2=2222 field3=333555
and so on and so forth ,,note that all rows are identical and just the filed value is changed(there is a dilemeter between fields)
how can I read field1,field2 and field3 from text file and insert them in the correct table and column in the database.....
any help?????
thanks for your cooperation
Best RegarsSure.
Which part don't you understand?
1. Reading a text file
2. Parsing the text file contents.
3. Relational databases and SQL.
4. How to create a database.
5. How to connect to a database in Java.
6. How to insert records into the database in Java.
7. How to map Java objects to records in a database.
This is a pretty nice list. Solve complex problems by breaking them into smaller ones.
% -
Reading text file and display in the selectOnechoice list item In ADF.
Hi,
I have a requirement to read the text field which have list of strings and that string display in the SelectOneChoice List item component on page load.
I am using Jdeveloper 11.1.2.3 version.
Any suggestion will highly appreciated..
Thanks in advance.
RegardsHi,
Google will produce you with hints on how to read content of a file from Java (ideally the file uses some delimiter). Then in a managed bean, you read the file and save its content in a list of SelectItem. So your managed bean should have the following property and setter/getter pairs
ArrayList<SelectItem> listFromFile = new ArrayList<SelectItem>();
public void setListFromFile(ArrayList l){ //you don't need this }
public ArrayList<SelectItem> getListFromFile(){
//read file content and iterate over the file list entries
for(i=0, i < fileContent.length, ++i){
SelectItem si = new SelectItem();
si.setValue(... the value to update the list of value with ...);
si.setLabel("... the label to show in the list ...");
listFromFile.add(si);
return listFromFile;
}The af:selectOneChoice component should look as follows
<af:selectOneChoice id=".." value="...attribute to update with selection ..." ...>
<f:selectItems value="#{managedBean.listFromFile}"/>
</af:selectOneChoice>Frank -
Read Text file and count occurences of certain string
Hello,
I have a text file with lines of data in it. I would like to read this text file and count how many lines match a certain string of text.
For example my text file has this data in it.
dog,blue,big
dog,red,small
dog,blue,big
cat,blue,big
If the certain string of text is "dog,blue,big" then the count would return "2".
Thanks for your helpHello,
Thank you for your post.
I am afraid that the issue is out of support range of VS General Question forum which mainly discusses the usage issue of Visual Studio IDE such as
WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System
and Visual Studio Editor.
I am moving your question to the moderator forum ("Where is the forum for..?"). The owner of the forum will direct you to a right forum.
In addition, if you are working with Windows Forms app. please consult on Windows Forms Forum:http://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms
If you are working with WPF app, please consult on WPF forum:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf
If you are working with ASP.NET Web Application, I suggest that you can consult your issue on ASP.NET forum:
http://forums.asp.net/
for better solution and support.
Visual Studio Language Forums:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=vslanguages
Best regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey. -
Reading text file and output (to stdout) a list of the unique words in the
Hi,
I have a main method as
main.java
package se.tmp;
public class Main
public static void main( String[] args )
WordAnalyzer.parse( args[0] );
and text file as
words.txt
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog
the requirement is like
I need create this WordAnalyzer class, implement the parse method, and then commit the file. This method takes a single parameter, the filename of the file to parse. The method should read this file and output (to stdout) a list of the unique words in the file along with the number of times each appears in the file.
Can anyone please help me on this?
Thanks.Where are you having problems?
-
I am attempting to read in blocks of data from a text file separated by a blank line. When the BufferedReader hits the first blank line it ends. I retyped the test text file and still cannot get it to continue to read the entire file; just the first block.
fReader = new FileReader( textFile );
bReader = new BufferedReader( fReader );
line = bReader.readLine();
while( line ! = null ){
do stuff;
bReader.close()
The format of my textFile is:
100
some name
type 1
type 2
101
some other name
type 1
type 2
Any suggestions would be greatly appreciated.Hi,
I saw that BigDaddyLoveHandles already helped you a lot but I wanted to add a little something to that, because from what I understood you need to keep the blocks together.
public static void main(String[] args) {
BufferedReader theReader = null;
try {
theReader = new BufferedReader(new FileReader("pathToYourFile"));
String line;
StringBuilder theBuilder = new StringBuilder();
while ((line = theReader.readLine()) != null){
if (line.isEmpty()){
doStuff(theBuilder.toString());
theBuilder = new StringBuilder();
}else {
theBuilder.append(line).append(System.getProperty("line.separator"));
doStuff(theBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
} finally{
if (theReader != null)
try {
theReader.close();
} catch (IOException e) {
e.printStackTrace();
}This actually allows your method doStuff to get the whole block vs. line by line.
Regards -
How to read text file and store in database
Respected All,
I have text file on my computer.
I am using forms6i and oracle 9i.
I want that when i press button then specifice world in text file is read and dispaly on form to be stored in database.
Please provide some solution.
Kind RegardsHello,
Study the functions of the TEXT_IO() package
http://www.oracle.com/webapps/online-help/forms/10g/state?navSetId=_&navId=3&vtTopicFile=f1_help/oraini/c_text_io.html&vtTopicId=
Francois -
How to read a text file and write text file
Hello,
I have a text file A look like this:
0 0
0 A B C
1 B C D
2 D G G
10
1 A R T
2 T Y U
3 G H J
4 T H K
20
1 G H J
2 G H J
I want to always get rid of last letter and select only the first and last line and save it to another text file B. The output should like this
0 A B
2 D G
1 A R
4 T H
1 G H
2 G H
I know how to read and write a text file, but how can I select the text when I am reading a text file. Can anyone give me an example?
Thank youIf the text file A look like that
0 0
0 3479563,41166 6756595,64723 78,31 1,#QNAN
1 3479515,89803 6756588,20824 77,81 1,#QNAN
2 3479502,91618 6756582,6984 77,94 1,#QNAN
3 3479516,16334 6756507,11687 84,94 1,#QNAN
4 3479519,14188 6756498,54413 85,67 1,#QNAN
5 3479525,61721 6756493,89255 86,02 1,#QNAN
6 3479649,5546 6756453,21824 89,57 1,#QNAN
1 0
0 3478762,36013 6755006,54907 54,8 1,#QNAN
1 3478756,19538 6755078,16787 53,63 1,#QNAN
2 0
3 0
N 0
I want to read the line that before and after 1 0, 2 0, ...N 0 line to arraylist. I have programed the following code
public ArrayList<String>save2;
public BufferedWriter bufwriter;
File writefile;
String filepath, filecontent, read;
String readStr = "" ;
String[]temp = null;
public String readfile(String path) {
int i = 0;
ArrayList<String> save = new ArrayList <String>();
try {
filepath = "D:\\thesis\\Material\\data\\CriticalNetwork\\test3.txt";
File file = new File(filepath);
FileReader fileread = new FileReader(file);
BufferedReader bufread = new BufferedReader(fileread);
this.read = null;
// read text file and save each line content to arraylist
while ((read = bufread.readLine()) != null ) {
save.add(read);
// split each arraylist[i] element by space and save it to String[]
for(i=0; i< save.size();i++){
this.temp = save.get(i).split(" ") ;
// if String[] contain N 0 such as 0 0, 1 0, 2 0 then save its previous and next line from save arraylist to save2 arraylist
if (temp.equals(i+"0")){
this.save2.add(save.get(i));
System.out.println(save2.get(i));
} catch (Exception d) {
System.out.println(d.getMessage());
return readStr;
My code has something wrong. It always printout null. Can anyone help me?
Best Regards,
Zhang -
Hi All,
powershell script Method required to read a value from registry and then taking the backup of that values in some text file.
For example the hive is
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
and under path i need to take back up of values in some text file and then put some value in the registry after back is taken in text file.
Also how to read the text file values so that we can again write to registry hive back from the back up text file.
Your help is much appreciated.
Umeed4uI think you need to read this first:
http://social.technet.microsoft.com/Forums/scriptcenter/en-US/a0def745-4831-4de0-a040-63b63e7be7ae/posting-guidelines?forum=ITCG
Don't retire TechNet! -
(Don't give up yet - 12,830+ strong and growing) -
How to read the contents of a text file and populate the data in a table ?
Hello All,
Can anyone advise on how to acheieve the above ? I am trying to read in a text file (CSV) and have the contents populated to the respective UI elements in a table. Any help is greatly appreciated.
from
Kwok WeiHi,
Let us consider you have list of names(Seperated by delimeter) in a text file and you want to display in a table.
1. Create Context Node "Names" and context attribute "Name"
2. Create Table and bind to the above context.
3.Write the following code in the "Init method.
try{
FileReader f =new FileReader("");
BufferedReader r=new BufferedReader(f);
String names=r.readLine();
Vector Names=new Vector();
// Use Tokenizer and store all the names i a vector//
for(int i=0;i<Names.size();i++){
IPrivate<<VieName>>.INameElement ele=wdContext.createNameElement();
ele.set<<Name>>( Names.get(i).toString());
wdContext.NodeName().addElement(ele);
Regards, Anilkumar
Message was edited by: Anilkumar Vippagunta -
hey ppl
i have a datagrid in my form.i need to read input(fields..sort of a database) from a text file and display its contents in the datagrid.
how can this be done.. and also after every few seconds reading event should be re executed.. and that the contents of the datagrid will keep changing as per the changes in the file...
please help as this is urgent and important.. if possible please provide me with an example code as i am completely new to flex...
thanks.....It's not possible to read from a file without using classes from the core API*. You'll have to get clarification from your instructor as to which classes are and are not allowed.
[http://java.sun.com/docs/books/tutorial/essential/io/]
*Unless you write a bunch of JNI code to replicate what the java.io classes are doing. -
Where does this java program read a text file and how does it output the re
someone sent this to me. its a generic translator where it reads a hashmap text file which has the replacement vocabulary etc... and then reads another text file that has what you want translated and then outputs translation. what i don't understand is where i need to plugin the name of the text files and what is the very first line of code?
[code
package forums;
//references:
//http://forums.techguy.org/development/570048-need-write-java-program-convert.html
//http://www.wellho.net/resources/ex.php4?item=j714/Hmap.java
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
public class Translate {
public static void main(String [] args) throws IOException {
if (args.length != 2) {
System.err.println("usage: Translate wordmapfile textfile");
System.exit(1);
try {
HashMap words = ReadHashMapFromFile(args[0]);
System.out.println(ProcessFile(words, args[1]));
} catch (Exception e) {
e.printStackTrace();
// static helper methods
* Reads a file into a HashMap. The file should contain lines of the format
* "key\tvalue\n"
* @returns a hashmap of the given file
@SuppressWarnings("unchecked")
private static HashMap ReadHashMapFromFile(String filename) throws FileNotFoundException, IOException {
BufferedReader in = null;
HashMap map = null;
try {
in = new BufferedReader(new FileReader(filename));
String line;
map = new HashMap();
while ((line = in.readLine()) != null) {
String[] fields = line.split("\\t", 2);
if (fields.length != 2) continue; //just ignore "invalid" lines
map.put(fields[0], fields[1]);
} finally {
if(in!=null) in.close(); //may throw IOException
return(map); //returning a reference to local variable is safe in java (unlike C/C++)
* Process the given file
* @returns String contains the whole file.
private static String ProcessFile(Map words, String filename) throws FileNotFoundException, IOException {
BufferedReader in = null;
StringBuffer out = null;
try {
in = new BufferedReader(new FileReader(filename));
out = new StringBuffer();
String line = null;
while( (line=in.readLine()) != null ) {
out.append(SearchAndReplaceWordsInText(words, line)+"\n");
} finally {
if(in!=null) in.close(); //may throw IOException
return out.toString();
* Replaces all occurrences in text of each key in words with it's value.
* @returns String
private static String SearchAndReplaceWordsInText(Map words, String text) {
Iterator it = words.keySet().iterator();
while( it.hasNext() ) {
String key = (String)it.next();
text = text.replaceAll("\\b"+key+"\\b", (String)words.get(key));
return text;
* @returns: s with the first letter capitalized
String capitalize(String s)
return s.substring(0,0).toUpperCase() + s.substring(1);
}without what arguments?Without any arguments. If there are no arguments, there are no arguments of any kind. If you have a zoo with no animals in it, it's not meaningful to ask whether the animals which are not there are zebras or elephants.
does it prompt me to give it a text file name?Apparently.
when i run it this is all i get:
usage: Translate wordmapfile textfile
Press any key to continue...Right. And "wordmapfile" is almost certainly supposed to be a file that holds the word map, and "textfile" is almost certainly the file of text that it's going to translate. -
How to create and read text file using LabVIEW 7.1 PDA module?
How to create and read text file using LabVIEW 7.1 PDA module? I can not create a text file and read it.
I attach my code here.
Attachments:
File_IO.vi 82 KBWell my acquisition code runs perfect. The problem is reading it. I can't seem to read my data no matter what I do. My data gets saved as a string using the array to string vi but I've read that the string to array vi (which I need to convert back to array to read my data) does not work on the pda. I'm using version 8.0. So I was trying to modify the program posted in this discussion so that it would save data from my DAQ. I did that but I still can't read the data after its saved. I really don't know what else to do. All I need to do is read the data on the pda itself. I can't understand why I'm having such a hard time doing that. I found a possible solution on another discussion that talks about parsing the strings because of the bug in the "string to array" vi. However, that lead me to another problem because for some reason, the array indicators or graphs don't function on the pda. When i build the program to the pda or emulator, the array indicators are faded out on the front panel as if the function is not valid. Does this kind of help give a better picture of what I'm trying to do. Simply read data back. Thanks.
-
How can i read the text files and buffer the data in Vector?
hi. I have been running into this problem for days, but with no luck and losing right direction.
The problem is : I am trying to read a text file and buffer the data into a
Queue for each user.
the sample text file is as below:( 1st column is timestamp, 2nd is user_id, 3rd is packet_id, 4th is packet_seqno, 5th is packet_size)
0 1 1 1 512
1 2 1 2 512
2 3 1 3 512
3 4 1 4 512
4 5 1 5 512
5 6 1 6 512
6 7 1 7 512
7 8 1 8 512
8 9 1 9 512
9 10 1 10 512
10 1 2 11 512
11 2 2 12 512
12 3 2 13 512
13 4 2 14 512
14 5 2 15 512
15 6 2 16 512
16 7 2 17 512
17 8 2 18 512
18 9 2 19 512
19 10 2 20 512
20 1 3 21 512
21 2 3 22 512
22 3 3 23 512
23 4 3 24 512
24 5 3 25 512
25 6 3 26 512
26 7 3 27 512
27 8 3 28 512
28 9 3 29 512
29 10 3 30 512
30 1 4 31 512
31 2 4 32 512
32 3 4 33 512
33 4 4 34 512
34 5 4 35 512
35 6 4 36 512
36 7 4 37 512
37 8 4 38 512
38 9 4 39 512
39 10 4 40 512
40 1 5 41 512
41 2 5 42 512
42 3 5 43 512
43 4 5 44 512
44 5 5 45 512
45 6 5 46 512
46 7 5 47 512
47 8 5 48 512
48 9 5 49 512
49 10 5 50 512
50 1 6 51 512
51 2 6 52 512
52 3 6 53 512
53 4 6 54 512
54 5 6 55 512
55 6 6 56 512
56 7 6 57 512
57 8 6 58 512
58 9 6 59 512
59 10 6 60 512
60 1 7 61 512
61 2 7 62 512
62 3 7 63 512
63 4 7 64 512
64 5 7 65 512
65 6 7 66 512
66 7 7 67 512
67 8 7 68 512
68 9 7 69 512
69 10 7 70 512
70 1 8 71 512
71 2 8 72 512
What I wanna do is to read all the data above and buffer them in a queue for each user( there are only 10 users in total).
I already created a class called Class packet:
public class packet {
private int timestamp;
private int user_id;
private int packet_id;
private int packet_seqno;
private int packet_size;
/** Creates a new instance of packet */
public packet(int timestamp,int user_id, int packet_id,int packet_seqno, int packet_size)
this.timestamp = timestamp;
this.user_id=user_id;
this.packet_id=packet_id;
this.packet_seqno=packet_seqno;
this.packet_size=packet_size;
}then I wanna to create another Class called Class user which I can create a queue for each user (10 users in total) to store type packet information. the queue for each user will be in the order by timestamp.
any idea and sample code will be appreciated.Doesn't sound too hard to me. Your class User (the convention says to capitalize class names) will have an ArrayList or Vector in it to represent the queue, and a method to store a Packet object into the List. An array or ArrayList or Vector will hold the 10 user objects. You will find the right user object from packet.user_id and call the method.
Please try to write some code yourself. You won't learn anything from having someone else write it for you. Look at sample code using ArrayList and Vector, there's plenty out there. Post in the forum again if your code turns out not to behave. -
Read multiple text files and sort them
I am trying to read multiple text files and store the data from the file in vector.
but for days. I am with no luck. anyone can help me out with it? any idea of how to sort them will be appreciated.
Below is part of the code I implemented.
public class packet {
private int timestamp;
private int user_id;
private int packet_id;
private int packet_seqno;
private int packet_size;
public packet(int timestamp0,int user_id0, int packet_id0,int packet_seqno0, int packet_size0)
timestamp = timestamp0;
user_id=user_id0;
packet_id=packet_id0;
packet_seqno=packet_seqno0;
packet_size=packet_size0;
public void setTime(int atimestamp)
this.timestamp=atimestamp;
public void setUserid(int auserid)
this.user_id=auserid;
public void setPacketid(int apacketid)
this.packet_id=apacketid;
public void setPacketseqno(int apacketseqno)
this.packet_seqno=apacketseqno;
public void setPacketsize(int apacketsize)
this.packet_size=apacketsize;
public String toString()
return timestamp+"\t"+user_id+"\t"+packet_id+"\t"+packet_seqno+"\t"+packet_size+"\t";
}Here is the data from part of the text files. ( the first column is timestamp, second is userid, third is packetid.....)
0 1 1 1 512
1 2 1 2 512
2 3 1 3 512
3 4 1 4 512
4 5 1 5 512
5 6 1 6 512
6 7 1 7 512
7 8 1 8 512
8 9 1 9 512
9 10 1 10 512
10 1 2 11 512
11 2 2 12 512
12 3 2 13 512
13 4 2 14 512
14 5 2 15 512
15 6 2 16 512
16 7 2 17 512Here's a standard idiom for object-list-sorting:
/* cnleafdata.txt *********************************************
0 1 1 1 512
1 2 1 2 512
2 3 1 3 512
3 4 1 4 512
4 5 1 5 512
5 6 1 6 512
6 7 1 7 512
7 8 1 8 512
8 9 1 9 512
9 10 1 10 512
10 1 2 11 512
11 2 2 12 512
12 3 2 13 512
13 4 2 14 512
14 5 2 15 512
15 6 2 16 512
16 7 2 17 512
import java.util.*;
import java.io.*;
public class Packet implements Comparable<Packet>{
private int timeStamp;
private int userId;
private int packetId;
private int packetSeqno;
private int packetSize;
public Packet(int timeStamp0, int userId0, int packetId0,
int packetSeqno0, int packetSize0) {
timeStamp = timeStamp0;
userId = userId0;
packetId = packetId0;
packetSeqno = packetSeqno0;
packetSize = packetSize0;
public Packet(String timeStamp0, String userId0, String packetId0,
String packetSeqno0, String packetSize0) {
this(Integer.parseInt(timeStamp0), Integer.parseInt(userId0),
Integer.parseInt(packetId0), Integer.parseInt(packetSeqno0),
Integer.parseInt(packetSize0));
public Packet(String[] a){
this(a[0], a[1], a[2], a[3], a[4]);
public void setTime(int aTimeStamp){
timeStamp = aTimeStamp;
public void setUserId(int aUserId){
userId = aUserId;
public void setPacketId(int aPacketId){
packetId = aPacketId;
public void setPacketSeqno(int aPacketSeqno){
packetSeqno = aPacketSeqno;
public void setPacketSize(int aPacketSize){
packetSize = aPacketSize;
public int getUserId(){
return userId;
public String toString(){
return String.format
("%2d %2d %2d %2d %4d", timeStamp, userId, packetId, packetSeqno, packetSize);
public int compareTo(Packet otherPacket){
return userId - otherPacket.getUserId();
/* main for test */
public static void main(String[] args){
String line;
ArrayList<Packet> alp;
alp = new ArrayList<Packet>();
try{
BufferedReader br = new BufferedReader(new FileReader("cnleafdata.txt"));
while ((line = br.readLine()) != null){
// if (! recordValid(line)){
// continue;
String[] ar = line.split("\\s");
alp.add(new Packet(ar));
catch (Exception e){
e.printStackTrace();
System.out.println("[original]");
for (Packet p : alp){
System.out.println(p);
System.out.println();
Collections.sort(alp);
System.out.println("[sorted by user ID]");
for (Packet p : alp){
System.out.println(p);
}
Maybe you are looking for
-
below i gave some sample of data .i need how to get the resultsrc1 ( is a flat file)col1 col2 col3 1 a b2 c d3 e f it is my first day souce target is same as it comes tgt1 (flat file )
-
Error accessing Name Server Configuration
Hello, I have installed WebAS 6.40 SP11 and deployed JDI SP11 on a single stand-alone machine, which is configured as Landscape and Name Server. When I try to access the Name Server Configuration in the DTR I get a "Server not found" error. The URL i
-
[svn:osmf:] 10516: More unit tests for ListenerProxyElement.
Revision: 10516 Author: [email protected] Date: 2009-09-22 15:13:13 -0700 (Tue, 22 Sep 2009) Log Message: More unit tests for ListenerProxyElement. Required some minor API changes to SwitchableTrait, to enable triggering of the indicesChange e
-
Prints Garbled Text with HP845C
My Garbled Text problem: My MacBook is back from Applecare after it went dead, and the internal hard disk was wiped clean by them as I requested, and in checking completed print jobs, I see that they did try printing a document or two after reformatt
-
Multi Block Form Navigation Problem
Hiii i have got a multi block form the problem that am facing now is that when running the FORM and when am over one of the my canvases then if i tried to press any of : (Tab button,Down arrow ) the canvas changes to ANOTHER Datablock ... i dont know