Download presentation
Presentation is loading. Please wait.
Published byTodd Holmes Modified over 8 years ago
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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 ++; }
12
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; }
13
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
14
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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.