0

My txt data is :

2,1

cloud,0,100,1000,1000000,10240,10.0,FOG_DEVICE
mobile_0,1,1000,1000,1000,1024,0.0,FOG_DEVICE
mobile_1,1,1000,1000,1000,1024,0.0,FOG_DEVICE

1.0,2.0,3.0  
2.0,3.0,4.0

While running this program, the compare function is not called. Can somebody tell me what I am doing wrong?

package org.fog.examples;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;

import org.fog.entities.FogDevice;
import org.fog.entities.Sensor;

public class Read {
  static List <FogDevice> fogDevices = new ArrayList < FogDevice > ();
  static List <String> Mylist = new ArrayList < String > ();
  static List <Sensor> sensors = new ArrayList < Sensor > ();
  static List <String> fogNode = new ArrayList < >();
  static List <String> linkNode = new ArrayList < >();
  static List <String> moduleNode = new ArrayList < >();
  static List <String> fd = new ArrayList < >();
  static String str,
  str1;
  static String level;
  static String[] ss,
  ss1;

  public static void main(String[] args) {
    File file = new File("/home/madhu/Desktop/data.txt");
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(file));
      String data[] = reader.readLine().split(",");
      String s;
      int nbline = 0,
      i = 0,
      block = 0;
      while ((s = reader.readLine()) != null && block < data.length) {
        if (s.equals("")) {
          block++;
          nbline = Integer.parseInt(data[block - 1]);
          i = 0;
        }
        for (; i < nbline; i++) {
          s = reader.readLine();
          if (s == null) break;
          else if (s.equals("")) {
            block++;
            break;
          }
          switch (block) {
          case 1:
            fogNode.add(s);
            break;
          case 2:
            linkNode.add(s);
            break;
          default:
            moduleNode.add(s);
          }
        }
      }
    } catch(IOException | NumberFormatException ex) {
      System.err.println(ex.getStackTrace());
    } finally {
      closeReader(reader);
    }
    System.out.println("nodes: " + fogNode);
    System.out.println("Links : " + linkNode);

    Iterator < String > nodeString = fogNode.iterator();
    Iterator < String > nodeString1 = linkNode.iterator();
    while (nodeString1.hasNext()) {
      str1 = nodeString1.next();
      System.out.println(str1);
      String[] ss1 = str1.split(",");
    }
    while (nodeString.hasNext()) {
      str = nodeString.next();
      System.out.println(str);
      String[] ss = str.split(",");
      System.out.println(ss[0]);
      compare(ss[0]);
    }
  }

  public static void compare(String level) {
    if (level == "1") {
      int link = setupLatency(Integer.parseInt(ss1[0]));
      System.out.println(link);

    } else if (level == "2") {
      int link = setupLatency(Integer.parseInt(ss1[1]));
      System.out.println(link);
    }
  }

  private static int setupLatency(int i) {
    int Latency = i;
    return Latency;
  }
  
  public static void closeReader(BufferedReader reader) {
    if (reader != null) {
      try {
        reader.close();
      } catch(IOException e) {}
    }
  }
}

I am reading from a text file. For each line, I want to compare the second value in order to set the latency value, which is present in the 2nd set of lines.

F. Müller
  • 3,969
  • 8
  • 38
  • 49
  • Why are you asking us to debug your code? What happened when you used a **debugger** on your own code? – Andreas Nov 05 '20 at 07:41
  • i did dubugging. it is not entering into function definition of compare() . –  Nov 05 '20 at 08:17
  • Did it print any nodes? What did `System.out.println("nodes: " + fogNode);` print? What did `System.out.println(str);` print? What did `System.out.println(ss[0]);` print? If the last one printed something, then `compare` was called. Of course, both `if` statements are false, no matter what was printed, as your debugger would have shown you, if you had stepped through the code. See "[How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)" link up top. – Andreas Nov 05 '20 at 08:36

0 Answers0