-3

I have been working for a few hours on trying to fix this code, and have visited countless questions on this site.

I apologize for the code dump, but I am not sure what is going wrong with the code.

The error is based in the reading of the file, but I included all my code to be safe.

package ch7;
import java.util.Scanner;
import java.io.*;    
public class DistributionChart 
{   
  public static void main (String[] args) throws IOException
   {
      int size = 10;
      int[] ranges = new int[size];  // each entry represents a range of values          
      getData(ranges);   //pass the entire array into the method          
      displayChart(ranges);          
      getDataFromFile(ranges);          
      displayChartToFile(ranges);          
      System.out.println("\nSee you later!!");          
   } //end of main       
   public static void getData(int[] someArray)
   {           
       Scanner scan = new Scanner (System.in);     
       System.out.println ("Enter a series of numbers between 1 and 100. Separate each number with a space.");
       System.out.println ("Signal the end by entering a number outside " +
             "of that range and then press enter.");
       System.out.print("Go: ");           
       //reads an arbitrary number of integers that are in the range 1 to 100 inclusive
       //for each integer read in, determine which range it is in and increment the corresponding element in the array         
       //your code goes here
          int values = 0;             
          values = scan.nextInt();            
          while(values > 0 && values < 101)
          {
              if(values >= 1 && values <= 10)
              {                   
                  someArray[0]++;
              }               
              else if (values >= 11 && values <= 20)
              {                   
                   someArray[1] ++;
              }
              else if (values >= 21 && values <= 30)
              {                   
                   someArray[2]++;
              }
              else if(values >= 31 && values <= 40)
              {                   
                  someArray[3]++;
              }               
              else if(values >= 41 && values <= 50)
              {
                  someArray[4]++;
              }               
              else if(values >= 51 && values <= 60)
              {
                someArray[5]++;                 
              }               
              else if(values >= 61 && values <= 70)
              {
                  someArray[6]++;
              }               
              else if(values >= 71 && values <= 80)
              {
                  someArray[7]++;
              }               
              else if(values >= 81 && values <= 90)
              {
                   someArray[8]++;
              }               
              else if(values >= 91 && values <= 100)
              {
                   someArray[9]++;
              }               
              values = scan.nextInt();                
          }         
        scan.close();  
   }//end of getData       
   public static void displayChart(int[] someArray)
   {
       System.out.println("Distribution chart by Royce T.");
       System.out.println("================================");   
       //Print histogram.
         for(int i = 0; i < someArray.length; i++)
       {    
           System.out.print( ((i * 10 + 1)) + "-" + (((i *10 +1)) + 9) + " \t" + "|");             
           for(int y = 0; y < someArray[i]; y++)
           {
               System.out.print("*");
           }
              System.out.println();
          }

   } //end of displayChart    
   public static void getDataFromFile(int[] someArray) throws IOException
   {
       Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
       int values = 0;
       while(inFile.hasNext())
       {               
              values = inFile.nextInt();                  
              while(values > 0 && values < 101)
              {
                  if(values >= 1 && values <= 10)
                  {                       
                      someArray[0]++;
                  }                   
                  else if (values >= 11 && values <= 20)
                  {                       
                       someArray[1] ++;
                  }
                  else if (values >= 21 && values <= 30)
                  {                       
                       someArray[2]++;
                  }
                  else if(values >= 31 && values <= 40)
                  {                       
                      someArray[3]++;
                  }                   
                  else if(values >= 41 && values <= 50)
                  {
                      someArray[4]++;
                  }                   
                  else if(values >= 51 && values <= 60)
                  {
                    someArray[5]++;
                  }
                  else if(values >= 61 && values <= 70)
                  {
                      someArray[6]++;
                  }                   
                  else if(values >= 71 && values <= 80)
                  {
                      someArray[7]++;
                  }                   
                  else if(values >= 81 && values <= 90)
                  {
                       someArray[8]++;
                  }                   
                  else if(values >= 91 && values <= 100)
                  {
                       someArray[9]++;
                  }                   
                  values = inFile.nextInt();                      
              }             
            inFile.close();
       }
   }
       public static void displayChartToFile(int[] someArray) throws IOException
       {
           getDataFromFile(someArray);
           PrintWriter outFile = new PrintWriter( "C:/Users/RTmag/workspaceRATI//CSC110/outputFile.txt");        
           //Print chart title with your name
           outFile.println("Distribution chart by Royce T.");
           outFile.println("================================");
           //Print histogram.
           for(int i = 0; i < someArray.length; i++)
           {    
               outFile.print( ((i * 10 + 1)) + "-" + (((i *10 +1)) + 9) + " \t" + "|");                
               for(int x = 0; x <someArray[i]; x++)
               {
                 outFile.print("*");
               }                   
               outFile.println();                  
           }
           outFile.close();  
       }
   }    
