Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read input with multiple lines in Java

Tags:

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:

**Sample Input:** 10 12 10 14 100 200  **Sample Output:** 2 4 100 

And here is what I've got so far :

public class Practice {      public static int calculateAnswer(String a, String b) {         return (Integer.parseInt(b) - Integer.parseInt(a));     }      public static void main(String[] args) {         System.out.println(calculateAnswer(args[0], args[1]));     } } 

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:

C:\sonic>java Practice.class 10 12 Exception in thread "main" java.lang.NoClassDefFoundError: Fact Caused by: java.lang.ClassNotFoundException: Fact.class         at java.net.URLClassLoader$1.run(URLClassLoader.java:20         at java.security.AccessController.doPrivileged(Native M         at java.net.URLClassLoader.findClass(URLClassLoader.jav         at java.lang.ClassLoader.loadClass(ClassLoader.java:307         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.         at java.lang.ClassLoader.loadClass(ClassLoader.java:248 Could not find the main class: Practice.class.  Program will exit. 

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

10 12 10 14 100 200 

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:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger;  public class Practice {      public static BigInteger calculateAnswer(String a, String b) {         BigInteger ab = new BigInteger(a);         BigInteger bc = new BigInteger(b);         return bc.subtract(ab);     }      public static void main(String[] args) throws IOException {         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));          String line;           while ((line = stdin.readLine()) != null && line.length()!= 0) {              String[] input = line.split(" ");              if (input.length == 2) {                  System.out.println(calculateAnswer(input[0], input[1]));              }          }      } } 
like image 280
Gandalf StormCrow Avatar asked Feb 19 '10 13:02

Gandalf StormCrow


People also ask

How do you read multiple lines of text in Java?

Another way to read multiple lines from the console can be done using the synchronized BufferedReader class in Java. The idea is to read each line using the readLine() method and use String. split() to split the line into individual tokens using whitespace as a delimiter. We can also use the StringTokenizer class.

How do you read multiple line inputs?

1st Method: inputlist = [] while True: try: line = input() except EOFError: break inputlist. append(line) 2nd Method import sys inputlist = sys. stdin. readlines() print(inputlist) This will take multi-line input however you need to terminate the input (ctrl+d or ctrl+z).

How do you scan two inputs in one line in Java?

To do this, we could read in the user's input String by wrapping an InputStreamReader object in a BufferedReader object. Then, we use the readLine() method of the BufferedReader to read the input String – say, two integers separated by a space character. These can be parsed into two separate Strings using the String.

Can we take multiple inputs in Java?

In java. util package, the scanner is one of the classes that help in collecting multiple inputs of the primitive types such as double, integer, strings, etc.


1 Answers

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

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()));         }     } } 
like image 54
Gandalf StormCrow Avatar answered Sep 22 '22 07:09

Gandalf StormCrow