How can I get the input from keyboard without using swing?

I

iherage

I used to use c++.If you want to get input you write
#include <iostream>
using namespace std;
int main(void)
{
int a,b;
cout<"Please input a & b";
cin>>a>>b;
count<<"what you input is a="<<a<<",b="<<b;
return 0;
}
Can I use java to get the input from the keyboard like this without
using the GUI?
 
I

iamfractal

I used to use c++.If you want to get input you write
#include <iostream>
using namespace std;
int main(void)
{
int a,b;
cout<"Please input a & b";
cin>>a>>b;
count<<"what you input is a="<<a<<",b="<<b;
return 0;
}
Can I use java to get the input from the keyboard like this without
using the GUI?

The indentation will be mangled, but hreanyway, here's a method ...

/**
* Returns a single line read from the keyboard.
*
* @return the keyboard-entered line
*/
private String readLine() {
String input = "";
BufferedReader standard =
new BufferedReader(new InputStreamReader(System.in));
try {
input = standard.readLine();
} catch (Exception e) {
System.out.println("Ooops: " + e);
}
return input;
}


..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 
M

millerch

I used to use c++.If you want to get input you write
#include <iostream>
using namespace std;
int main(void)
{
int a,b;
cout<"Please input a & b";
cin>>a>>b;
count<<"what you input is a="<<a<<",b="<<b;
return 0;
}
Can I use java to get the input from the keyboard like this without
using the GUI?

How about something like:

import java.io.*;
class ConsoleApp
{
public static void main(String[] args)
{
try
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input a & b");
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());

System.out.println("What you input is a="+a+",b="+b);
}
catch (IOException e)
{
System.out.println("Error reading input: "+e.getMessage());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top