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 2 of 2

Thread: Display every second world from file.

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Display every second world from file.

    I have a file which contains a sentence "Alina ma pingwina a w basenie delfina". So my program should show Alina/pingwina/w/delfina.
    My code looks like this:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    public class zad679 {
        public static void main(String[] args) {
            try {
                FileReader fileReader = new FileReader("src/zad679/test.txt");
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String content;
     
                List<String> l = new ArrayList<String>();
                while ((content = bufferedReader.readLine()) != null) {
                    l.add(content);
                    for (int i =0; i < l.size(); i += 2) {
                        System.out.println(l.get(i) + "/");
                    }
                }
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
    }
    And the result is:
    Alina ma pingwina a w basenie delfina/

    So the sentence is just like in file, program doesn't display every second world from file like it should. What am I missing, how to fix it? Thank you for your help in advance
    Last edited by javastart; August 16th, 2020 at 08:38 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display every second world from file.

    like it should.
    Where does the program split the line that is read into the separate words you want to see?

    The Scanner class has methods to read the input one word/token at a time.
    The String class has the split method that can be used to separate the words/tokens on each line.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    javastart (August 16th, 2020)

Similar Threads

  1. [SOLVED] Display .txt file in java using eclipse
    By Ronnyf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 17th, 2018, 05:32 AM
  2. How Do I Display A World Into A JFrame?
    By _vertig0 in forum Java Rendering and Gaming
    Replies: 0
    Last Post: December 31st, 2017, 01:51 AM
  3. Does anyone know how to display an image from a file on your screen?
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:06 PM
  4. Trying to display text from file in JTextArea. Please Help.
    By rjdelight in forum Java Theory & Questions
    Replies: 7
    Last Post: June 29th, 2011, 05:10 AM
  5. Display file name
    By Puk284 in forum Java Servlet
    Replies: 1
    Last Post: April 13th, 2010, 03:13 AM

Tags for this Thread