Search code examples
javaiobufferedreader

Java compare the data from method to hashmap in another method


This code is reading the textfile and creating HashMap. What I want to do is to compare the StatusMAP method in my ModelOne.txt and ModelTwo.txt

Let's say my ModelOne.txt has data like this:

Model1, 54321, Test, Online

And on my ModelTwo.txt has data like this:

Model2, 12345, Check, OFF

I already know how to get the indexes data in textfile, but what I can't do is to compare those two data from two on each other with base on StatusMAP. So basically if the ModelOne.txt compare to ModelTwo.txt it will get the index[3] then it will compare if the index[3] is Online in ModelOne it should be OL in ModelTwo if it's not, it will going to print 'Not equal' otherwise 'They're Equal'.

This is really quite long to read, but any help, suggestions or comments would be appreciated. Thanks in advance guys.

import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.io.*;

    public class ReadTxt {

      public static void main(String[] args) throws Exception{

        ReadTxt obj = new ReadTxt();
        obj.PackageMAP();
        obj.StatusMAP();
        obj.ReadModelOne();
        obj.ReadModelTwo();

      }

      public static void StatusMAP() {
          Map<String, String> hashstatus = new HashMap<String, String>(); 
          hashstatus.put("Offline", "OFF");
          hashstatus.put("Online", "OL");
       }


      public static void PackageMAP() {

        String csvFile = "Names.txt";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            Map<String, String> maps = new HashMap<String, String>();

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] packagecode = line.split(cvsSplitBy);

                maps.put(packagecode[0], packagecode[1]);

            }

            //loop map
            for (Map.Entry<String, String> entry : maps.entrySet()) {

                System.out.println("Name Package one \t" + entry.getKey()
                + "\t"
                + "|"
                + "\t"
                + "Name Package two \t"
                + entry.getValue());


            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("This is all the Name packages");
      }


       public static void ReadModelOne() throws Exception{
        BufferedReader br = new BufferedReader(new FileReader("ModelOne.txt"));
        String line = null;

        while ((line = br.readLine()) != null) {
          String[] MOne_STATUS = line.split(",");
          for (String inputOne : MOne_STATUS) {
          inputOne = MOne_STATUS[2];

          }
        }
        br.close(); 
      }

      public static void ReadModelTwo() throws Exception{
        BufferedReader br = new BufferedReader(new FileReader("ModelTwo.txt"));
        String line = null;
        while ((line = br.readLine()) != null) {
            String[] MTwo_STATUS = line.split(",");
            for (String inputTwo : MTwo_STATUS) {
                inputTwo = MTwo_STATUS[2];

            }   
        }
        br.close(); 
    }


        public static void checkStatus() {

        // do the checking here for status

    }


    }

Solution

  • Try as follows,

    String data1 = "Model1, 54321, Test, Online";
    String data2 = "Model2, 12345, Check, OFF";
    
    String output = (data1.split(", ")[3].equalsIgnoreCase("Online") && 
                     data2.split(", ")[3].equalsIgnoreCase("OL")) ? 
                     "Equal" :
                     "Not Equal";
    
    System.out.println(output);