V B
V B

Reputation: 61

Making histogram and stars are not output correctly

I am trying to make a histogram where the user inputs numbers from 1 - 100 and then the program sees how many numbers were in 1-10, 11-20, etc..

Currently if the input is 1 2 3 4 5 6 7 8 it would do 8 stars up to list 81-90, all of them having 8 stars. Obviously, this is not what I want but I am stumped on this.

Here is the code:

import java.util.*;

public class test5
{
public static void main (String [] args)
{
  int i = 0;
  int[] Array = new int[10];

  List<Integer> list = new ArrayList<Integer>();

  Scanner s = new Scanner(System.in);

  System.out.println ("Please enter any number of values (from 1 - 100 only) ending with -1 when done:");
  while(i != -1){            
     i = s.nextInt();
     if(i != -1) 
        list.add(i);
  }           

  for (int a = 0; a < list.size(); a++){
     int range1 = 1;
     int range2 = 10;
     for (int b = 0; b < list.size(); b++){
        if (list.get(b) >= range1 && list.get(b) <= range2)
           Array[a] += 1;
     }  
     range1 += 10;
     range2 += 10;  
  }

  for (int count = 0; count < 10; count++){
     System.out.print("| " + (count*10+1) + (count == 0 ? " ": "") + " - " + (count*10+10) + (count == 9 ? "" : " ")
     + "| \t");

     for(int h = 0; h < Array[count]; h++)  
        System.out.print("*");  

     System.out.println();  
  } 
}
} 

Upvotes: 0

Views: 2339

Answers (4)

Jaroslav Tulach
Jaroslav Tulach

Reputation: 528

Here is a live demo that allows user to specify numbers and shows ten histogram bars according to the number distribution: http://dew.apidesign.org/dew/#7510833

Method bars does the histogram logic.

Upvotes: 1

poiu2000
poiu2000

Reputation: 980

import java.util.*;

public class Test5
{
    public static void main (String [] args)
    {
        int i = 0;
        int[] nums = new int[10];

        List<Integer> list = new ArrayList<Integer>();

        Scanner s = new Scanner(System.in);

        System.out.println ("Please enter any number of values (from 1 - 100 only) ending with -1 when done:");
        while(i != -1){
           i = s.nextInt();
           if(i != -1)
              list.add(i);
        }

        for (int a = 0; a < list.size(); a++){
            nums[(list.get(a)-1) / 10]++;
        }

        for (int count = 0; count < 10; count++){
           System.out.print("| " + (count*10+1) + (count == 0 ? " ": "") + " - " + (count*10+10) + (count == 9 ? "" : " ") + "| \t");

           for(int h = 0; h < nums[count]; h++)
              System.out.print("*");

           System.out.println();
        }
    }
}

Test result:

$ java Test5
Please enter any number of values (from 1 - 100 only) ending with -1 when done:
1
2
3
44
66
12
13
100
16
-1
| 1  - 10 |     ***
| 11 - 20 |     ***
| 21 - 30 |
| 31 - 40 |
| 41 - 50 |     *
| 51 - 60 |
| 61 - 70 |     *
| 71 - 80 |
| 81 - 90 |
| 91 - 100|     *

Upvotes: 1

Mengjun
Mengjun

Reputation: 3197

Change your first for - loop.

In your first for-loop, range1 and range2 does not change. Since they are re-decalared and the range1 and range2 are not used after doing the modify:range1 += 10; range2 += 10;

In your code, Array (int[]), it store the number of range data, such as,

Array[0] stores the number of values in range | 1 - 10 |

Array[1] stores the number of values in range | 11 - 20 |

... ...

Array[9] stores the number of values in range | 90 - 100|

We can use a simple way to increace the number of values in a specified data range.

Take number 11 for example , we can use Array[11/10] +=1,this equals to Array[1] +=1;

Make sure the range1 and range2 are correct, OR follow the solution as I mentioned below:

Change

for (int a = 0; a < list.size(); a++){
     int range1 = 1;
     int range2 = 10;
     for (int b = 0; b < list.size(); b++){
       if (list.get(b) >= range1 && list.get(b) <= range2)
           Array[a] += 1;
     }  
     range1 += 10;
     range2 += 10;  
 }

To

 for (int a = 0; a < list.size(); a++){

  if(list.get(a) ==100)
  {
      Array[9] +=1; 
  }else
  {
      Array[list.get(a)/10] +=1; 
      System.out.println(Arrays.toString(Array));
  }

  }

With this change you can get the expected result:

such as,

Inout:

12
13
25
36
36
-1

Output in Console:

 | 1  - 10 |
 | 11 - 20 |    **
 | 21 - 30 |    *
 | 31 - 40 |    **
 | 41 - 50 |    
 | 51 - 60 |    
 | 61 - 70 |    
 | 71 - 80 |    
 | 81 - 90 |    
 | 91 - 100| 

Upvotes: 1

subash
subash

Reputation: 3140

try this ...

for (int count = 0; count < 10; count++) {
        int start = count * 10 + 1;
        int end = count * 10 + 10;
        System.out.print("| " + (count * 10 + 1) + (count == 0 ? " " : "") + " - " + (count * 10 + 10) + (count == 9 ? "" : " ") + "| \t");

        for (int h = 0; h < list.size(); h++)
            if (list.get(h) >= start && list.get(h) <= end)
                System.out.print("*");
        System.out.println();
    }

Upvotes: 1

Related Questions