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.
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.*;
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 :wave:
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 :wave:
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.