Exemplo n.º 1
0
  public static int CountTotalDataPointsInFile(String fileName) // length of
        // array to be
        // filled
      {
    int numExamples;
    int numFeatures;
    int predictedValuesCount = 0;
    int valuesCount = 0;
    TextFileInput tfi = new TextFileInput(fileName);
    String line = tfi.readLine();
    // create a new scanner with the specified String Object
    Scanner scanner = new Scanner(line);
    numExamples = scanner.nextInt();
    numFeatures = scanner.nextInt();
    // close the scanner
    scanner.close();
    predictedValuesCount = numExamples * numFeatures;

    while (line != null) {
      line = tfi.readLine();
      if (line != null) {
        // create a new scanner with the specified String Object
        scanner = new Scanner(line);

        // use US locale to be able to identify doubles in the string
        scanner.useLocale(Locale.US);

        // find the next double token and print it
        // loop for the whole scanner
        while (scanner.hasNext()) {

          // if the next is a double, print found and the double
          if (scanner.hasNextDouble()) {
            System.out.println("Found :" + scanner.nextDouble());
            valuesCount++;
          }

          // if the next is an int, print found and the int
          if (scanner.hasNextInt()) {
            System.out.println("Found :" + scanner.nextInt() * 1.0);
            valuesCount++;
          }
        }
        // close the scanner
        scanner.close();
      }
    }
    if (predictedValuesCount != valuesCount) {
      System.out.println(
          "Error parsing file. Expected: " + predictedValuesCount + ". Parsed: " + valuesCount);
    }
    return valuesCount;
  }
Exemplo n.º 2
0
  /**
   * opens the chosen file, reads in the file, and prints out a receipt
   *
   * @param chosenFile
   */
  private void readSource(File chosenFile) {
    String chosenFileName = chosenFile.getName();
    TextFileInput inFile = new TextFileInput(chosenFileName);
    Container myContentPane = jframe.getContentPane();
    // chosenFile TextArea myTextArea = new TextArea();
    myContentPane.add(myTextArea);

    int count = 0;
    float priceTotal = 0.0f;
    Database db = new Database("database2.txt");
    String[] transaction = new String[100];
    String line = inFile.readLine();
    DecimalFormat df = new DecimalFormat("#00.00");
    while (line != null) {
      StringTokenizer tokenized = new StringTokenizer(line, ",");
      String code = tokenized.nextToken();
      float weight = Float.parseFloat(tokenized.nextToken());
      String name;
      float price;

      try {
        name = db.getName(code);

      } catch (ItemNotFoundException e) {
        name = JOptionPane.showInputDialog(null, "Item " + code + " not found. Enter Name: ");
      }
      try {
        price = db.getPrice(code);
      } catch (ItemNotFoundException e) {
        price =
            Float.valueOf(
                JOptionPane.showInputDialog(
                    null, "Price for " + name + " not found. Enter price: "));
      }
      float itemTotal = weight * price;
      priceTotal += itemTotal;
      transaction[count] =
          name + "\t" + price + "\t" + df.format(weight) + "\t $" + df.format(itemTotal);
      count++;
      line = inFile.readLine();
    } // while
    myTextArea.setText("ITEM: \t PRICE\\LB: \t POUNDS: \t TOTAL:");
    myTextArea.append("\n");
    for (int i = 0; i < count; i++) {
      myTextArea.append(transaction[i]);
      myTextArea.append("\n");
    }
    myTextArea.append("\t\t   TOTAL: $" + df.format(priceTotal));
    jframe.setVisible(true);
  } // openFile
Exemplo n.º 3
0
 /**
  * reads from database file and creates a new fruit or vegetable
  *
  * @param filename
  */
 public void readFromFile(String filename) {
   TextFileInput input = new TextFileInput(filename);
   String line = input.readLine();
   while (line != null) {
     StringTokenizer token = new StringTokenizer(line, ",");
     String type = token.nextToken();
     String code = token.nextToken();
     String name = token.nextToken();
     float price = Float.parseFloat(token.nextToken());
     ProduceItem item;
     if (type.equals("F")) {
       item = new Fruit(code, name, price);
     } else item = new Vegetable(code, name, price);
     items.append(item);
     line = input.readLine();
   } // while
 } // readFromFile
Exemplo n.º 4
0
  public double[] readFile(String fileName, int TotalDataPoints) // Length of
        // array to
        // be filled
      {
    double[] alldatapoints = new double[TotalDataPoints];

    TextFileInput tfi = new TextFileInput(fileName);
    String line = tfi.readLine(); // first line of file
    int count = 0;
    while (line != null) {
      line = tfi.readLine(); // second line of file (given first
      // iteration)
      if (line != null) {
        // create a new scanner with the specified String Object
        Scanner scanner = new Scanner(line);

        // use US locale to be able to identify doubles in the string
        scanner.useLocale(Locale.US);

        // find the next double token and print it
        // loop for the whole scanner
        while (scanner.hasNext()) {

          // if the next is a double, add double to data set
          if (scanner.hasNextDouble()) {
            alldatapoints[count] = scanner.nextDouble() * 1.0;
            count++;
          }

          // if the next is an int, parse to double and add to data
          // set
          if (scanner.hasNextInt()) {
            alldatapoints[count] = scanner.nextInt() * 1.0;
            count++;
          }
        }
        // close the scanner
        scanner.close();
      }
    }
    System.out.println("Size of data set: " + alldatapoints.length);
    return alldatapoints;
  } // Read File