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
Coosa
//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;
try
{
while ((c = istream.read()) != -1)
{
ostream.write(c);
}
}
catch(IOException e)
{
System.out.println ("Error: " + e.getMessage());
}
finally
{
istream.close();
ostream.close();
}
System.exit(0);
}
}
/////////////////////////////////////////////////////////////////
//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 (
null,
"The Sum is "+ sum,
output,
JOptionPane.INFORMATION_MESSAGE
);
}
}
/////////////////////////////////////////////////////////////////////
//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;
* }