Introduction to Java: input, output, file operations

I wrote a simple console application to show you how to read from and write to text files in Java. Basically it generates two HTML files (exam results based on a data file) in two formats. There is a version for students (results sorted by student id) and a version for a teacher (sorted by result). For Java programming I recommend IntelliJ IDEA.

A data file looks like this. First name then surname, student system id and number of points achieved. An example data file:

Chandler Bing 987622345 10
Joey Tribbiani 983455234 1
Phoebe Buffay 345678434 21
Rachel Green 345634456 4

We want to store these lines in an array of objects so there is a class with variables needed, getters and setters and some more methods I will describe later. What’s important I use comparators (needed for sorting). I need data sorted by two keys so I use two comparators.

/**
 * Created by Krzysztof Grabowski on 04.03.15.
 */

import java.util.Comparator;


public class Student {
//public class Student implements Comparable<Student> {
// Needed to use:  @Override public int compareTo()

    private String studentname;
    private String studentsurname;
    private int systemid;
    private int points;

    public Student(String studentname, String studentsurname, int systemid, int points) {
        this.studentname = studentname;
        this.studentsurname = studentsurname;
        this.systemid = systemid;
        this.points = points;
    }

    public String getStudentname() {
        return studentname;
    }
    public void setStudentname(String studentname) {
        this.studentname = studentname;
    }
    public String getStudentsurname() {
        return studentsurname;
    }
    public void setStudentsurname(String studentsurname) {
        this.studentsurname = studentsurname;
    }
    public int getSystemid() {
        return systemid;
    }
    public void setSystemid(int systemid) {
        this.systemid = systemid;
    }
    public int getPoints() {
        return points;
    }
    public void setPoints(int points) {
        this.points = points;
    }

    /*@Override
    public int compareTo(Student comparesto) {
        int comparepoints=((Student)comparesto).getPoints();
        *//* For Ascending order*//*
        //return this.points-comparepoints;

        *//* For Descending order do like this *//*
        return comparepoints-this.points;
    }*/


    // Comparator for sorting the list by systemid
    public static Comparator<Student> StuSystemid = new Comparator<Student>() {

        public int compare(Student s1, Student s2) {

            int systemid1 = s1.getSystemid();
            int systemid2 = s2.getSystemid();

	   /*For ascending order*/
            //return systemid1-systemid2;

	   /*For descending order*/
            return systemid2-systemid1;
        }
    };

    // Comparator for sorting the list by points
    public static Comparator<Student> StuPoints = new Comparator<Student>() {

        public int compare(Student s1, Student s2) {

            int points1 = s1.getPoints();
            int points2 = s2.getPoints();

	   /*For ascending order*/
            //return points1-points2;

	   /*For descending order*/
            return points2-points1;
        }
    };


    @Override
    public String toString() {
        return studentname + " " + studentsurname + " " + systemid + " " + points;
    }

}

As you can see there is a few lines commented. If you need only one way of sorting you can uncomment them and comment those two comparators instead and use the following code somewhere in your StudentApp to do the sorting.

Continue reading Introduction to Java: input, output, file operations

Introduction: Installing Java (JDK) and Android Studio on Arch Linux

Welcome, I’m Krzysztof, I live in Kraków (Poland) and a few weeks ago I decided to learn Java and Android. My programming experience is limited to C++ but it’s never too late to start. I’m gonna document problems I managed to resolve and show you some examples, especially code snippets appropriate for beginners. My main goal is to learn Android development, I’m also interested in Linux and Java. The operating system of my choice is Arch Linux.

At the links section you can find my Github account where I’m currently working on Facebook Lite, Web App for Android. I learnt a lot creating the app so it’s a great place to show you how it’s made and share my experience. But what is actually needed to start building Android apps? I personally recommend Android Studio by Google. It’s a great app for developers and the only recommended way of creating Android apps for now. But before you start you need Java Development Kit. I’m working on Arch Linux so now I’m gonna show you how to install Oracle Java (JDK) which is not present in Arch Linux (the system uses OpenJDK by default) and how to install Android Studio.

yaourt -S jdk
sudo archlinux-java set java-8-jdk
yaourt -S android-studio

Now you are ready to go so it’s a perfect time to create your first Android app. Just create a new project with blank activity and test it on your device or start a new emulator. I’m not gonna write about the basic usage of Android Studio. It’s rather intuitive and well documented software. Instead of that I’ll share some links good to begin your programming journey.

  1. developer.android.com/training/index.html – you should begin here
  2. stackoverflow.com – it’s where you look for all the answers
  3. romannurik.github.io/AndroidAssetStudio/icons-launcher.html – Android Asset Studio: Launcher Icon Generator

Next time I will show you how to create an advanced WebView.