0

I'd like to replace Int[] mydata and to use the array argv and to call my class Graph with javafx from another class with javax, from there I declare my array of strings and call my java program. - Now I need to call my class from another Program with string like mydata. and - To use the array argv and to build my new array.

Is that possible? and how? Thank you for your Help.

public class Graph extends Application {

public void start(Stage stage) {

    int[] mydata = {
            12, 9, 0, 1, 38, 19, 21, 72, 33, 83, 14, 10, 65, 46, 10, 17, 27, 38, 65, 98, 8, 58, 38, 79, 37, 69, 26, 15};
    Pane root = new Pane();
    NumberAxis xAxis = new NumberAxis();
    NumberAxis yAxis = new NumberAxis();


    ScatterChart scatterChart=new ScatterChart(xAxis,yAxis);
    scatterChart.setTitle("Chart");
    XYChart.Series data=new XYChart.Series();
    data.setName("Data");
    for (int i=0; i< mydata.length; i++) {
        data.getData().add(new XYChart.Data(i,mydata[i]));
        }

    scatterChart.getData().addAll(data);
    root.getChildren().add(scatterChart);

    Scene scene = new Scene(root, 1000, 600);

    //scene.getStylesheets().add("style.css");
    //scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
    scatterChart.setPrefSize(1000, 600);
    stage.setTitle("Graph");
    stage.setScene(scene);

    stage.show();

    File file = new File("c:\\Images\\Image.png");

    WritableImage snapshot = scene.snapshot(null);
    BufferedImage bimg = SwingFXUtils.fromFXImage(snapshot, null); 
    try {
        ImageIO.write(bimg, "png", file);
    } catch (Exception s) {
        s.printStackTrace();
    }

}

public static void main(String[] argv) {
    launch(argv);
}

}
6
  • argv is String[] and you want to move it to an int[] ? Integer.parseInt() maybe? Commented Apr 19, 2016 at 14:22
  • Thank you, i've used Integer.parsInt() but I became mistakes public static void main(String[] argv) { int[] list = new int [argv.length]; for(int i = 0; i < argv.length; i++) { list[i] = Integer.parseInt(argv[0]); } //System.out.println(liste[i]); launch(list); and I Think that my new list is unknown in the method start(Stage stage) Commented Apr 19, 2016 at 14:32
  • plus beware command line limitation from your OS, if you want to pass a lot of data you may overflow max command line length. You'd better use a temp file if you have > 100 values Commented Apr 19, 2016 at 14:35
  • OK, I think, I need to use a temp file because sometimes I have more than 100 values. Commented Apr 19, 2016 at 14:43
  • @Lolitta your list injection into the method start seems to be not working. Can you share your code of method launch Commented Apr 19, 2016 at 14:44

1 Answer 1

1

If you want to have access to the arguments given to your main method in your start method in order to convert it into integers, you can call the method getParameters().getUnnamed(). More details here.

Then to convert your List of String into a List of Integer, you can do something like this:

With Java 8:

List<Integer> mydata = arguments.stream().map(Integer::parseInt).collect(Collectors.toList());

With Java 7:

List<Integer> mydata = new ArrayList<>(arguments.size());
for (String argument : arguments) {
    mydata.add(Integer.parseInt(argument));
}
Sign up to request clarification or add additional context in comments.

14 Comments

Thank you Nicolas, with the method getParameters().getUnnamed() I have access to the arguments but I become still mistakes from List<Integer> mydata = arguments.stream().map(Integer::parseInt).collect(Collectors.toList());
What are the arguments that you provide to your application?
Do we agree that the code above assumes that arguments is the value of getParameters().getUnnamed(), right?
I've set some values as Arguments in the debug configurations and I see my values as Variables under Parameters --> unnamedParams [ 12, 9, 0, 1, 38, 19, 21, 72, 33, 83, 14, 10, 65, 46, 10, 17, 27, 38, 65, 98, 8, 58, 38, 79, 37, 69, 26, 15 ] and now I need to convert List of String into a List of Integer.
so if you call getParameters().getUnnamed().stream().map(Integer::parseInt).collect(Collectors.toList()) as mentioned above, it fails?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.