Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
iherage@gmail.com - 26 Jun 2005 20:51 GMT
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?
iamfractal@hotmail.com - 26 Jun 2005 21:39 GMT
> I used to use c++.If you want to get input you write
> #include <iostream>
[quoted text clipped - 9 lines]
> 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
millerch - 26 Jun 2005 21:43 GMT
> I used to use c++.If you want to get input you write
> #include <iostream>
[quoted text clipped - 9 lines]
> 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());
 }
}
}
ChrisWSU - 27 Jun 2005 04:47 GMT
java 5 added a new toy for this also

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.