problem: read a text file line by line and spilt words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • No Signal X
    New Member
    • Dec 2009
    • 4

    problem: read a text file line by line and spilt words

    hello

    i'm having a problem in reading a text file line by line with splitting words, counting the number of lines & words and saving the position of each word


    i've started with this :
    Code:
    public void readlineandword() throws IOException{
    		BufferedReader rd = new BufferedReader(new FileReader("t1.txt"));
    
    		String line = rd.readLine();
    		Scanner read =	new Scanner(line);
    		String word1 = read.next();
    		System.out.println(word1);
    		String word2 = read.next();
    		System.out.println(word2);
    	}
    anyone could help please ? =(
  • miss java
    New Member
    • Dec 2009
    • 1

    #2
    anyone could help please ? =(
    i have the same problem !

    Comment

    • coder44
      New Member
      • Dec 2009
      • 6

      #3
      you can also use like that;
      Code:
      String line = rd.readLine();
      char [] words = line.split(" "); 
      system.out.println(words[0]);
      system.out.println(words[1]);
      Last edited by Frinavale; Dec 8 '09, 04:34 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags

      Comment

      • No Signal X
        New Member
        • Dec 2009
        • 4

        #4
        thanx

        what i'm missing are lines & words counter

        cuz I have to create an array with a list inside each element of the array

        and every list contains words that have the same number of Characters

        for example : and , eat , cat .. are inside a list in index 0

        Comment

        • coder44
          New Member
          • Dec 2009
          • 6

          #5
          if you use split function you should also use parameters for parsing. In your example and, eat, cat words seperated with space and comma. You must give this arguments to split function. For example;

          char [] delims = {"," , " "};
          line.split(deli ms);

          Comment

          • No Signal X
            New Member
            • Dec 2009
            • 4

            #6
            thanks a lot coder44 you've helped me a lot

            still wondering about the counters cuz I have to save the number of line & position of each word

            i.e.:
            coder44 have helped me
            he is very helpful guy

            the word "very" is in 2nd line , 3rd word

            I just need the counters >.<"

            Comment

            • coder44
              New Member
              • Dec 2009
              • 6

              #7
              Code:
              import java.io.BufferedReader;
              import java.io.FileReader;
              import java.io.IOException;
              import java.util.ArrayList;
              
              
              // main class
              public class Main {
              
              	// for saving word objects
              	private ArrayList<Word> words;
              		
              	public static void main(String[] args) throws IOException {
              		
              		Main main = new Main();
              		main.start();//start
              		
              	}
              	
              	private void start() throws IOException {
              		
              		readFile(); // read file
              		printWords(); // print results
              	}
              
              	private void readFile()throws IOException{
              		
              		BufferedReader input = new BufferedReader(new FileReader("tl.txt"));
              		words = new ArrayList<Word>();
              		
              		int lineNum = 1; // we read first line in start
              		
              		// delimeters of line in this example only "space"
              		char [] parse = {' '};
              		String delims = new String(parse);
              		
              		String line = input.readLine();
              		 
              		// read line while end of file
              		while(line != null){
              			
              			String [] lineWords = line.split(delims);
              			
              			// split the words and create word object
              			for (int i = 0; i < lineWords.length; i++) {
              				Word w = new Word(lineNum,i+1,lineWords[i]); // lineNum -> line number i+1--> words index in line
              				words.add(w);	                             // lineWords[i] is word  
              			}
              			lineNum++;	// pass the next line
              			
              			line = input.readLine();
              			
              		}
              		
              	}
              	
              	// print out the results
              	private void printWords() {
              		
              		System.out.println("WORD\t\tLINE\t\tINDEX");
              		System.out.println();
              		
              		for (int i = 0; i < words.size(); i++) {
              			Word w = words.get(i);
              			System.out.println(w.name + "\t\t" + w.lineNum + "\t\t" + w.index);
              		}
              		
              	}
              
              	// class word for model of every word object
              
              	class Word{
              		private int lineNum;
              		private int index;
              		private String name;
              		
              		public Word(int lineNum, int index, String name) {
              			this.lineNum = lineNum;
              			this.index = index;
              			this.name = name;
              		}
              		
              		public int getLineNum() {
              			return lineNum;
              		}
              		public int getIndex() {
              			return index;
              		}
              		public String getName() {
              			return name;
              		}	
              	}	
              }
              Last edited by Frinavale; Dec 8 '09, 04:35 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags

              Comment

              • coder44
                New Member
                • Dec 2009
                • 6

                #8
                This code help u but my dear if you are new in java maybe you don't understand some sections, please ask me I try to explain you. Also you study hard and write more code examples about java...

                Good work.

                Comment

                • No Signal X
                  New Member
                  • Dec 2009
                  • 4

                  #9
                  Coder44 u're awesome don't know how to thank u I was in trouble cuz i'm not good with files

                  thanx a LOT man

                  Comment

                  Working...