Java Utililty Methods Scanner Usage

List of utility methods to do Scanner Usage

Description

The list of methods to do Scanner Usage are organized into topic(s).

Method

ListstringToDoubleList(String s)
string To Double List
Scanner scanner = new Scanner(s);
if (s.indexOf(',') != -1) {
    scanner.useDelimiter(",");
List<Double> data = new ArrayList<Double>();
while (scanner.hasNextDouble()) {
    data.add(scanner.nextDouble());
scanner.close();
return data;
voidswapElements(int length, int maxValue)
swap Elements
int[] randomArray = new int[length];
for (int i = 0; i < randomArray.length; i++) {
    randomArray[i] = (int) (Math.random() * maxValue);
    System.out.print(randomArray[i]);
Scanner scan = new Scanner(System.in);
System.out.println("\nEnter two indexes from the array above: ");
int index1 = scan.nextInt();
...
StringtermInput()
term Input
Scanner in = new Scanner(System.in);
String result = "";
boolean done = false;
while (!done) {
    result = in.nextLine();
    if (result.length() < 3) {
        System.out.println("Please enter at least one keyword that's " + "at least 3 characters long:");
    } else {
...
voidTokenize(String s, String s2, String s3)
Tokenize
StringTokenizer tok = new StringTokenizer(s, " ");
System.out.println("Jumlah token: " + tok.countTokens());
int i = 0;
while (tok.hasMoreTokens()) {
    System.out.println(tok.nextToken());
    i++;
System.out.println("Jumlah token: " + i);
...
LongtoNumeric(String ip)
Method to convert ip value in long.
Scanner sc = new Scanner(ip).useDelimiter("\\.");
return (sc.nextLong() << 24) + (sc.nextLong() << 16) + (sc.nextLong() << 8) + (sc.nextLong());
ArrayListtoWords(String content)
to Words
Scanner s = new Scanner(content);
s.useDelimiter("\\s");
ArrayList<String> wordsArray = new ArrayList<>();
while (s.hasNext()) {
    wordsArray.add(s.next().toLowerCase());
s.close();
return wordsArray;
...
StringtransformTXTandANNtoRQS(String TXT, String ANN)
Transforms a txt and an ann string to a string complying to the rqs format.
ArrayList<String> requirements = new ArrayList<String>();
Scanner txtScanner = new Scanner(TXT);
while (txtScanner.hasNextLine()) {
    requirements.add(txtScanner.nextLine());
txtScanner.close();
ArrayList<String> annotations = new ArrayList<String>();
Scanner annScanner = new Scanner(ANN);
...
Stringtrim(String s)
Removes all ending \r and/or \n
if (s == null)
    return null;
Scanner scanner = new Scanner(s);
String res = "";
while (scanner.hasNextLine()) {
    if (!res.equals("")) {
        res = res + "\n";
    res = res + scanner.nextLine();
return res;
intvalidateInt(Scanner keyboard, String message, String error)
validateInt - validates that a user has entered a correct value such as an integer.
int returnValue = 0;
boolean isError = true;
do {
    try {
        System.out.println(message);
        returnValue = keyboard.nextInt();
        keyboard.nextLine();
        isError = false;
...
voidwaitForEnter()
Calling this method waits until the user presses enter (scans standard input for next line), writing a default message to standard output: "[Press enter to continue]".
waitForEnter("[Press enter to continue]");