java - How to compare two elements from different arrays in Arraylist? -


i've created 2 arrays arraylist (java), alist1 , alist2. both have mix of doubles , strings. how directly compare individual contents of alist1 corresponding individual contents in alist2 example, if first value or string in alist1 doesn't match first value or string in alist2, there should message saying 2 don't match. should go on every element of each arraylist.

thanks!

edited:

here initial attempt:

if (!alist1.get(0).equals(alist2.get(0))) {       joptionpane.showmessagedialog(null,  "#1 incorrect."); } if (!alist1.get(1).equals(alist2.get(1))) {      joptionpane.showmessagedialog(null, "#2 incorrect."); } 

and on, comparing each element alist1 alist2, , seeing if not equal (whether doubles or strings). corresponding elements , sizes of arrays same. example, if alist1 = {0,1,2,3,4,dog}, alist2 contain {10,2,5,2,cat}.

edit: whole code.

import javax.swing.joptionpane; import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.io.writer; import java.util.arraylist;  public class kevinmath2 {         static file filename = new file("homework.txt");         static arraylist <object> alist = new arraylist <object> ();         static string homework = " ";         static file filename2 = new file("homework2.txt");         static arraylist <object> alist2 = new arraylist <object> ();         static string homework2 = " ";     public static void main(string[] args) {         // todo auto-generated method stub         string initialinput = joptionpane.showinputdialog(null, "would add answers or check answers?");         string again;         char repeat;         {             switch (initialinput) {                 case "add answers":                     char answerfinal1;                     string answerpass = joptionpane.showinputdialog(null, "please enter password");                     while (!answerpass.equals("victor")) {                         joptionpane.showmessagedialog(null,  "incorrect password. please try again.");                         answerpass = joptionpane.showinputdialog(null, "please enter password.");                     }                     {                         {                             string options = joptionpane.showinputdialog(null, "enter date of desired" +                                                                             " answers add (m/d/y)");                             switch (options) {                                 case "05/29/15":                                     while (!homework.isempty()) {                                         homework = joptionpane.showinputdialog("please enter answers, in order. after "                                                                                 + "the last answer, leave next answer"                                                                                  + " blank, , click ok.");                                         if (!homework.isempty()) alist.add(homework);                                     }                                     try {                                         filewriter fw = new filewriter (filename);                                         writer output = new bufferedwriter (fw);                                         int sz = alist.size();                                         (int i=0; < sz; i++) {                                             output.write(alist.get(i).tostring() + "\n");                                         }                                         output.close();                                     } catch (exception e) {                                         joptionpane.showmessagedialog(null, "oops! cannot create file.");                                     }                                     break;                                 default:                                     joptionpane.showmessagedialog(null,  "please enter valid date.");                                     break;                             }                         } while (!homework.isempty());                         string final1 = joptionpane.showinputdialog(null, "is correct: " + alist.get(0) + " "                                                                         + alist.get(1) + " " + alist.get(2) + " " +                                                                          alist.get(3) + " " + alist.get(4) + "? (yes or no)");                         answerfinal1 = final1.charat(0);                     } while (answerfinal1 == 'n' || answerfinal1 == 'n');                      break;                     //need store array permanently                 case "check answers": //need make stores array of kevin's answers permanently                     char answerfinal2;                     string checkpass = joptionpane.showinputdialog(null, "please enter password");                     while (!checkpass.equals("kevin")) {                         joptionpane.showmessagedialog(null,  "incorrect password. please try again.");                         answerpass = joptionpane.showinputdialog(null, "please enter password.");                     }                        {                         {                             string options2 = joptionpane.showinputdialog(null, "enter date of desired" +                                                                             " answers check (m/d/y)");                             switch (options2) {                                 case "05/29/15":                                     while (!homework2.isempty()) {                                         homework2 = joptionpane.showinputdialog("please enter answers, in order. after "                                                                             + "the last answer, leave next answer" +                                                                              " blank, , click ok.");                                         if (!homework2.isempty()) alist2.add(homework2);                                     }                                     try {                                         filewriter fw = new filewriter (filename2);                                         writer output = new bufferedwriter (fw);                                         int sz = alist2.size();                                         (int i=0; < sz; i++) {                                             output.write(alist2.get(i).tostring() + "\n");                                         }                                         output.close();                                     } catch (exception e) {                                         joptionpane.showmessagedialog(null, "oops! cannot create file.");                                     }                                     break;                                 default:                                     joptionpane.showmessagedialog(null,  "please enter valid date.");                             }                         } while (!homework2.isempty());                         string final2 = joptionpane.showinputdialog(null, "is correct: " + alist2.get(0) + " "                                 + alist2.get(1) + " " + alist2.get(2) + " " +                                  alist2.get(3) + " " + alist2.get(4) + "? (yes or no)");                         answerfinal2 = final2.charat(0);                     } while (answerfinal2 == 'n' || answerfinal2 == 'n');                      int = 0; // counter variable                       if (alist.size() == alist2.size()) { // check if both lists equal                         (object obj : alist) { // iterate through list                             if (obj.getclass() == string.class) { // find if it's string                                 if (!alist.get(i).equals(alist2.get(i))) {                                     joptionpane.showmessagedialog(null, "#" + + " wrong.");                                 }                              }                              if (obj.getclass() == double.class) { // or double                                 if (!alist.get(i).equals(alist2.get(i))) {                                     joptionpane.showmessagedialog(null, "#" + + " wrong.");                                 }                             }                              i++;                         }                     }                      break;                 default:                     joptionpane.showmessagedialog(null, "please enter valid option.");                     break;                 }              again = joptionpane.showinputdialog(null, "would check answer, or "+                                                     "add answer? (yes or no)");             repeat = again.charat(0);         } while (repeat == 'y' || repeat == 'y');         joptionpane.showmessagedialog(null,  "thanks using program");      }  } 

try using loop iterate through first arraylist , use counter ('i' in example below) loop compare each of indices loop through using method provided arraylist.

for (int = 0; < alist1.size(); i++) {     if (!alist1.get(i).equals(alist2.get(i)))         //output whatever want } 

edit: changed .equals instead of == suggestion. catch.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -