Java Keyboard

java

How can we read input from the user?

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n];
        for(int arr_i=0; arr_i < n; arr_i++){
            arr[arr_i] = in.nextInt();
        }
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        System.out.println(sum)
    }
}

How to read a string of text from the user / keyboard?

import java.utils.Scanners;
public static void main(String[] args) {
  Scanner keys = new Scanner(System.in);
  System.out.println("Enter some text:");
  String text = keys.nextLine();
  System.out.println(text);
  keys.close()
}

How to get a double from the user / keyboard:

Scanner keyboard = new Scanner(System.in);
fnum = keyboard.nextDouble();

Another example of working with files and getting user input from keyboard:

import java.io.*;

public class StressTest {
    private static String destinationFolder = "C:\\bea\\user_projects\\domains\\quantrosDev\\applications\\myquantros.ear\\quantros.war\\";
    private static String filename = "";
    public static void main(String[] args) throws Exception {
        InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader bufRead = new BufferedReader(istream) ;
        System.out.print("Please enter the starting number:");
        int start = Integer.parseInt(bufRead.readLine());
        System.out.print("Please enter the ending number:");
        int stop = Integer.parseInt(bufRead.readLine());
        for (int i=start; i < stop; i++) {
            filename = destinationFolder + "khaitest" + i;
            File fd = new File(filename);
            fd.createNewFile();
            FileWriter fstream = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("It is a long established fact that a reader will be distracted by the readable content 
            of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less 
            normal distribution of letters, as opposed to using 'Content here, content here', making it look like 
            readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum 
            as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in 
            their infancy. Various versions have evolved over the years, sometimes by accident, sometimes 
            on purpose (injected humour and the like).");
            out.close();
        }
        System.out.println("Please see that you can start your weblogic and load your page");
    }
}
import java.io.*;

public class DeleteTestFiles {
    private static String destinationFolder = "C:\\bea\\user_projects\\domains\\quantrosDev\\applications\\myquantros.ear\\quantros.war\\";
    private static String filename = "";
    public static void main(String[] args) throws Exception {
        InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader bufRead = new BufferedReader(istream) ;
        System.out.print("Please enter the starting number:");
        int start = Integer.parseInt(bufRead.readLine());
        System.out.print("Please enter the ending number:");
        int stop = Integer.parseInt(bufRead.readLine());
        for (int i=start; i < stop; i++) {
            filename = destinationFolder + "khaitest" + i;
            File fd = new File(filename);
            fd.delete();
        }
        System.out.println("Please see that you can start your weblogic and load your page");
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License