user3100858
user3100858

Reputation: 11

JTextArea ArrayList string data and JTextField input data not matching

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!");
            }

        }
    }
}

Upvotes: 0

Views: 1320

Answers (3)

user3444989
user3444989

Reputation:

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.

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

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!");
         }          
    }
}       
}  

Upvotes: 0

user3444989
user3444989

Reputation:

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.

Upvotes: 0

Related Questions