Objectives 1 File IO using Scanner with File
Objectives 1 § File I/O: using Scanner with File § Inserting into Partially Filled Array § Deleting from Partially Filled Array § Static methods and variables Fall 2012 CS 2302: Programming Principles
File I/O Use Scanner with File: Scanner in. File = new Scanner(new File(“in. txt”)); § Similar to Scanner with System. in: Scanner keyboard = new Scanner(System. in); § 2 Fall 2012 CS 2302: Programming Principles
Reading in int’s Scanner in. File = new Scanner(new File(“in. txt")); int number; while (in. File. has. Int()) { number = in. File. next. Int(); // … } 3 Fall 2012 CS 2302: Programming Principles
Reading in lines of characters Scanner in. File = new Scanner(new File(“in. txt")); String line; while (in. File. has. Next. Line()) { line = in. File. next. Line(); // … } 4 Fall 2012 CS 2302: Programming Principles
Multiple types on one line 5 // Name, id, balance Scanner in. File = new Scanner(new File(“in. txt")); while (in. File. has. Next()) { name = in. File. next(); id = in. File. next. Int(); balance = in. File. next. Float(); // … new Account(name, id, balance); } ----------String line; while (in. File. has. Next. Line()) { line = in. File. next. Line(); Scanner parse. Line = new Scanner(line) // Scanner again! name = parse. Line. next(); id = parse. Line. next. Int(); balance = parse. Line. next. Float(); // … new Account(name, id, balance); } Fall 2012 CS 2302: Programming Principles
Summary § Create a Scanner object Scanner in. File = new Scanner(new File(“in. txt")); § Use the methods next(), next. Byte(), next. Short(), next. Int(), next. Long(), next Float(), next. Double(), or next. Boolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. For example, double d = in. File. next. Double(); 6 Fall 2012 CS 2302: Programming Principles
My suggestion § Use Scanner with File – § Use has. Next…() to check for EOF – § 7 while (in. File. has. Next…()) Use next…() to read – § new Scanner(new File(“in. txt”)) in. File. next…() Simpler and you are familiar with methods for Scanner Fall 2012 CS 2302: Programming Principles
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. 8 Fall 2012 CS 2302: Programming Principles
Example double array[] = new double[100]; // counter for how many are stored int array. Size = 0; Scanner in. File = new Scanner(new File(“in. txt")); double value, sentinel = -1; value = in. File. next. Double(); while (value != sentinel) { array[array. Size] = value; array. Size++; value = in. File. next. Double(); } System. out. println("Array has " + array. Size + " elements"); in. File. close(); 9 Fall 12 CS 2302: Programming Principles
Insertion 10 public void insert. At(double[] data, double val, int index) { if(index < 0 || index > array. Size) { System. out. println("Insert index out of bounds"); } else if(array. Size >= data. length) { System. out. println("Partial array is full"); } else { //move elements to the right for(int j = array. Size; j > index; j--) { data[j] = data[j-1]; } // put the new value in place data[index] = val; // one more element stored array. Size ++; } } Fall 2012 CS 2302: Programming Principles
Deletion 11 public double remove. At(double[] data, int index) { double value; if(index < 0 || index > array. Size) { System. out. println("Insert index out of bounds"); } else if(array. Size >= data. length) { System. out. println("Partial array is full"); } else { value = data[index]; //move elements to the left for(int j = index; j <= array. Size; j++) { data[j] = data[j+1]; } array. Size --; } return value; } Fall 2012 CS 2302: Programming Principles
The static keyword § Java methods and variables can be declared static § These exist independent of any object § This means that a Class’s – – 12 Fall 2012 static methods can be called even if no objects of that class have been created and static data is “shared” by all instances CS 2302: Programming Principles
Example class Static. Test { 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); } 14 } Fall 2012 CS 2302: Programming Principles
- Slides: 13