// end of DistributionChart tester class

I am attempting to have the displayChartToFile method write to a file named outputFile.txt that is located in the CSC110 folder. This folder also contains my bin and src folders.

Whenever I run my code, the first two methods properly display after I input.

example of output with errors:

Enter a series of numbers between 1 and 100. Separate each number with a space.
Signal the end by entering a number outside of that range and then press enter.
Go: 55 66 555
Distribution chart by Royce T.
================================
1-10    |
11-20   |
21-30   |
31-40   |
41-50   |
51-60   |*
61-70   |*
71-80   |
81-90   |
91-100  |
Exception in thread "main" java.io.FileNotFoundException: C:\Users\RTmag\workspaceRATI\CSC110\inputFile.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at ch7.DistributionChart.getDataFromFile(DistributionChart.java:143)
    at ch7.DistributionChart.main(DistributionChart.java:28)

Line 28 is:

getDataFromFile(ranges);

Line 143 is :

Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));

The text inside inputFile.txt is

75 49 62 35 18 97 62 54 83 61 44 29 98 75 14

outputFile.txt is empty.

Any help is greatly appreciated. Thanks!

And if I don't have all the information you may need to help me just let me know!

EDIT:

I fixed the file location, but now it is returning a new set of errors.

 Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at ch7.DistributionChart.getDataFromFile(DistributionChart.java:207)
    at ch7.DistributionChart.main(DistributionChart.java:30)
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
Royce T
  • 11
  • 3
  • 1
    Make sure that the file exists at the specified location ... also, not sure what the `//` is for – MadProgrammer Apr 12 '17 at 04:15
  • @MadProgrammer I removed the unnecessary slash before CSC110 and made sure the files were there. The code still gives the same errors. – Royce T Apr 12 '17 at 04:18
  • Try adding `System.out.println(new File("C:/Users/RTmag/workspaceRATI/CSC110/inputFile.txt").exists());` if this prints `false`, then the file is not where you think it is – MadProgrammer Apr 12 '17 at 04:19
  • @MadProgrammer Okay, so it returned false and I fixed that, but now it is returning a different error. The new error is added in original post. – Royce T Apr 12 '17 at 04:28
  • Okay, maybe you should consider closing/deleting this question (now it's solved) and post a new question with the new error and code – MadProgrammer Apr 12 '17 at 04:30
  • @MadProgrammer I would do that, but it won't let me ask a new question. Would it be frowned upon to keep up this post but use edits to try and solve the new error? – Royce T Apr 12 '17 at 04:32
  • Yes, I would be considered a bad idea, as you're changing the question, the current answer is no longer applicable and could be downvoted as irrelevant – MadProgrammer Apr 12 '17 at 04:34
  • How many lines are you expecting there to be in the input file? – MadProgrammer Apr 12 '17 at 04:38
  • @MadProgrammer one line in the input file. – Royce T Apr 12 '17 at 04:41

2 Answers2

1

To me, this looks wrong...

int values = 0;
while (inFile.hasNext()) {

    values = inFile.nextInt();

    while (values > 0 && values < 101) {
        //...
        values = inFile.nextInt();
    }
}
  • You check to see if the file has any more lines, good
  • You get the next int value, good
  • You loop while the values are between 1..100, okay, but what happens if you reach the end of the input?
  • You read the next int ... ah, not sure about that.

Given your input of 75 49 62 35 18 97 62 54 83 61 44 29 98 75 14 you could simply do something like...

int values = 0;
while (inFile.hasNext()) {
    values = inFile.nextInt();
    //...
}

But if the input file might have multiple lines, you could do...

int values = 0;
while (inFile.hasNext()) {
    String nextLine = inFile.nextLine();
    Scanner line = new Scanner(nextLine);
    while (line.hasNextInt()) {
        values = line.nextInt();
        //...
    }
}

(You could do this even if it had only one line).

The basic problem is, you're reading to the end of the file and Scanner has run out of data to read

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

change this line

Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));

to either

Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI/CSC110/inputFile.txt"));

or

Scanner inFile = new Scanner(new FileReader("C:\\Users\\RTmag\\workspaceRATI\\CSC110\\inputFile.txt"));
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74