Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.

Similar presentations


Presentation on theme: "Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA."— Presentation transcript:

1 Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA

2 From Lecture 4: How to Use java.util.Scanner 1. import java.util.Scanner; 2. Create an instance of the Scanner class, a Scanner object: Scanner scanner = new Scanner(System.in); //We set up Scanner object with the name “scanner” 3. Do Stuff with it 4. Close it to free up resources: scanner.close();

3 From Lecture 4: Scanner Input Methods  https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html  Since most of the the methods are instance methods, you have to create the scanner object before you can use it  The main method is not an instance method because of the keyword “static”  For example, methods in String and Math are static methods  You can read an entire line, a String, a double, an int, etc.  Read the documentation and lecture code examples to learn more about Scanner methods

4 Unsatisfying Conclusion From Lecture 4: How to Read an entire line?  ??????????????????????????????  See Scanner lecture code example

5 Debugging a Tricky Logical Error 1. Identify the bug behavior 2. Identify how the “bug” occurred 1. What was the program doing at the moment 2. What was the program doing prior 3. What was the program after 3. Read documentation 1. Make sure it is not something as simple as using the method wrong 4. If you can’t figure it out, google it or ask your friend for help

6 Debugging nextLine() Skipping 1. Identify the bug behavior 1. nextLine() doesn’t read the and seems to be skipped 2. Identify how the “bug” occurred 1. What was the program doing at the moment: 1. Trying to read an entire line 2. What was the program doing prior 1. Read in an int using nextInt 3. What was the program doing after 1. Suppose to print out the result

7 Taking a Deeper Look at Scanner.nextInt()  Reading the documentation:  public int nextInt()  Scans the next token of the input as an int.  What is a token?  “A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.”  When you type a number, then press ENTER, nextInt() reads your number as an integer, but it didn’t read the carriage return (remember /r?)

8 So How to Mitigate the Issue? 1. Call nextLine() after reading a token using nextInt(), nextDouble(), etc to consume the /r character 2. Use static parsing methods (not instance methods) provided by the various classes, and just read in the entire line then convert it. 1. Integer.parseInt() 2. Double.parseDouble()

9 Useful Scanner Methods  nextInt(): reads the next token as an int  next(): reads the next token as a string  nextLine(): reads the entire line, consumes end of line(carriage return)  nextDouble(): reads the next token as a double  nextBoolean(): reads in the next token as a boolean

10 What Exactly Are Methods?  We talked about the main method, we also used methods from Scanner  Methods:  A block of code that can be called using the identifier  It can do things without any input/output, like System.out.println()  You can pass in parameters as inputs  You can also specify the method return type, and return a value of that type using the return keyword.

11 Example Method: public double calculateTriangleArea(double base, double height) { double area = base * height / 2; return area; } Return type Method Name/Identifier Parameters Return a value with type matching the return type specified

12 Another Example Method: public void calculateTriangleArea(double base, double height) { System.out.println("I'm just printing out stuff"); } Return type is void, so not returning anything

13 Using Methods  Recall the differences between static and instance methods:  Static methods belong to the class  Has the “static” keyword between “public” and the return type  Using static methods:  CLASSNAME.STATIC_METHOD_NAME()  Example: Integer.parseInt(“23456”);  Instance methods belong to the objects that are instances of the class  Does not have the “static” keyword  Using instance methods:  Create an object first  OBJECT_NAME.METHOD_NAME()  Example: scanner.nextInt()

14 What Do I Use In CS201 When I Write My Own Methods?  If I don’t specify, make the methods static.  For example, you have a class MyColor, with a static method that prints out the color passed in. In the main method, you will do MyColor.printColor(“blue”)  For Codingbat assignements, use instance methods because how they test your code.

15 Codingbat  http://codingbat.com/ http://codingbat.com/  Free interactive learning site created by Nick Parlante at Stanford  Gives you immediate feedback on your code  Tests your method using different inputs  IMPORTANT: Sign up for an account to do the problems assigned in the labs  Use your real name so I know which student you are  Add my email (mhsu0020@gmail.com) to the Share To field under Preferences so I know you did the workmhsu0020@gmail.com  http://codingbat.com/pref http://codingbat.com/pref  Otherwise you won’t get credit for it  Remember to login every time you do an assignment on codingbat

16 More on Codingbat  Solutions for all problems are widely available online  I encourage you to NOT copy solutions, as there will be problems on the quiz/midterm/final that are modeled after codingbat problems.

17 System.out.println() and System.out.print()  println writes the string literal you passed in, then write a new line character at the end  print does not add a new line character at the end


Download ppt "Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA."

Similar presentations


Ads by Google