Lecture 19: Reading Input from Files Announcements & Review Lab 6 Due Thursday arrays of objects more flight reservations Last time: static methods vs.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Lecture 2: Classes and Objects, using Scanner and String.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
Reading External Files By: Greg Patterson APCS 2010.
1 Iteration. 2 Java looping  Options while do-while for  Allow programs to control how many times a statement list is executed.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Iteration. Java looping Options –while –do-while –for Allow programs to control how many times a statement list is executed.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Style guide in JAVA From a lecture by Dr. Rahman.
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
ArrayLists (and the for-each loop) ArrayList example = new ArrayList (); example.add(4);
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CSC 142 Computer Science II Zhen Jiang West Chester University
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
COMP Review of Chapter 1 & 2
CSC111 Quick Revision.
COMP Streams and File I/O
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lecture Note Set 1 Thursday 12-May-05
Introduction to Methods in java
Data types, Expressions and assignment, Input from User
Iteration.
שימוש במחלקות קיימות מחרוזות, קבצים, וקבלת קלט מהמשתמש
Something about Java Introduction to Problem Solving and Programming 1.
שימוש במחלקות קיימות מחרוזות, קבצים, וקבלת קלט מהמשתמש
Iteration.
Use proper case (ie Caps for the beginnings of words)
Introduction to Classes and Methods
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
Building Java Programs
Building Java Programs
Announcements & Review
Announcements & Review
Lecture 9 Using Objects.
Building Java Programs
Presentation transcript:

Lecture 19: Reading Input from Files Announcements & Review Lab 6 Due Thursday arrays of objects more flight reservations Last time: static methods vs object methods Today: review static vs object methods Reading from files

Lecture 19: Reading Input from Files Class, Variables, Methods Class - a model for an object instance –one class, lots of objects Class variable declarations – type variableName; private - only accessible through method calls, e.g., get and set methods public - accessible directly, e.g., objectName.varName static - one per class non-static - one per object Class method declarations – methodName; private - only accessible within the class public - accessible from clients static - invoke on the class non-static - invoke on an object of the class

Lecture 19: Reading Input from Files Java Examples in BlueJ Adding static methods for input Putting static methods together with arrays

Lecture 19: Reading Input from Files So far, user input from the console stdin.next(); // a string stdin.nextLine(); // a line of text stdin.nextInt(); // an integer, etc. stdin.hasNextInt(); // First make sure there is one... Scanner stdin = new Scanner (System.in); System.out.println(“Input an integer:”) while (!stdin.hasNextInt()) { System.out.println(“Input an integer:”) stdin.nextLine(); // consumes bad input } int i = stdin.nextInt();

Lecture 19: Reading Input from Files Lots of Input? and/or Persistant Input Store and read input from a file

Lecture 19: Reading Input from Files File Input Syntax Scanner stdin = new Scanner(System.in); System.out.println("Input a file name: "); String fileName = stdin.next(); Scanner groceryFile = new Scanner(new File(fileName)); while (!groceryFile.hasNextInt()) { System.out.println("File did not start with an integer."); groceryFile.nextLine(); // consumes bad input } int totalEntries = groceryFile.nextInt(); Read the file by making it into Scanner object, and then treat it like stdin, where stdin = new Scanner (System.in)

Lecture 19: Reading Input from Files Java Examples in BlueJ Reading from a file Array of objects initialized from a file

Lecture 19: Reading Input from Files More Questions?