Which of the following statements contains an error? I. String firstName = in.next(); II. String lastName = in.nextLine(); III. System.out.print("/n"); 1 & 2 right?
http://pastebin.com/QJ8uJAWr as you can see, the code works quite well using all of your options. The only thing that might be off is whether it assumes you already used this line of code or not: ``` Scanner in = new Scanner(System.in); ``` with this import: ``` import java.util.Scaner; ```
can you break this down and help me understand it Scanner in = new Scanner(System.in); Scanner is the class in is the object not sure what = new Scanner(System.in) does
Though nextLine() has to be before next().. unless you do ``` String firstname = in.next(); in.nextLine(); String lastname = in.next(); ``` this has to do with something called the keyboard buffer.. though I wouldn't worry about that for now.
Sure. You will see similar statements over and over with all of the new classes that you learn.. ``` Scanner youChooseThisName = new Scanner(System.in); ``` so... this gets to the heart of what OOP (object oriented programming) is all about. When you use this statement, you create an "instance of the Scanner class". Imagine that there is a Scanner class out there.. just like the HelloWorld classes that you've made.. and by creating an "instance" of the class, you can call forth some of that program and code.. So you are using already written code-- and it wont be too long before you make your own classes like scanner, and access them in the same way. The new keyword (special words in java are called keywords aka; reserved words) means that you are creating an object.. you can create objects of many classes: ie; ``` StringTokenizer st = new Stringtokenizer("Hello World"); StringBuilder sbb = new StringBuilder("I will manipulate this string."); File file = new File("C:\\Users\\woodrow\\desktop\\textDoc.txt"); ``` and by using the word that you define.. (st, sbb, file, in) you can access that class's methods.. a method is some prewritten code... it is fairly straight forward -- you can make your own method like so: ``` import java.util.Scanner; public class JustInTimeForJava { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("What's your first name?"); String firstName = in.next(); System.out.println("What's your last name?"); in.nextLine(); String lastName = in.next(); System.out.println("/n"); System.out.println("/n"); System.out.println("Hello, " + firstName + " " + lastName); //now I will call the method that I made below sayHelloToTheWorld50Times(); } public static void sayHelloToTheWorld50Times(){ int accumulator = 0; while(accumulator < 50) { System.out.println("Hello World"); accumulator = accumulator +1; } } } ``` So when you do ``` in.next(); ``` You are really accessing the next() method which is inside of the Scanner class. Just like the method we just made. remember that when you create an object.. it holds information... it remembers information, and you can change that information with different methods.. pretend we have access to a class called ManipulateIntVariableCalledX, I'll create a couple instances, and show you how it works, maybe it'll click for you: ``` public class Main { public static void main(String[] args){ ManipulateIntVariableCalledX meObject = new ManipulateIntVariableCalledX(); meObject.setX(500); //sets it's information on x = to 500 ManipulateIntVariableCalledX different = new ManipulateIntVariableCalledX(); meObject.addToX(44); //adds 44 to x different.setX(33000); //sets x inside of the different object to 33000 //the different object and the meObject are different int getValueFromFirstInstance = meObject.getX(); //returns x's value as int int getValueFromDifferent = different.getX(); System.out.println("First instance: " + getValueFromFirstInstance); System.out.println("Second instance: " + getValueFromDifferent); } } ``` The above program will print: ``` First instance: 544 Second instance: 33000 ``` and this calls methods inside of the ManipulateVariableCalledX object. In terms of understanding how to create your own class, I wouldn't worry about it -- definitely work on making methods first within your own class like the "hello world" 50 times method. As you progress it'll make more and more sense.
Feel free to apply some of your skills at projecteuler.net - that is if you enjoy challenges.
@alprinceofnofl
@alprincenofl
@woodrow73 thanks a lot for your effort and the website link, i am checking it right now sorry about late reply, because my semester finals is soon and I am busy.
@alprincenofl I notified you on this post b/c I knew you were looking to learn more java - which I explained a bit on this thread. Good luck on your finals my friend.
Join our real-time social learning platform and learn together with your friends!