Code Version A -
All, switch in read method

By: Nicholas Duchon

Scanner - back

 Code Versions:

Musings

Home


The entire program, with the MyClassIO constructor set to read the data file, "data.txt".

Example

More about processing an input line.

Consider the following line:

f   681  36.12881  33.97121  34.06929  11.29526   5.56375   6.27562 : Niq Qqp Zna Soy Uax Wxs *

f 681 36.12881 33.97121 34.06929 11.29526 5.56375 6.27562 : Niq Qqp Zna Soy Uax Wxs *
CA
switch
CA
id=nextInt
CE
while (hasNextDouble)
aLe.add (nextDouble)
CE
next
CF
while (t=next != *)
aLf.add (t)

Processing sequence:


// File: MyClassIO.java
// Date: Mar 21, 2017
// Author: Nicholas Duchon
// Purpose:
//    show Scanner constructors in the context of a class hierarchy
//    also show use of super calls in constructor hierarchy

//    Class Hierarchy:
// CA
// +---CB
//    +---CC
//    +---CD
// +---CE
//    +---CF
//    +---CG

import java.util.ArrayList;
import java.util.Random;
import java.util.Comparator;
import java.util.Scanner;
import java.io.PrintStream;
import java.io.File;

public class MyClassIO {
  String outFileName = "data.txt";
  ArrayList <CA> stuff = new ArrayList <> ();
 
  MyClassIO (int m) {
//     writeStuff (m);
    readStuff ();
  } // end int constructor
 
  void readStuff () {
    try {
      stuff = new ArrayList <> ();
      Scanner sc = new Scanner (new File(outFileName));
      while (sc.hasNextLine()) {
        Scanner s = new Scanner (sc.nextLine());
        if (! s.hasNext()) continue; // nothing on line
        switch (s.next()) {
          case "a": stuff.add (new CA (s)); break;
          case "b": stuff.add (new CB (s)); break;
          case "c": stuff.add (new CC (s)); break;
          case "d": stuff.add (new CD (s)); break;
          case "e": stuff.add (new CE (s)); break;
          case "f": stuff.add (new CF (s)); break;
          case "g": stuff.add (new CG (s)); break;
        } // end switch
      } // end for each line in sc
    } catch (Exception e) {
      System.out.println ("Exception:\n" + e);
    } // end try/catch
   
    stuff.sort ((a,b) -> (a.n).compareTo (b.n));
   
    System.out.println ("--- Read file ----");
    for (CA ca: stuff) System.out.println ("" + ca);
 
} // end method readStuff
 
  void writeStuff (int m) {
    for (int i = 0; i < m; i++) {
      stuff.add (CA.getRandomObject ());
    } // end creating m random objects children of CA

    System.out.println ("stuff:");
    for (CA ca: stuff)
      System.out.println ("" + ca);
     
    try {
      PrintStream ps = new PrintStream (outFileName);
      for (CA ca: stuff) ps.println ("" + ca);
    } catch (Exception e) {
      System.out.println ("Exception:\n" + e);
    } // end try/catch
   
    stuff.sort ((a,b) -> (a.n).compareTo(b.n));
   
    System.out.println ("---- SORTED stuff:");
    for (CA ca: stuff)
      System.out.println ("" + ca);
 
} // end method writeStuff
 
  public static void main (String args []) {
    MyClassIO cio = new MyClassIO (20);
    System.out.println ("---- Bye ----");
 
} // end main
} // end class MyClassIO

class CA {
  static Random rn = new Random ();
  int id;
  String n;
 
  static CA getRandomObject () {
    CA x = null;
    switch (rn.nextInt (7)) {
      case 0: x = new CA (); break;
      case 1: x = new CB (); break;
      case 2: x = new CC (); break;
      case 3: x = new CD (); break;
      case 4: x = new CE (); break;
      case 5: x = new CF (); break;
      case 6: x = new CG (); break;
    } // end switch
    return x;
  } // end no-parameter constructor
 
  static String randomString (int m) {
    String st = "";
    st += (char)(rn.nextInt (26) + 'A');
    for (int i = 1; i < m; i++)
      st += (char) ('a' + rn.nextInt (26));
    return st;
 
} // end method randomString
 
