Skip to main content

APIzation: Replication Package

[Q#2296685][A#2297629] How to read input with multiple lines in Java

https://stackoverflow.com/q/2296685

Our professor is making us do some basic programming with Java, he gave a website and everything to register and submit our questions, for today I need to do this one example I feel like I'm on the right track but I just can't figure out the rest. Here is the actual question: And here is what I've got so far : Now I always get the answer 2 because I'm reading the single line, how can I take all lines into account? thank you For some strange reason every time I want to execute I get this error: Whatever version of answer I use I get this error, what do I do ? However if I run it in eclipse Run as > Run Configuration -> Program arguments I get no output EDIT I have made some progress, at first I was getting the compilation error, then runtime error and now I get wrong answer, so can anybody help me what is wrong with this:

Answer

https://stackoverflow.com/a/2297629

I finally got it, submited it 13 times rejected for whatever reasons, 14th "the judge" accepted my answer, here it is :

APIzation

import java.io.BufferedInputStream;
import java.util.Scanner;

public class HashmatWarrior {

    public static void main(String args[]) {
        Scanner stdin = new Scanner(new BufferedInputStream(System.in));
        while (stdin.hasNext()) {
            System.out.println(Math.abs(stdin.nextLong() - stdin.nextLong()));
        }
    }
}
package com.stackoverflow.api;

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Human2297629 {

  public static StringBuilder readMultipleLongs() {
    StringBuilder lines = new StringBuilder();
    Scanner stdin = new Scanner(new BufferedInputStream(System.in));
    while (stdin.hasNext()) {
      lines.append(Math.abs(stdin.nextLong() - stdin.nextLong()));
    }
    return lines;
  }
}

package com.stackoverflow.api;

import java.io.BufferedInputStream;
import java.util.Scanner;

/**
 * How to read input with multiple lines in Java
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/2297629">https://stackoverflow.com/a/2297629</a>
 */
public class APIzator2297629 {

  public static void readInput() {
    Scanner stdin = new Scanner(new BufferedInputStream(System.in));
    while (stdin.hasNext()) {
      System.out.println(Math.abs(stdin.nextLong() - stdin.nextLong()));
    }
  }
}