I need to design a histogram that prints out an "" for certain intergers and the integers need to be read from a text file. I think I got the hole "" thing down, but the file reading isn't working not matter what I try so I could use a little help.
I simply called the file text.txt.

The full description of what I'm working on is:
"Write a Java program to input an unknown number of integers from a text file and produce a histogram, in your solution, print one “” for each number entered (if we were expecting tons of data, one might have used one “” to represent a bunch of values). Use an array to store the frequency counts. You have no need to store the actual data itself."

This is my code:

import java.util.Scanner; 
import java.io.*; 

public class file 
{ 
    public static void main(String [] args) throws IOException
    { 
        final int LIMIT = 9999; 
        String file;
        String num = "*";
        int string; 
        int[]store = new int [LIMIT];

        Scanner scan = new Scanner( new File("text.rtf"));  

        int file1 = file.hasNext();

        System.out.println("This program will read out a histogram from a file");




       for( string = 0; string < file1.hasNext; string ++)
       { 
       if (9<= string >= 0 )  
    store[0]++;
    else if (19<= string >= 10)  
    store[1] ++;
    else if (29<= string >= 20)  
    store[2] ++;
    else if (39<= string >= 30)  
    store[3] ++;
     else if (49<= string >= 40) 
    store[4] ++;
     else if (59<= string >= 50) 
    store[5] ++;
     else if (69<= string >= 60) 
    store[6] ++;
     else if (79<= string >= 70) 
    store[7] ++;
     else if (89<= string >= 80) 
    store[8] ++;
     else if (99<= string >= 90) 
    store[9] ++;


           file = scan.nextLine();

        System.out.println("*"); 


}
}
}

ok, that's not Java code. It's not even close to being Java code.
Here's something to get you started though, you'll have to think of how to get it to match your requirements but it at least works.

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test3 {
    public static void main(String... args) throws Exception {
        Map<Integer, Integer> values = new HashMap<>();
        Scanner scan = new Scanner( new File("d:/tmp/nums.txt"));
        System.out.println("This program will read out a histogram from a file");
        while(scan.hasNextInt()) {
            int i = scan.nextInt();
            if (values.containsKey(i)) {
                int v = values.get(i);
                values.put(i, ++v);
            } else {
                values.put(i, 1);
            }
        }
        for (Map.Entry<Integer, Integer> val: values.entrySet()) {
            System.out.print(val.getKey() + ": ");
            int num = val.getValue();
            for (int i=0;i<num;i++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

FYI: That little pattern on lines13-17 is so common that Java 8 Map has a new getOrDefault method to make it simpler.

values.put(i,  values.getOrDefault(i, 0) +1); 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.