简体   繁体   中英

How can I print an array and a method from another class

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Histogram
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Row[] numbers = {
           new Row("1  -  10"), new Row("11 -  20"), new Row("21 -  30")                 
           new Row("31 -  40"), new Row("41 -  50"), new Row("51 -  60")
           new Row("61 -  70"), new Row("71 -  80"), new Row("81 -  90"),
           new Row("91 - 100")
                };
for(Row number : numbers)
  System.out.print(number);   
Counter section = new Counter();
section.StarCounter();

}

it prints out something like this:

1  - 10|
11 - 20|
21 - 30|
*****************
*********
***
ect.

I would like it to print out something like:

1  - 10|*****************
11 - 20|*********
21 - 30|***
ect.

thanks in advance for any pointers, I'm probably just overlooking the obvious.

Counter class:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Counter
{
  public void StarCounter() throws FileNotFoundException
  {
    File file = new File("alotofnumbers.txt");
    Scanner scan = new Scanner(file);
    int[] integers = new int[1000];
    int start = 0;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int count6 = 0;
    int count7 = 0;
    int count8 = 0;
    int count9 = 0;
    int count10 = 0;

    while (scan.hasNextInt())
    {
  integers[start++] = scan.nextInt();
}

for (int input : integers)
{
  if(input <= 10)
  {
    count1++;  
  }
  else if(input > 10 && input <= 20)
  {
    count2++;
  }
  else if(input > 20 && input <= 30)
  {
    count3++;
  }
  else if (input > 30 && input <= 40)
  {
    count4++;
  }
  else if (input > 40 && input <= 50)
  {
    count5++;
  }
  else if (input > 50 && input <= 60)
  {
    count6++;
  }
  else if (input > 60 && input <= 70)
  {
    count7++;
  }
  else if (input > 70 && input <= 80)
  {
    count8++;
  }
  else if (input > 80 && input <= 90)
  {
    count9++;
  }
  else if (input > 90 && input <= 100)
  {
    count10++;
  }
} 
  double counted1 = count1 / 2.7;
  double counted2 = count2 / 2.7;
  double counted3 = count3 / 2.7;
  double counted4 = count4 / 2.7;
  double counted5 = count5 / 2.7;
  double counted6 = count6 / 2.7;
  double counted7 = count7 / 2.7;
  double counted8 = count8 / 2.7;
  double counted9 = count9 / 2.7;
  double counted10 = count10 / 2.7;

  for(int star = 0; star <= counted1  ; star++)
  {
   System.out.print("*");

  }
  System.out.println();


  for(int star = 0; star <= counted2 ; star++)
  {
    System.out.print("*");
  }

   System.out.println();
   for(int star = 0; star <= counted3 ; star++)
   {
    System.out.print("*");
   }

     System.out.println();

   for(int star = 0; star <= counted4 ; star++)
   {
     System.out.print("*");
   }

    System.out.println(); 
    for(int star = 0; star <= counted5 ; star++)
    {
     System.out.print("*");
    }

      System.out.println();

    for(int star = 0; star <= counted6 ; star++)
    {
      System.out.print("*");
    }

     System.out.println();
     for(int star = 0; star <= counted7 ; star++)
     {
      System.out.print("*");
     }

       System.out.println();

     for(int star = 0; star <= counted8 ; star++)
     {
       System.out.print("*");
     }

      System.out.println();
      for(int star = 0; star <= counted9 ; star++)
      {
       System.out.print("*");
      }

        System.out.println();

      for(int star = 0; star <= counted10 ; star++)
      {
        System.out.print("*");
      }

       System.out.println();     


 }
 }

Apparently my post has to much code and I don't really have much more info to give so it wants me to type something random up. public class Row { private String rows;

public Row(String colums)
{
rows = colums ;
}

public String toString()
{
Counter section = new Counter();
return rows + "|" + "\n";
}

public void setRow(String colums)
{
rows = colums;
}

public String getRow()
{
return rows;
}

}

I'm not exactly sure what your question is as the title doesn't seem to relate that closely to the question so I'm going to assume it's "is there are better approach for creating a histogram?".

You can't take the approach of printing all the titles (rows) and then printing the stars. They have to be done at the same time.

Because integer division in Java automatically rounds down, all of your counting code can be significantly reduced:

int[] counts = new int[GROUPS];
for (int i: inputs)
    counts[Math.min(GROUPS - 1, (i + 1) / 10)]++;

The GROUPS will need to be defined as a static class constant.

Then printing the counts can be simplified to:

for (int i = 0; i < GROUPS; i++) {
    System.out.print(String.format("%2d - %2d]", i * 10 + 1, i * 10 + 10));
    for (int s = 0; s < counts[i]; s++)
        System.out.print("*");
    System.out.println();
}

In fact this can be further simplified using Java 8 streams but that's probably more information than you are after at the moment.

I don't see any opening or closing brackets for the for loop. section.StarCounter should be in the same loop.

Try this:

for(Row number : numbers){
System.out.print(number);   
Counter section = new Counter();
section.StarCounter();
System.out.println();
}

Notice the for loop has braces and the loop ends with a new line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM