Friday, January 4, 2019

First Java Program!

Hello!
public class hello {

        public static void main(String[] args) {
                System.out.println("Hello World!");
        }

}
Data types
import java.awt.*;
import java.util.Scanner;

public class hello {

        public static void main(String[] args) {
                System.out.println("hello");

                int i;
                double d;
                String s = "my string";
                boolean b;
                char c = s.charAt(0);
                String us = s.toUpperCase();
                Color mycolor = Color.RED;

                Scanner sc = new Scanner (System.in);
                System.out.print ("Enter anything: ");
                String ui = sc.next();
                System.out.print ("Enter a number: ");
                int userNumber = sc.nextInt();
                System.out.print ("Enter a floating number: ");
                double userFP = sc.nextDouble();

                System.out.println (s);
                System.out.println (us);
                System.out.println (ui);
                System.out.println (ui.contains ("xxx"));

        }
}
comments
// this is comments
/* this is comments for multiple lines
*/
Java IO
import java.io.*;
import java.util.*;
public class io {
        public static void main(String[] args) throws IOException {
                BufferedReader br = new BufferedReader(new FileReader("my.in"));
                PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("my.out")));

                // read in data from file
                int n = Integer.parseInt(br.readLine());
                int[] start = new int[n];
                int[] end = new int[n];
                for(int i = 0; i < n; i++) {
                        StringTokenizer st = new StringTokenizer(br.readLine());
                        start[i] = Integer.parseInt(st.nextToken());
                        end[i] = Integer.parseInt(st.nextToken());
                        System.out.println (start[i] + " / " + end[i]);
                }

        }
}

# my.in
# 2
# 1 2
# 3 4
import static
import static java.lang.System.*;

public class thisclass {
        public static void main(String[] args) {
                out.print( "print this" );
                // only possible with private method below
                println( " ... more xxx" ); 
        }
        private static void println(String str) {
                System.out.println(str);
        }
}

No comments:

Post a Comment