I am having a lot of trouble getting this program to work properly. No compilation errors, but this is the output i get when i compile;

init:
deps-jar:
Compiling 1 source file to /home/alias120/NetBeansProjects/createFile/build/classes
compile:
run:
Checking to see if file already exists...
true
You produced the following file contents: null
BUILD SUCCESSFUL (total time: 1 second)

Here is the code, I am trying to read the contents of the file and output it to the user. Any suggestions would be great, been racking my brain with this.

public class Main {

  public static void main(String[] args) throws Exception { 
      File inputFile = new File("/home/alias120/Desktop/School/CMIS 141/Project 4/input.txt");
      
      System.out.println("Checking to see if file already exists...");
      System.out.println(inputFile.exists());
      
     PrintWriter output = new PrintWriter(inputFile);
      
      output.print("We came, ");
      output.print("we saw, ");
      output.print("we conquered.");
     
     Input myInput = new Input();
     System.out.println("You produced the following file contents: "
             + myInput.readData);
     }
}
class Input{
 String readData;
     Input() throws Exception {
   
    File inputFile = new File("/home/alias120/Desktop/School/CMIS 141/Project 4/input.txt");
    
    Scanner input = new Scanner(inputFile);
    
    while(input.hasNext()){
        String s1 = input.next();
        String s2 = input.next();
        String s3 = input.next();
        readData = s1 + s2 + s3;
        
        input.close();
        
     }
   } 
}

readData is not a public variable, so you can't access it by myInput.readData. Try this for your Input class:

class Input{
  private String readData;
  Input() throws Exception {
    readData = "";
    File inputFile = new File("/home/alias120/Desktop/School/CMIS 141/Project 4/input.txt");
    Scanner input = new Scanner(inputFile);
    while(input.hasNext()){
      String s1 = input.next();
      readData += s1;
    }
    input.close();
  }
  public String getReadData()
  {
     return readData;
  }
}

Then use Input.getReadData() to display your result.

Doesn't readData have default (package) scope, and is therefore accessible from the main class? If it wasn't, you would get a compile error.
Maybe the output file needs to be closed before the contents are read?

You may not even need that variable , read from file and print it .

I'll give the code above a shot, Yes James i was curious about that as well. If the data was inaccessible from the main class i figured there would be a compilation error. Initially i tried containing both the write data and read data in the same class, but the program would not even read the data or output anything being contained in the while loop. So does my syntax look correct on the Scanner input and the while loop? I appreciate your time all.

-alias

Hi,

i believe that if you can use BufferedReader and InputStreamReader / filereader it would be better.

for e.g.

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileReader(FILE_NAME));
String content;
while((content=br.readLine))!=null)
{
/// do something with the content
}

i hope this will help you.

I am having a lot of trouble getting this program to work properly. No compilation errors, but this is the output i get when i compile;

init:
deps-jar:
Compiling 1 source file to /home/alias120/NetBeansProjects/createFile/build/classes
compile:
run:
Checking to see if file already exists...
true
You produced the following file contents: null
BUILD SUCCESSFUL (total time: 1 second)

Here is the code, I am trying to read the contents of the file and output it to the user. Any suggestions would be great, been racking my brain with this.

public class Main {

  public static void main(String[] args) throws Exception { 
      File inputFile = new File("/home/alias120/Desktop/School/CMIS 141/Project 4/input.txt");
      
      System.out.println("Checking to see if file already exists...");
      System.out.println(inputFile.exists());
      
     PrintWriter output = new PrintWriter(inputFile);
      
      output.print("We came, ");
      output.print("we saw, ");
      output.print("we conquered.");
     
     Input myInput = new Input();
     System.out.println("You produced the following file contents: "
             + myInput.readData);
     }
}
class Input{
 String readData;
     Input() throws Exception {
   
    File inputFile = new File("/home/alias120/Desktop/School/CMIS 141/Project 4/input.txt");
    
    Scanner input = new Scanner(inputFile);
    
    while(input.hasNext()){
        String s1 = input.next();
        String s2 = input.next();
        String s3 = input.next();
        readData = s1 + s2 + s3;
        
        input.close();
        
     }
   } 
}

Look OK to me, but like I said, you don't close the output file, so maybe your 3 lines are still sitting in a buffer somewhere waiting to be written to disk. Try an output.close(); immediately after the 3 primts

As for the accessibility of the variable, I was trying to be polite, rather than directly contradict the previous poster, but yes, the default scope is the whole package, so the two classes would be in the same scope unless declared otherwize, and there's no need to declare anything public.

Thanks for the responses guys. I will take a look at what you recommended PB, the only issue is that my Professor wants us to utilize certain elements in this project, Scanner and PrintWriter being two of them. I will add the output.close() James, the only issue is that i had that in there originally and it would still not output the files contents. I'll keep tooling around with it, i still need to implement StringBuffer to append the contents of the file so we'll see how that goes.

-alias

I was able to get the program working, but i had to use JFileChooser instead. For some reason once i implemented a GUI to select the file, it ran properly. I was going to try using command line arguements, but couldn't figure out how to get that to work properly. I appreciate the help here, thanks again guys.

-alias

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.