0

so I am trying to have my program read a list of lines from a txt file. This is then displayed in a JTextArea. The user can input data using the JTextField and the goal is to display "Hooray" if the user matches the text in the JArea and "Wrong!" if they do not. Any help is appreciated.

public class TextArea1 {

    JTextArea text;
    JFrame frame;
    JTextField textField;
    public int k;
    public ArrayList aList;
    public String correctAnswer;

    public static void main(String[] args) {
        TextArea1 gui = new TextArea1();

        gui.go();
    }

    private String textLine;

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        textField = new JTextField("");
        textField.addActionListener(new startTextFieldListener("correct answer"));
        JButton startButton = new JButton("Start!");
        startButton.addActionListener(new startButtonListener(aList));


        text = new JTextArea(30, 60);
        text.setLineWrap(true);

        JScrollPane scroller = new JScrollPane(text);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        panel.add(scroller);

        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.WEST, startButton);
        frame.getContentPane().add(BorderLayout.SOUTH, textField);
        frame.setSize(350, 300);
        frame.setVisible(true);
    }


    class startButtonListener implements ActionListener {
        ArrayList aList;

        startButtonListener(ArrayList passedInList) {
            aList = passedInList;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            String fileName = "test.txt";
            String line;
            ArrayList aList = new ArrayList();

            try {
                try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {
                    if (!input.ready()) {
                        throw new IOException();

                    }

                    while ((line = input.readLine()) != null) {
                        aList.add(line);
                    }
                }
            } catch (IOException e) {
                System.out.println(e);

            }

            int sz = aList.size();

            for (int k = 0; k < sz; k++) {

                String correctAnswer = aList.get(k).toString();

                text.append(aList.get(k).toString());
                text.append("\n");
            }
        }
    }

    class startTextFieldListener implements ActionListener {
        String correctAnswer;

        startTextFieldListener(String answer) {
            correctAnswer = answer;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            if (text.getText().equals(correctAnswer)) {
                JOptionPane.showMessageDialog(null, "Hooray!");
            } else {
                JOptionPane.showMessageDialog(null, "Wrong!");
            }

        }
    }
}
10
  • So where is the problem? Commented Apr 25, 2014 at 5:21
  • Right now, even if I match the text that is displayed on the JArea it does not show the correct messageDIalog Commented Apr 25, 2014 at 5:22
  • Do you run the action that checks if they match? Commented Apr 25, 2014 at 5:23
  • @JakeChasan I thought that it would make sense to show the data from the array in the TextArea and the TextField was used to get input directly from the JFrame without JOptionPane Commented Apr 25, 2014 at 5:27
  • @DonyorM I'm not exactly sure what you mean. If it checks for it not matching, doesn't that mean that it also checks if it does match? Commented Apr 25, 2014 at 5:28

3 Answers 3

0

Ok Try this one:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class TextArea1{
    JTextArea text;
    JFrame frame;
    JTextField textField;
    public int k;
    public ArrayList aList;
    public String correctAnswer;

    public static void main (String [] args) {
        TextArea1 gui = new TextArea1();

        gui.go();
    }
    private String textLine;

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        textField = new JTextField("");
        JButton startButton  = new JButton ("Start!");
        startButton.addActionListener(new startButtonListener());


        text = new JTextArea (30, 60);
        text.setLineWrap(true);

        JScrollPane scroller = new JScrollPane(text);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        panel.add(scroller);

        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.WEST, startButton);
        frame.getContentPane().add(BorderLayout.SOUTH, textField);      
        frame.setSize(350, 300);
        frame.setVisible(true);
    }       

    class startButtonListener implements ActionListener {
     ArrayList aList;
        public void actionPerformed(ActionEvent event) {
            String fileName = "test.txt";
            String line;
            ArrayList <String>aList = new ArrayList<>();

            try {
                 try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
                     if (!input.ready())   {
                         throw new IOException();

                     }

                     while ((line = input.readLine()) !=null) {
                         aList.add(line);
                     }
                }
            } catch (IOException e) {
                System.out.println(e);

            }
            int sz = aList.size();
            boolean result=false;
            for(String t:aList){ 
            if (t.equalsIgnoreCase(textField.getText())) {
                    JOptionPane.showMessageDialog(null, "Hooray! Loading File contents....");
                    int count=0;
                    for (int k = 0; k< sz; k++) {          
                        text.append(aList.get(k).toString());
                        System.out.println(count);
                        count++;
                        // if(k<sz-1)
                        //  text.append(", ");
                        text.append("\n");
                    }
                    result=true;
                    break;
                 }

                 else {
                    result=false;
                 }
            }
            if(!result){
                JOptionPane.showMessageDialog(null, "Wrong!");
            }
        }
    }       
}

