Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.net.*;

import java.util.*;

public class Main {
    public static double readDoubleFromStdin(String message) throws Exception {
        String tmp = readStringFromStdin(message);
        return Double.parseDouble(tmp);
    }

    public static String readStringFromStdin(String message) throws Exception {
        System.out.print(message);
        System.out.flush();
        System.in.skip(System.in.available());
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        return reader.readLine().trim();
    }

    public static <T> String print(Collection<T> objs) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (T obj : objs) {
            if (first)
                first = false;
            else
                sb.append(", ");
            sb.append(obj);
        }
        return sb.toString();
    }

    public static <T> String print(Map<T, T> map) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Map.Entry<T, T> entry : map.entrySet()) {
            if (first)
                first = false;
            else
                sb.append(", ");
            sb.append(entry.getKey()).append("=").append(entry.getValue());
        }
        return sb.toString();
    }

    public static String print(List<NetworkInterface> interfaces) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (NetworkInterface intf : interfaces) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(intf.getName());
        }
        return sb.toString();
    }

    /**
     * Reads a line of text.  A line is considered to be terminated by any one
     * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
     * followed immediately by a linefeed.
     * @return A String containing the contents of the line, not including
     * any line-termination characters, or null if the end of the
     * stream has been reached
     * @throws IOException If an I/O error occurs
     */
    public static String readLine(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder(35);
        int ch;

        while (true) {
            ch = in.read();
            if (ch == -1)
                return null;
            if (ch == '\r')
                ;
            else {
                if (ch == '\n')
                    break;
                else {
                    sb.append((char) ch);
                }
            }
        }
        return sb.toString();
    }
}