JAVA:将我的班级方法连接到我的主要角色

时间:2017-02-17 01:42:44

标签: java

我是java新手,试图将直方图类(实现直方图界面)中的方法用于主方法,该方法读取文件并使用每个整数的第一个数字来创建直方图。

我的直方图类看起来像这样

    public class Histogram implements HistogramInterface
{
protected int min, max;
protected int[] counts;

public Histogram(int min, int max) 
{
  this.min = min;
  this.max = max;
  counts = new int[max - min + 1];
}

public Histogram(int max) 
{
  this.min = 1;
  this.max = max;
  counts = new int[max - min + 1];
}

public void submit(int i) throws HistogramOutOfBoundsException
{
  if ((min <= i) && (i <= max))
     counts[i-min] += 1;
  else
  {
     String message = "\n*******\n";
     message += "Submitted value " + i + " is outside range [";
     message += min + "," + max + "]" + " of Histogram.\n";
     message += "*******\n";
     throw new HistogramOutOfBoundsException(message);
  }
}

public String toString() 
{
  int weight;    // value of a single *
  int num;       // number of *'s to print

  // calculate weight
  int highIndex = 0;
  for (int i = 1; i < counts.length; i++)
    if (counts[highIndex] < counts[i])
      highIndex = i;

  weight = ((counts[highIndex] - 1) / MAXWIDTH) + 1;  

  // create "header"
  String histoString ="\n";

  if (weight != 1) 
     histoString += "* = approximately " + weight + " occurrences\n\n";

  // create histogram
  for (int i = 0; i <= max - min; i++)
  {
     histoString += String.format("%4d",(i + min)) + ": ";  
     if (counts[i] == 0)
       num = 0;
     else 
       num = ((counts[i] - 1) / weight ) + 1;
     for (int j = 0; j < num; j++)
        histoString += "*";
     histoString += "\n";
  }

  return histoString;
 }
 }

我正在努力的班级读者

import java.util.*;
import java.io.*;
public class Reader {
public static void main(String[] args) {

    int min = 1;
    int max = 9;
    Histogram x = new Histogram(min, max);

    int firstNum;
    int digits;
    int num = 0;
    int weight = 20;

    Scanner scan;
    File file = new File("Data.txt");

    try
    {
    scan = new Scanner(file);
    while(scan.hasNextLine()){
        digits = scan.nextInt();
        firstNum = Integer.parseInt(Integer.toString(digits).substring(0,1));
        digits++;
    }
    }
    catch(Exception e)
    {

    }


 }
 }

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找类似的东西:

s3DownloadStream('my-file').pipe(transformStream).pipe(backToS3Stream)