I'm currently creating a Java program in swing for my final year project simliar to something you'd find on codeacademy etc.
i'm trying to compare the user input for example "System.outprintln(helloworld");" with a string of the stored answer for the question in this case the answer would be "System.outprintln(helloworld");" i've got the method below which should compare the user input which is in a jtextarea to the answer which is another string. however it doesn't seem to work unless its comparing the last line of the input and i can't figure out why. Has anyone got any ideas?

i've put a println so I can see which comparisons are being made which seems fine but it doesn't enter the next if statement unless the last string is the same as the answer.

String[] lines = inputPane.getText().split("\\n");
                    chapter.get(chapterSelect).getLevel().get(levelSelect).getTask().getAnswer().split("\\n");

                         for(int i = 0 ; i< lines.length; i++){
                             System.out.println(chapter.get(chapterSelect).getLevel().get(levelSelect).getTask().getAnswer() +"compare 2 "+ lines[i]);
                             if(lines[i].equals(chapter.get(chapterSelect).getLevel().get(levelSelect).getTask().getAnswer())){
                                 CardLayout cl = (CardLayout)mainJPanel.getLayout();
                                 JOptionPane.showMessageDialog(null,"CORRECT");
                                 }
                                 }

ok fixed this with trim() method you can delete this thread.

No need to delete - this solution may be useful to someone else. I've marked it "solved"
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.