Pages

Thursday, June 14, 2012

Wednesday, June 6, 2012

JavaFx - CHART example

We would like to have a chart of the imported fruits. 
Let' see in JavaFx the PieChart diagram:

public class JavaFXApplication1 extends Application {

    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
       
      primaryStage.setTitle("Chart example");
      ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");       
       
        StackPane root = new StackPane();
        root.getChildren().add(chart);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
               
    }
}

Java - read from console, dos prompt

The JAVA file is the following, for an example of reading input values from console:

package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {
       
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       
        String prompt = "";       
        System.out.println("Enter your name: ");
        prompt = reader.readLine();
        if (prompt.equals("\t"))
            System.out.println("You entered a TAB from your keyboard.");
        else
            System.out.println("Your name is: "+prompt);
       
    }
}

Friday, June 1, 2012

Java - sorting strings with Hungarian characters

        Let's sort some Hungarian strings in alphabetical order:

        String[] words = {"Bruce","Géza","Ákos"};

        Collator c = Collator.getInstance(new Locale("hu"));      
        Arrays.sort(words, c);


        for(int m=0; m<words.length; m++)
            System.out.print(words[m]+", ");