if (iStream.close()
I am stuck on how to use iStream to print

import java.io.*;
import java.util.*;
public class BucketSort {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in); // This is a scanner for the keyboard

    System.out.print("Enter a filename: ");
    String fname=sc.nextLine();  // This is the name of the input file
    BufferedReader iStream=null; // This represents the input file itself
    Scanner fin;  // This is a scanner for the input file
    try{// The following line opens the file.  It must be in a try block
        iStream = new BufferedReader(new FileReader(fname));
    }
    // catch blocks will deal with possible exceptions
    catch (FileNotFoundException e)
       {System.out.println("Did not find "+fname);}
    catch (IOException e) {}
    // This instantiates the Scanner for the input file.
    fin = new Scanner(iStream);
    int num = fin.nextInt();  // This would input the first int in the input file

    sc.nextLine();  // clear the input buffer
    String[] name = new String[num]; // declare and instantiate an array of num strings

    int[] count = new int[num]; // declare and instantiate an array of num ints

    for (int i=0; i<num; i++)
         name[i] = fin.nextLine();  // input a string for each cell of name

    while (sc.hasNext()){  // until the end of the file is reached
        int vote = sc.nextInt();  // input another int from the file & call it vote

        // process the vote here

    }
    if (iStream.close() {
    // print out the results here.
		System.out.print("Enter a fname: ");
		System.out.print("Enter a fname: ");
		System.out.print("Enter a fname: ");
		System.out.print("Enter a fname: ");
		System.out.print("Enter a fname: ");
		System.out.print("Enter your vote (0-4): ");
    }
}
}

Dani AI

Generated

The immediate problems are the if (iStream.close() line and mixing up the two Scanners. As noted, an input stream is not something you "print to." As pointed out, the if line is syntactically wrong and conceptually wrong: close() returns void (and can throw IOException), so it cannot be used as a boolean condition. The code also creates the file Scanner even if opening the file failed, and it uses sc (keyboard) in the vote loop instead of the file Scanner (fin), which will never read the file data.

Fixes and rules of thumb:

  • Open the file once and read from that Scanner (or BufferedReader). Do not mix the keyboard Scanner and the file Scanner for the same input stream.
  • Check hasNextInt() or hasNextLine() on the file Scanner before calling nextInt()/nextLine().
  • After reading an integer header, consume the rest of that line on the same Scanner (e.g., in.nextLine()), then read the following name lines.
  • Validate votes before indexing the counts array (e.g., skip or report out-of-range votes).
  • Use try-with-resources (Java 7+) so the stream is closed automatically and you avoid calling close() inside an if.

Example pattern (concise, corrected):

try (Scanner kb = new Scanner(System.in)) {
  System.out.print("Enter filename: ");
  String fname = kb.nextLine().trim();
  try (Scanner in = new Scanner(new java.io.File(fname))) {
    if (!in.hasNextInt()) return;
    int n = in.nextInt(); in.nextLine();
    String[] names = new String[n];
    int[] counts = new int[n];
    for (int i = 0; i < n && in.hasNextLine(); i++) names[i] = in.nextLine();
    while (in.hasNextInt()) {
      int v = in.nextInt();
      if (v >= 0 && v < n) counts[v]++;
    }
    for (int i = 0; i < n; i++) System.out.println(names[i] + ": " + counts[i]);
  } catch (java.io.FileNotFoundException e) {
    System.out.println("File not found: " + fname);
  }
}

Troubleshooting: ensure the input file format matches expectations (first line = count, then exactly that many name lines, then integer votes). Avoid empty catch blocks (they hide the real error).

Recommended Answers

All 2 Replies

It's an input stream, you can't print to it.

if (iStream.close() {
    // print out the results here.
        System.out.print("Enter a fname: ");
        System.out.print("Enter a fname: ");
        System.out.print("Enter a fname: ");
        System.out.print("Enter a fname: ");
        System.out.print("Enter a fname: ");
        System.out.print("Enter your vote (0-4): ");
    }
}

First of all you have a syntax error because there is no closing paren to match the first opening one. Second of all that piece of code just makes no sense.

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.