Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Reading a text file word by word

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading a text file word by word

    Hi all, new to Java programming. Got into it, cause a friend also has been doing it, but he's been doing it for a while, so is really good. Anyways.

    So what I want to do it, read a specific .txt file, then go through that, and split it into words. If that makes sense.

    What I want the program to do, is, go through a file, and identify and count how many palindromes are inside the file. So I have been able to do the palindrome part of it. But I am just confused as to how you go about reading the text, word for word, then testing it out.

    And help will be appreciated, and not in a rush, this is just a hobby, that I am really enjoying


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading a text file word by word

    Here is the first hit after googling "java file io": Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    But this sounds like a job for the Scanner class. The API is your new best friend: Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading a text file word by word

    OK, hi again. I ended up figuring out how to do it. But I have a new problem. When I run the program, I want it to only find single word palindromes. At the moment, when I do it, it is also getting multiple word palindromes. So, in a text file I have, it has, for example "avid diva". It is coming up that each of these are palindromes. And I want it too be only one word palindromes, for example "otto". Any help again is appreciated. Here is what I currently have

    import java.io.*;
    public class PalindromDetector {
    	public static void main(String[] args) {
     
    		try { 
     
    			FileInputStream fstream = new FileInputStream("C:/Test1.txt");
     
    			DataInputStream in = new DataInputStream(fstream);
    			BufferedReader br = new BufferedReader(new InputStreamReader(in));
    			String strLine = null;
    			while ((strLine = br.readLine()) != null)   {
     
     
     
    				String reverse = new
    				StringBuffer(strLine).reverse().
    				toString();
    				int i,j,counter=0;
     
    				String m[]=strLine.split(" ");
    				String[] word=reverse.split(" ");
     
    				System.out.println("The palindrome words are:");
    				for(i=0;i<m.length;i++) {
    					for(j=word.length-1;j>=0;j--) {
    						if(m[i].equalsIgnoreCase(word[j])) {
    							System.out.println(m[i]);
    							counter++;
    							break;
    						}
     
    					}
    				}
    				System.out.println("Number of palindromes:"+counter);
    			}
    		}
    		catch(IOException e){}
     
    	}
    }

    Dunno what I have done. Obviously something silly. I'm sure what I have done is supposed to be harder that what I am supposed to have done

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading a text file word by word

    Instead of taking the file one line at a time, why don't you take it one word at a time, since that's what you really care about?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. MS word file in Oracle
    By tandonpiyush in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 07:41 AM
  2. Replies: 2
    Last Post: June 27th, 2011, 11:14 AM
  3. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  4. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  5. Reading the size (height and width) of word documents
    By pradeep_das in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 7th, 2010, 09:58 AM

Tags for this Thread