Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Creating a program that reads from a file

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a program that reads from a file

    Im working on a problem today and I getting errors for some reason.

    Problem statement:
     
    Design and implement an application that creates a histogram,
    which allows you to visually inspect the frequency distribution
    of a set of values.  The program should read in an arbitrary
    number of integers that are in the range 1 to 100 inclusive;
    then produce a chart similar to the one below that indicates
    how many input values fell in the range 1-10, 11-20, and so on.
    Print one asterisk for each value entered.

    Here's the catch, making that problem is okay for me, but what if i wanted it to create it from reading values from a txt file?

    This is my final code:

     
    import java.util.*;
    import java.io.*;
     
     
    public class Histogram 
    {
     
     
    	public void displayIntArray(int[] iAr)
    	{
    		for(int index = 0; index < iAr.length; index++)	
    		{
    			System.out.println(iAr[index]);
    		}
    	}
     
     
    	public int[] readIntArrayFromFile(String fileName) throws Exception
    	{
    		Scanner fileScan  = new Scanner(new File(fileName));
    		int counter = 0;
    		while(fileScan.hasNext())
    		{
    			fileScan.next();
    			counter++;
     
    		}
     
    		int[] iArray = new int[counter];
     
    		Scanner fScan  = new Scanner(new File(fileName));
    		counter = 0;
    		while(fScan.hasNext())
    		{
    			iArray[counter] = fScan.nextInt();
    			counter++;
    		}
     
    		return iArray;
     
     
     
     
     
    	}
     
    	public int[] initializeCounterArray(int[] inArray) 
    	{
    		final int SIZE = 10;
    		final int MIN = 1;
    		final int MAX = 100;
    		int[] counters = new int[SIZE];
    		int n, nextt, ix;
     
     
    		while (n < 0);
    		{
    		  int[] numbers = new int[n];
    		}
     
     
    		for (int index = 0; index < inArray.length; index++) 
    		{
    			int number = inArray[index];
    			if (number >= MIN && number <= MAX) {
    				int newIndex = (number - 1) / SIZE;
    				counters[newIndex]++;
    			}
     
    		}
     
    		return counters;
     
     
     
      // Creates the histogram values
     
      String[] stars = {" 1 - 10 |", "11 - 20 | ", "21 - 30 | ", "31 - 40 | ", "41 - 50 | ",
                     "51 - 60 | ", "61 - 70 | ", "71 - 80 | ", "81 - 90 | ", "91 - 100 | "};
     
      //10 strings for the different parameters
     
      for (ix = 0; ix < n; ix++) 
      {
          nextt = numbers[ix];
     
          if (nextt < 11) 
          {
              stars[0] += "*";
          } 
          else
          {
            if (nextt < 21) 
            {
              stars[1] += "*";
          } 
     
            else
            {
              if (nextt < 31) 
          {
              stars[2] += "*";
          }
          else
          {
            if (nextt < 41) 
          {
              stars[3] += "*";
          }
          else
          {
            if (nextt < 51) 
          {
              stars[4] += "*";
          } 
          else
          {
            if (nextt < 61) 
          {
              stars[5] += "*";
          } 
          else
          {
            if (nextt < 71) 
          {
              stars[6] += "*";
          } 
          else
          {
            if (nextt < 81) 
          {
              stars[7] += "*";
          } 
          else
          {
            if (nextt < 91) 
          {
              stars[8] += "*";
          } 
          else 
          {
              stars[9] += "*";
          }
      }
      for (ix = 0; ix < 10; ix++) {
          System.out.println(stars[ix]);
      }
     
    }
          }
     
          }
          }
          }
            }
          }
      }
    	}
    }

    I've got the driver class all good to go but just in case:
     
    public class Driver 
    {
    	public static void main(String[] args) throws Exception
    	{
    		int[] numbers, counters;
    		Histogram hist = new Histogram();
     
    		numbers = hist.readIntArrayFromFile("data-8.3.txt");
    		counters = hist.initializeCounterArray(numbers);
     
    		hist.displayIntArray(counters);
    	}
     
    }

    The only error im getting from this is the " nextt = numbers[ix]; " part of the code. ive tried implementing some commands at the top but nothing worked. But does everything look good? So if you feel that the code is not good, please feel free to make any required changes.

    I appreciate everyone taking their time out of their day to help me out, it really means alot

    Thank you!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a program that reads from a file

    Please copy and paste here the full text of the error messages you are getting.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a program that reads from a file

     
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	numbers cannot be resolved to a variable
     
    	at Histogram.initializeCounterArray(Histogram.java:85)
    	at Driver.main(Driver.java:10)

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Creating a program that reads from a file

    On line 85 of the Histogram class you are attempting to use the numbers variable which has either not been declared or it has been declared in the wrong scope.

    By the way what is the purpose of the following code? Maybe I should rephrase the question, what exactly is this code doing not what do you think it should be doing?
    while (n < 0);
    {
        int[] numbers = new int[n];
    }
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a program that reads from a file

    Im trying to create a histogram which reads values from a txt file and then displays how many of each corresponds in each interval.

    For:
     
    while (n < 0);
    {
        int[] numbers = new int[n];
    }

    How could i resolve this issue? could i just declare numbers a brand new int[]?

    Thanks!

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Creating a program that reads from a file

    Take a close look at that code. Problems are:
    1. You have placed a semi-colon after the while statement which makes the rest of the code outside the loop.
    If you fix that
    2. You have declared the array inside the loop and therefore it cannot be used outside the loop.
    3. You create a new array each time around the loop which means the previous array is thrown away. What possible reason would you need to do that?
    4. The value of n never changes therefore it might be an infinite loop or might never enter the loop depending upon the value of n.
    5. For the loop to enter the value of n must be negative. Inside the loop you make an array with a length of n. It is impossible to make an array with a negative length.
    Improving the world one idiot at a time!

Similar Threads

  1. Need help with creating Program
    By apescato in forum Java Theory & Questions
    Replies: 4
    Last Post: October 20th, 2011, 07:46 PM
  2. Static class that reads from file -> performance issues?
    By rbk in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 18th, 2011, 09:59 AM
  3. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  4. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM