Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Input Streams

  Asked By: Richard    Date: May 26    Category: Java    Views: 1052

I'm starting to switch to Java where i learned earlier C and C++.
Currently i'm facing difficuluties writing a simple program that promts the
user to input 2 numbers, where the program then calculates the sum for
example and displays it on the screen.
As i searched for classes responsible for input streams i couldn't find a
way except to read character by character or a stream as whole.
I wish to kopw simply how to input various data types such as integers,
double, characters, ..etc. either by a line reading where the function would
recognize the '\n' character or even write a multiple data types in one line
separated by spaces.
I struggled a little to create such a program InputStreamsCon.java but i
believe there must be an easier way in Java to do it using direct integer
inputs insted of strings.
The second program InputStreams.java doesn't use the console, it uses the
JOptionPane insted but i also couldn't do it in separated spaces.
Could some one please help me with that?
The last program which uses the console is ReadKBWriteScreen.java; it does
direcctly take an integer data type as input, jowever it doesn't recognize
the end of line automatically. In the code it's inside a while loop untill
-1 is returned.

What i need is:

1.) A simple data class method that reads ANY data type as input and
recognizes the end of line.
2.) A simple data class method that reads ANY data type as input and
recognizes the space character to accept multiple variables.
3.) Small examples for illustration

Best regards


//File: ReadKBWriteScreen.java
import java.io.*;
public class ReadKBWriteScreen
public static void main (String [] args)
throws IOException
System.out.print ("Input a number: ");
InputStream istream;
OutputStream ostream;
int c;
istream = System.in;
ostream = System.out;
while ((c = istream.read()) != -1)
catch(IOException e)
System.out.println ("Error: " + e.getMessage());

//File: InputStreams.java
import javax.swing.JOptionPane;

public class InputStreams
public static void main(String[] args)
String input, output = "Results";
int num1, num2, sum;
input = JOptionPane.showInputDialog ("Input first number");
num1 = Integer.parseInt (input);
input = JOptionPane.showInputDialog ("Input second number");
num2 = Integer.parseInt (input);
sum = num1 + num2;
JOptionPane.showMessageDialog (
"The Sum is "+ sum,


//File: InputStreamsCon.java
import java.io.*;

class InputStreamsCon
public static void main(String[] args) throws IOException
String str = "", subStr1 = "", subStr2 = "";
int SplitIndex = 0, num1, num2, Sum;
System.out.print("Input two Values separated by space: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
str = in.readLine ();
for (int i = 0; i<str.length (); i++)
if (str.charAt (i) == 32)
SplitIndex = i;
subStr1 = str.substring (0,SplitIndex);
subStr2 = str.substring (SplitIndex+1,str.length());
num1 = Integer.parseInt (subStr1);
num2 = Integer.parseInt (subStr2);
Sum = num1+num2;
System.out.println("The Sum = " + Sum);
* In C++ it's simpler to write two inputs in this format, namely:
* //File: streams.cpp
* #include <iostream>
* using namespace std;
* int main (void)
* {
* int num1, num2, sum;
* cout << "Input two numbers separated by space: ";
* cin >> num1 >> num2; // Incredibly simple!
* sum = num1 + num2;
* cout << "Sum = " << sum << endl;
* return 0;
* }
* Even in C, namely:
* //File: streams.c
* #include <stdio.h>
* int main (void)
* {
* int num1, num2, sum;
* printf("Input two numbers separated by sopace: ");
* scanf("%d %d", &num1, &num2); //Very simple as well
* sum = num1 + num2;
* printf("The Sum = %d", sum);
* return 0;
* }



3 Answers Found

Answer #1    Answered By: Kellie Bishop     Answered On: May 26

Check out the Reader and Writer classes  and their
implementations, especially the FileReader and

Answer #2    Answered By: Mona Wagner     Answered On: May 26

and if you just want to read  stuff in from a string  you can u se
BufferedReader and BufferedWriter

Answer #3    Answered By: Eloise Lawrence     Answered On: May 26

Using the combination of BufferedReader and StringTokenizer...

Here is the example...

import java.io.*;
public class  Token1 {
public static  void main(String[] args) throws  Exception {
double sum=0;
System.out.println("Type two or more numbers  separated by space
to add:(Example: 4 5.6 8.2 2");
//Using BufferedReader recognize the return key input
BufferedReader br = new BufferedReader(new InputStreamReader
String s1 = br.readLine();
//Using string  Tokenizer will split the data  using the
delimiter space.
java.util.StringTokenizer st =new java.util.StringTokenizer
while (st.hasMoreTokens()) sum  += Double.parseDouble
System.out.println(" Sum = "+sum);} }

Didn't find what you were looking for? Find more on Input Streams Or get search suggestion and latest updates.