Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables.

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Advertisements

More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Chapter 6 Loops and Files. 2 Knowledge Goals Understand the semantics of while loop Understand when a count-controlled loop is appropriate Understand.
Java Syntax Primitive data types Operators Control statements.
Chapter 2: Java Fundamentals Input and Output statements.
18 File handling1June File handling CE : Fundamental Programming Techniques.
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.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
Review Java.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
Chapter 2 Elementary Programming
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
Reading External Files By: Greg Patterson APCS 2010.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Using Java Class Library
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Streams and File. 1. Introduction Data stored in variables and arrays is temporary For long-term retention of data, even after the programs that create.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
CS 160 – Summer 16 Exam 1 Prep.
Chapter 6 Loops and Files
Reading from a file and Writing to a file
Project 1.
Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Streams and File I/O.
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Computer Programming Methodology Input and While Loop
Repetition.
CSS161: Fundamentals of Computing
الكلية الجامعية للعلوم التطبيقية
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
A+ Computer Science INPUT.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
File Handling in Java January 19
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Basic operation of String Searching Arrays Sorting Arrays
Presentation transcript:

Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables Fall 2012 CS2302: Programming Principles 1

File I/O  Use Scanner with File : Scanner inFile = new Scanner(new File(“in.txt”));  Similar to Scanner with System.in : Scanner keyboard = new Scanner(System.in); Fall 2012 CS2302: Programming Principles 2

Reading in int ’s Scanner inFile = new Scanner(new File(“in.txt")); int number; while (inFile.hasInt()) { number = inFile.nextInt(); // … } Fall 2012 CS2302: Programming Principles 3

Reading in lines of characters Scanner inFile = new Scanner(new File(“in.txt")); String line; while (inFile.hasNextLine()) { line = inFile.nextLine(); // … } Fall 2012 CS2302: Programming Principles 4

Multiple types on one line // Name, id, balance Scanner inFile = new Scanner(new File(“in.txt")); while (inFile.hasNext()) { name = inFile.next(); id = inFile.nextInt(); balance = inFile.nextFloat(); // … new Account(name, id, balance); } String line; while (inFile.hasNextLine()) { line = inFile.nextLine(); Scanner parseLine = new Scanner(line) // Scanner again! name = parseLine.next(); id = parseLine.nextInt(); balance = parseLine.nextFloat(); // … new Account(name, id, balance); } Fall 2012 CS2302: Programming Principles 5

Summary  Create a Scanner object Scanner inFile = new Scanner(new File(“in.txt"));  Use the methods next(), nextByte(), nextShort(), nextInt(), nextLong(), next Float(), nextDouble(), or nextBoolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. For example, double d = inFile.nextDouble(); Fall 2012 CS2302: Programming Principles 6

My suggestion  Use Scanner with File – new Scanner(new File(“in.txt”))  Use hasNext…() to check for EOF – while (inFile.hasNext…())  Use next…() to read – inFile.next…()  Simpler and you are familiar with methods for Scanner Fall 2012 CS2302: Programming Principles 7

Multiple types on one line // Name, id, balance Scanner inFile = new Scanner(new File(“in.txt")); String line; while (inFile.hasNextLine()) { line = inFile.nextLine(); Account account = new Account(line); } public Account(String line) // constructor { Scanner accountLine = new Scanner(line); _name = accountLine.next(); _id = accountLine.nextInt(); _balance = accountLine.nextFloat(); } Fall 2012 CS2302: Programming Principles 8

Ending a Loop with a Sentinel Value Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a special input values is known as a sentinel value. Fall 2012 CS2302: Programming Principles 8

Example double array[] = new double[100]; // counter for how many are stored int arraySize = 0; Scanner inFile = new Scanner(new File(“in.txt")); double value, sentinel = -1; value = inFile.nextDouble(); while (value != sentinel) { array[arraySize] = value; arraySize++; value = inFile.nextDouble(); } System.out.println("Array has " + arraySize + " elements"); inFile.close(); Fall 12 CS2302: Programming Principles 9

Insertion Fall 2012 CS2302: Programming Principles 10 public void insertAt(double[] data, double val, int index) { if(index arraySize) { System.out.println("Insert index out of bounds"); } else if(arraySize >= data.length) { System.out.println("Partial array is full"); } else { //move elements to the right for(int j = arraySize; j > index; j--) { data[j] = data[j-1]; } // put the new value in place data[index] = val; // one more element stored arraySize ++; }

Deletion Fall 2012 CS2302: Programming Principles 11 public double removeAt(double[] data, int index) { double value; if(index arraySize) { System.out.println("Insert index out of bounds"); } else if(arraySize >= data.length) { System.out.println("Partial array is full"); } else { value = data[index]; //move elements to the left for(int j = index; j <= arraySize; j++) { data[j] = data[j+1]; } arraySize --; } return value; }

The static keyword  Java methods and variables can be declared static  These exist independent of any object  This means that a Class’s – static methods can be called even if no objects of that class have been created and – static data is “shared” by all instances Fall 2012 CS2302: Programming Principles 12

Example 14 Fall 2012 CS2302: Programming Principles class StaticTest { static int i = 47; public static void main(String[] args){ i++; System.out.println(“Before call increase, i = ” + i); increase(); System.out.println(“After call increase, i =” + i);} public static void increase(){ i++; System.out.println(“Inside increase, i = ” + i); }