  public CA (Scanner s) {
    n = "a";
    id = s.nextInt ();
 
} // end constructor
 
  public CA () {
    id = rn.nextInt (1000);
    n = "a";
 
} // end constructor
 
 
public String toString () {
    String st = "";
    st += String.format ("%s %5d", n, id);
    return st;
 
} // end toString
} // end class CA

class CB extends CA {
  double da;
  int    za;
 
  public CB (Scanner s) {
    super (s);
    n = "b";
    za = s.nextInt ();
    da = s.nextDouble ();
 
} // end constructor

  public CB () {
    // super () is default
    n = "b";
    da = rn.nextDouble () * 10000;
    za = rn.nextInt (2000);
 
} // end constructor
 
 
public String toString () {
    String st = super.toString ();
    st += String.format ("%5d %10.3f", za, da);
    return st;
 
} // end toString
} // end class CB

class CC extends CB {
  int zb;
  boolean bb;
 
  public CC (Scanner s) {
    super (s);
    n = "c";
    zb = s.nextInt ();
    bb = s.nextBoolean ();
 
} // end constructor

  public CC () {
    // super () is default
    n = "c";
    zb = rn.nextInt (300);
    bb = (rn.nextInt (100) > 50)?true:false;
 
} // end constructor
 
 
public String toString () {
    String st = super.toString ();
    st += String.format ("%5d %b", zb, bb);
    return st;
 
} // end toString
} // end class CC

class CD extends CB {
  int zd;
  boolean bd;
  double dd;
  String sd;
 
  public CD (Scanner s) {
    super (s);
    n = "d";
    zd = s.nextInt ();
    bd = s.nextBoolean ();
    dd = s.nextDouble ();
    sd = s.next ();
 
} // end constructor

  public CD () {
    // super () is default
    n = "d";
    sd = randomString (10);
    zd = rn.nextInt (150);
    bd = (rn.nextInt (100) > 25)?true:false;
    dd = rn.nextDouble () * 500;
 
} // end constructor
 
 
public String toString () {
    String st = super.toString ();
    st += String.format ("%4d %6b %10.5f %s", zd, bd, dd, sd);
    return st;
 
} // end toString
} // end class CD

class CE extends CA {
  static final int MAX = 6;
  ArrayList <Double> aLe = new ArrayList <> ();
 
  public CE (Scanner s) {
    super (s);
    n = "e";
    while (s.hasNextDouble ())
      aLe.add (s.nextDouble());
    s.next (); // ":"
 
} // end constructor

  public CE () {
    // super () is default
    n = "e";
    for (int i = 0; i < MAX; i++)
      aLe.add (rn.nextDouble () * 37);
 
} // end constructor
 
 
public String toString () {
    String st = super.toString ();
    for (Double d: aLe)
      st += String.format ("%10.5f", d);
    st += " : ";
    return st;
 
} // end toString
} // end class CE

class CF extends CE {
  ArrayList <String> aLf = new ArrayList <> ();
 
  public CF (Scanner s) {
    super (s);
    n = "f";
    while (s.hasNext()) {
      String st = s.next ().trim();
      if (st.equals ("*")) break;
      aLf.add (st);
    } // end while
 
} // end constructor

  public CF () {
    // super () is default
    n = "f";
    for (int i = 0; i < MAX; i++)
      aLf.add (randomString (3));
 
} // end constructor
 
 
public String toString () {
    String st = super.toString ();
    for (String s: aLf)
      st += s + " ";
    st += "* ";
    return st;
 
} // end toString
} // end class CF

class CG extends CE {
  int [] zg = new int [10];
 
  public CG (Scanner s) {
    super (s);
    n = "g";
    for (int i = 0; s.hasNextInt(); i++)
      zg [i] = s.nextInt ();
 
} // end constructor

  public CG () {
    // super () is default
    n = "g";
    for (int i = 0; i < zg.length; i++)
      zg [i] = rn.nextInt (400);
 
} // end constructor
 
  public String toString () {
    String st = super.toString ();
    for (int k: zg)
      st += String.format ("%5d", k);
    return st;
  } // end toString
} // end class CG


ND.