In this it will look for you text enter in textfield and if it matches it will add entire file content line by line. i have already tested. But remember i am not having sufficient time so ,i have not done regex pattern matching it is simple equal comparison with one line in you text file with text entered in textbox exactly.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, I have just modified your code so that you will get proper message when value in contain in list. look and modify accordingly.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class TextArea1{

JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public String correctAnswer;

public static void main (String [] args) {
    TextArea1 gui = new TextArea1();

    gui.go();
}
private String textLine;

public void go() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    textField = new JTextField("");
    //textField.addActionListener(new startTextFieldListener("correct answer"));
    JButton startButton  = new JButton ("Start!");
    startButton.addActionListener(new startButtonListener());


    text = new JTextArea (30, 60);
    text.setLineWrap(true);

    JScrollPane scroller = new JScrollPane(text);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    panel.add(scroller);

    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.getContentPane().add(BorderLayout.WEST, startButton);
    frame.getContentPane().add(BorderLayout.SOUTH, textField);      
    frame.setSize(350, 300);
    frame.setVisible(true);
}       



class startButtonListener implements ActionListener {
 ArrayList aList;
 startButtonListener()
 {

 }

public void actionPerformed(ActionEvent event) {
     String fileName = "test.txt";
    String line;
    ArrayList aList = new ArrayList();

    try {
         try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
             if (!input.ready())   {
                 throw new IOException();

             }

             while ((line = input.readLine()) !=null) {
                 aList.add(line);
             }
             System.out.println("List ready to check. values in list are :"+aList);
         }
    } catch (IOException e) {
        System.out.println(e);

    }
        if (aList.contains(text.getText())) {
            JOptionPane.showMessageDialog(null, "Hooray!");
         }

         else {
            JOptionPane.showMessageDialog(null, "Wrong!");
         }          
    }
}       
}     

if this is correct then mark it as correct.

7 Comments

Thank you for the input. I tried running your code but whenever I pressed the start button it would display "hooray" instead of loading the txt file data into the JTextArea.
actually i have leave that code for you. if you want i can do for you. but i'll suggest you to do yourself. so you will came to know how it is working.
I would be very grateful if you could do it for me. This is for a school project due in today but I will try my best to understand why it worked.
Ok Tell me you want to load onlu that word which match or entire file whenever start button click.
I would like it to do it line by line if possible
|
0

Minor mistake you added a new line each time you append to the textArea.. that why its not comparing due to the extra new line

if you have new line in you file no problem the arrayList has it already when you parse the file so no need to add new line.

solution:

public class TextArea1{

JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public String correctAnswer;

public static void main (String [] args) {
    TextArea1 gui = new TextArea1();

    gui.go();
}
private String textLine;

public void go() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    textField = new JTextField("");
    //textField.addActionListener(new startTextFieldListener("correct answer"));
    JButton startButton  = new JButton ("Start!");
    startButton.addActionListener(new startButtonListener());


    text = new JTextArea (30, 60);
    text.setLineWrap(true);

    JScrollPane scroller = new JScrollPane(text);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    panel.add(scroller);

    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.getContentPane().add(BorderLayout.WEST, startButton);
    frame.getContentPane().add(BorderLayout.SOUTH, textField);      
    frame.setSize(350, 300);
    frame.setVisible(true);
}       



class startButtonListener implements ActionListener {
 ArrayList aList;
 startButtonListener()
 {

 }

public void actionPerformed(ActionEvent event) {
     String fileName = "test.txt";
    String line;
    String string = "";

    try {
         try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
             if (!input.ready())   {
                 throw new IOException();

             }

             while ((line = input.readLine()) !=null) {
                 string += line;
                 //aList.add(line);
             }
             System.out.println("List ready to check. values in list are :"+aList);
         }
    } catch (IOException e) {
        System.out.println(e);

    }
        if (string.equals(textField.getText())) {

            JOptionPane.showMessageDialog(null, "Hooray!");
         }

         else {
            JOptionPane.showMessageDialog(null, "Wrong!");
         }          
    }
}       
}  

6 Comments

I did already try that but it did not work. Ty for the input regardless.
@user3100858 you want to compare the file and textfield right may i know what is inside the file so I can test it
yep, that is right. This is what is inside the txt file: So what does your brain do with all the routine, ordinary, normal things you encounter?
you want to just compare a word right if that word exist in that string or you want to whole string compared?
I wish to check if the whole string matches
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.