6 Replies - 1950 Views - Last Post: 20 November 2011 - 10:18 PM Rate Topic: -----

#1 Fushigisou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-November 11

Reading data from a .txt file

Posted 20 November 2011 - 07:58 PM

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

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 | "};
		              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.


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)




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!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading data from a .txt file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading data from a .txt file

Posted 20 November 2011 - 08:05 PM

hasNext() and hasNextInt() are not the same

if you test with hasNext() read using next()
if you test with hasNextInt() read using nextInt()

and close() your Scanner when you are done with them
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,773
  • Joined: 27-December 08

Re: Reading data from a .txt file

Posted 20 November 2011 - 08:14 PM

This section makes no sense. First, the semi-colon closes the while loop. The braces are a separate block not associated with the loop. The array is local to this block, and cannot be accessed outside of it. Hence, the compilation error on line 85.
while (n < 0);
{
      int[] numbers = new int[n];
}


Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading data from a .txt file

Posted 20 November 2011 - 09:10 PM

Oups !!! Missed those 2

Mosty must be right :)
Was This Post Helpful? 0
  • +
  • -

#5 Fushigisou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-November 11

Re: Reading data from a .txt file

Posted 20 November 2011 - 10:09 PM

I looked over at those problems you mentioned and I managed to change things around:


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)
  {
    DataInputStream stdin = new DataInputStream(stdin);
    BufferedReader d = new BufferedReader(new InputStreamReader(stdin));
    final int SIZE = 10;
    final int MIN = 1;
    final int MAX = 100;
    final int scaleFactor = 60;
    int[] counters = new int[SIZE];
    int[] list = new int[MAX];
    
    for(int i = 0; i < list.length; i++)
    {
      list[i] = 0;
    }
    
    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;
    
  int value = Integer.parseInt(stdin.readInt());
  
  // Divide by scaleFactor to determine the range to increment
  
  list[(value - 1) / scaleFactor] = list[(value - 1) / scaleFactor] + 1;
  
  // Printing Histogram
  
  System.out.println("Here is the Histogram: ");
  for (int i = 0; i < list.length; i++)
  {
    System.out.print("    " + (i * scaleFactor + 1) + " - " + (i + 1) * scaleFactor + "\t| ");
  }
  int i;
  for (int j = 0; j < list[i]; j++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  }
    }




How's the code look? I've looked at the API and managed to find a command DataInputStream, so i was wondering if i implemented it right. The incrementation is included since the .txt file im scanning from contains quite alot of numbers in the ranges 1 - 10, 11 - 20, etc.

For the hasNext and hasNextInt, im not finding that error, is it because Java displays it as a non-error?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,773
  • Joined: 27-December 08

Re: Reading data from a .txt file

Posted 20 November 2011 - 10:17 PM

Does your code work as expected? What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading data from a .txt file

Posted 20 November 2011 - 10:18 PM

I already said that you have to be consistant with what you test and what your read
hasNext() returns if there is a String nextInt() returns a int
If the file has "abcd" hasNext() will return true and nextInt() wiull generate an exception

And if you have the number of int, why not just using a for() loop ?
And close() your Scanner


  public int[] readIntArrayFromFile(String fileName) throws Exception
  {
    Scanner fileScan  = new Scanner(new File(fileName));
    int counter = 0;
    while(fileScan.hasNextInt())  // it is int you want to read
    {
      fileScan.nextInt();      // read an int
      counter++;
    }
    fileScan.close();   // done with it

    int[] iArray = new int[counter];
    
    Scanner fScan  = new Scanner(new File(fileName));
    for(int i = 0; i < counter; ++i)
      iArray[i] = fScan.nextInt();

    fScan.close();

    return iArray;
  }


This post has been edited by pbl: 20 November 2011 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1