1

I am using a screen scraping tool to pull data and output into a CSV. It works perfectly unless a session variable does not exist. I want to change my code to say, if this variable does not exist, replace with value 0.

E.G. If Session.getVariable "Figure2" is not present use value 0.

session.log( "Writing data to a file." ); 

out = new FileWriter( "C:/Users/Documents/Live/data.CSV" );
out.write ("User, Figure 1,Figure 2, Figure 3, Figure 4 ");
out.write ("\n");
out.write ( "User" ) ;
out.write (",");
out.write ( session.getVariable( "Figure1" ) );
out.write (",");
out.write ( session.getVariable( "Figure2" ) );
out.write (",");
out.write ( session.getVariable( "Figure3" ) );
out.write (",");
out.write ( session.getVariable( "Figure4" ) );
out.close();

session.log( "Writing data to a file." ); 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
James Harding
  • 69
  • 1
  • 8

3 Answers3

1

You can wrap the variable extraction with a simple method that contains this logic:

private static String variableOrZero(String variable) {
    String retVal = session.getVaraible(variable);
    if (retVal == null) {
        retVal = "0";
    }
    return retVal;
}

And then use it in your code:

out = new FileWriter( "C:/Users/Documents/Live/data.CSV" );
out.write ("User, Figure 1,Figure 2, Figure 3, Figure 4 ");
out.write ("\n");
out.write ( "User" ) ;
out.write (",");
out.write ( variableOrZero( "Figure1" ) );
out.write (",");
out.write ( variableOrZero( "Figure2" ) );
out.write (",");
out.write ( variableOrZero( "Figure3" ) );
out.write (",");
out.write ( variableOrZero( "Figure4" ) );
out.close();
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • If Java 8 or later is used, it can be also done with ``java.util.Optional`` for example like this ``optional.ofNullable(session.getVariable(variable)).orElse("0");`` – Kamil Dec 01 '18 at 13:55
0

My solution would be this using the ternary operator Another stackoverflow link for ternary operator discussion

session.log( "Writing data to a file." ); 

out = new FileWriter( "C:/Users/Documents/Live/data.CSV" );
out.write ("User, Figure 1,Figure 2, Figure 3, Figure 4 ");
out.write ("\n");
out.write ( "User" ) ;
out.write (",");
out.write ( session.getVariable( "Figure1" )==null ? 0 :session.getVariable( "Figure1" ));
out.write (",");
out.write ( session.getVariable( "Figure2" )==null?0:session.getVariable("Figure2") );
out.write (",");
out.write ( session.getVariable( "Figure3" ) );
out.write (",");
out.write ( session.getVariable( "Figure4" ) );
out.close();

session.log( "Writing data to a file." ); 
Bishal Gautam
  • 380
  • 3
  • 16
0

I don't know your session type.So I guess session.getVariable() maybe return null. If you run this code:

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        Integer integer = null;
        FileWriter out = new FileWriter( "data.CSV" );
        out.write(integer);
    }
}

The program will throw a NullPointerException.

Java8 Optional class is a container can save null value.We can use it to check if an Integer object is null. Another bug is write method is write an byte to the Stream.You need PrintWriter and print method to write an int to file directly.

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Optional;

public class Main {
    public static void main(String[] args) throws IOException {
        Integer a = null;
        Integer b = 1;
        FileWriter out = new FileWriter( "data.CSV" );
        PrintWriter printWriter = new PrintWriter(out);
        Optional<Integer> oa = Optional.ofNullable(a);
        Optional<Integer> ob = Optional.ofNullable(b);
        printWriter.print(oa.orElse(0));
        printWriter.print(ob.orElse(0));
        out.close();
        printWriter.close();
    }
}
Yao Yuan
  • 350
  • 3
  • 11