Results 1 to 4 of 4

Thread: User input from the console

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved User input from the console

    How can i get user input from the console?

    Here is my code so far if it helps:
    Code:
    public class Student
    {
    	private static String Teacher;
    	private static String StudentName;
    
    	/**
    	 * Create a student
    	 *
    	 * @param StudentName
    	 */
    	public Student()
    	{
                //Do nothing!
    	}
    	
    	public static void main(String StudentName)
    	{
    	    Student student = new Student();
    	    	    
                Teacher = "Mr Whyley";
                StudentName = StudentName;
                //Print details
                System.out.println("Student Name: " + StudentName);
                System.out.println("Teacher: " + Teacher);
    	}
    
    	/**
    	 * Get teacher name for a student
    	 * 
    	 * @return Teacher
    	 */
    	public String getTeacher()
    	{
    	    return Teacher;
    	}
    }
    I want to get 'StudentName' from the console.
    Last edited by x-ice; Feb 25th, 2007 at 07:17 PM.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: User input from the console

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    use br.readLine() to get the text,but be sure to catch an io exception with this line.


    *import java.io.*;

  3. #3
    New Member Santa Clause's Avatar
    Join Date
    Dec 2003
    Location
    North Pole. Lapland when I visit my Wife.
    Posts
    10

    Re: User input from the console

    Shouldn't this declaration of the main method be as follows:
    Code:
    public static void main(String args[])
    args will then contain an array of strings supplied at the command line. Another way to receive input from the command line.

    Merry Christmas
    I am the real Santa Clause

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: User input from the console

    Quote Originally Posted by Santa Clause
    Shouldn't this declaration of the main method be as follows:
    Code:
    public static void main(String args[])
    args will then contain an array of strings supplied at the command line. Another way to receive input from the command line.

    Merry Christmas
    I didn't catch that, but yes, your main method MUST be public, static, void, called main, and takes an array of strings as an argument.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width