



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to read input files in java using a scanner object and the file class. It also covers java's dynamic array allocation and the use of arrays of primitives and objects. The vector class is introduced as a collection class that can store different types of objects and grow and shrink dynamically.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




This can be done extremely similarly to reading from the keyboard. A Scanner object can still be used. But, instead of passing System.in into the constructor, a File object must be passed in: Scanner fin = new Scanner(new File("anyfile.txt")); In order to create a File object, the File constructor must be called. This constructor simply takes in a String that stores the name of the file. Thus, in a single statement, we can set a Scanner object to read from a file instead of the keyboard. Once the Scanner object is "set up," then you use it EXACTLY as we have been using Scanner objects until now. Use the following methods: next(), nextInt(), and nextDouble(). (There are other methods, but these are the ones you'll be using most frequently.) The only difference between reading from the keyboard and a file is that you should close a file once you are done reading from it. Thus, you must close the appropriate Scanner object. The name of the method is close and it takes no parameters: fin.close();
Just like all other non-primitives in Java, arrays are references. Unlike C, all arrays are dynamically allocated. This means that the size of an array is always specified at run-time. Since a dynamic allocation is done, we use the new keyword as follows: int[] values = new int[10]; This allocates an array of size 10 that values references. Consider the following picture: From this point, we use arrays of primitives exactly as they would be used in C. To reference an individual array slot, we use the [] operator. The following segment of code initializes each slot in the array values to 0: for (int i=0; i<values.length; i++) values[i] = 0; One key idea to notice here that is different than C is that you can access the length of an array, using the length field. Note that length is a public variable for all arrays. Thus, there are no parentheses (which would indicate a method call). Let's look at an example that utilizes Java's dynamic array allocation capabilities.
Java provides a few classes that allow us to manage collections of objects. Today, we'll discuss the Vector class, which stores a group of objects. One of the "limitations" of an array in C is that it can only store one type of object. In Java, a Vector can store different types of objects. (Actually so can an array.) Secondly, arrays in C are of a fixed size. (Actually you can also declare dynamically allocated arrays in C.) The Vector class in Java essentially handles both of these issues very well. A Vector object can arbitrarily grow and shrink as needed without the user worrying about any of the details. Furthermore, a Vector can store different types of objects. Well, this is a bit of a stretch actually. The manner in which Java gets away with having Vectors that can store different types of objects is through its "tree" of inheritance. All non- primitives are objects, and some objects inherit from others. For example, if I wrote a Car class, I could write a SportsCar class that inherited all the methods and variables of the Car class, but then maybe had some extra variables and methods that were specific to the SportsCar. Furthermore, then I could maybe have a Corvette class that inherited from SportsCar. It would retain all the attributes of the SportsCar class, but then have some of its extra attributes. In this example, we have the following: SportsCar inherits from Car, so a SportsCar object IS-A Car object. Corvette inherits from SportsCar, so a Corvette object IS-A SportsCar object AND a Car object.
Java also provides a very basic class called Object. Every non- primitive class automatically inherits from Object. Thus, in a way, all non-primitives are valid "Object" objects. Thus, if we create an array of Object or a Vector of Object, then that collection can store ANY sort of non-primitive, ranging from a Time object, to a String object, to a Corvette object. In the old version of Java, the way to create an empty Vector was as follows: Vector v = new Vector(); This looks like a regular default constructor. In the new version of Java, templates are used. This essentially means that some classes can be based upon a "template" class, where you fill in the class you desire. For a Vector object, you are allowed to declare a Vector of any type of class. Here is how we can declare a Vector of class Object: Vector
Most of the methods are quite self-explanatory, but a couple things need to be mentioned: