Which of the following statements does not contain an error? I. Scanner in = new Scanner(System.in); II. System.out.print("Please enter your name (first last): "); III. String firstName = in.Next();
Depends are we looking for syntax errors, or program functionality?
Try to run the code if you have no idea, it's usually the best way to find out.
Well off the bat in.Next(); will throw an error, the 'N' should be lower-case.
When you do: ``` Scanner in = new Scanner(System.in); ``` Using the 'in' object, you can get user input to the program. So doing ``` System.out.println("User, enter a string."); String userInput = in.nextLine(); System.out.println(userInput + " is what the user typed into the program."); ``` Putting (nextLine()) after in. will allow the user to save what they type into the console to a String in the program.
why can in.next not start with a capital N?
as @Algorithmic said, the (next()) method works for user input as well. Java is a case-sensitive language, meaning that you have to get the capitals all in the right spots for it to work. Though it isn't as intimidating as you might think -- there is a method to the madness.. calling a method like .next() or .nextLine(), you can see that the first character is always lower-case in a method, and the first letter of every word after is uppercase... more methods for example: in.nextDouble(); in.nextInt(); in.nextByte(); in.nextShort(); .setToolTipText("This is a longer method for example");
@woodrow73 refresh my memory methods are "forbidden" from starting with a capitalized letter, that naming convention is reserved for classes, correct?
@Algorithmic right "forbidden". You have it right-- methods don't start with a capital letter, and every word in a class starts with an upper case letter. Though nothing is stopping someone from breaking this 'rule' lol.
thank you guys :)
yw
You are welcome, sorry, I hijacked your thread a tad bit.
Join our real-time social learning platform and learn together with your friends!