Results 1 to 4 of 4
  1. #1
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy

    [TUT]How to read input from keyboard

    You are going to want to start off by importing the Scanner object in the util package (i use wildcard to import the whole package, you don't have to if you don't want to).

    Code:
    import java.util.*;
    public class Reca {
    
    	public static void main(String[] args) {
    
    	}
    	
    }
    Next we are going to want to create our Scanner object and pass a parameter of System.in to it.

    Code:
    import java.util.*;
    public class Reca {
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    	}
    	
    }
    There are various types of ways to grab input, I'm not going to go into detail on how to use each one, but I am going to show you how to grab a string. (Since it probably would be the most useful data type seeing as you can do anything to a string!)

    You want to create a variable and assign it the Scanner reference ("input") and the method we are going to use is "nextLine()" to grab the line of input.
    Code:
    import java.util.*;
    public class Reca {
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String s = input.nextLine();
    	}
    	
    }
    That is pretty much how to use the Scanner object to grab input from the keyboard, in a nutshell!

    here is a test code to show you that it works

    Code:
    import java.util.*;
    public class Reca {
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("What is your name? ");
    		String s = input.nextLine();
    		System.out.println("Your name is " + s);
    	}
    	
    }
    If you have any troubles, or questions feel free to ask! :]

  2. The Following 2 Users Say Thank You to XORxHACK For This Useful Post:

    Cilk (03-08-2010),nepito (03-13-2010)

  3. #2
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Noes! =P
    Code:
     string str = JOptionPane.ShowInputDialog("Enter your name: " ); // Don't know if I did that right
    for the win. I miss that's its harder to do something like that in C++. You've gotta use the windows API and make some textbox, check if Unicode or ASCII, etc. There should be a message box type thing in the Windows API like JOptionPane. ;(

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  4. #3
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy
    Quote Originally Posted by why06 View Post
    Noes! =P
    Code:
     string str = JOptionPane.ShowInputDialog("Enter your name: " ); // Don't know if I did that right
    for the win. I miss that's its harder to do something like that in C++. You've gotta use the windows API and make some textbox, check if Unicode or ASCII, etc. There should be a message box type thing in the Windows API like JOptionPane. ;(
    DANG! XD way to come kill everything aha, well now they know how to use console input and JOptionPane! XD

  5. #4
    kronus980's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Location
    Long Beach, California
    Posts
    44
    Reputation
    10
    Thanks
    1
    My Mood
    Aggressive
    Quote Originally Posted by why06 View Post
    Noes! =P
    Code:
     string str = JOptionPane.ShowInputDialog("Enter your name: " ); // Don't know if I did that right
    for the win. I miss that's its harder to do something like that in C++. You've gotta use the windows API and make some textbox, check if Unicode or ASCII, etc. There should be a message box type thing in the Windows API like JOptionPane. ;(
    Aah. I don't know much about C++ but that sounds difficult :/ Coulden't you bring up the cmd console thingy :/ And nice tutorial






    [IMG]https://i931.photobucke*****m/albums/ad158/kronus980/bg_logo.png[/IMG]



Similar Threads

  1. Replies: 0
    Last Post: 07-07-2010, 10:20 AM
  2. Tut How to Play NA From Eu?
    By edenmor11 in forum Combat Arms Help
    Replies: 19
    Last Post: 05-05-2010, 05:48 AM
  3. [TUT]How to read and write to a text file
    By XORxHACK in forum Java
    Replies: 18
    Last Post: 02-26-2010, 04:41 PM
  4. TuT] How to Change File Attributes from Read only to Normal [TuT
    By XGelite in forum Visual Basic Programming
    Replies: 1
    Last Post: 11-19-2009, 01:08 PM
  5. [TUT] How to launch SF from your desktop
    By donh in forum Soldier Front Hacks
    Replies: 9
    Last Post: 04-13-2009, 12:46 AM