Hey guys I have this code snippet and I want to read from this file and print the contents of my text file (Transcript.txt) to the screen. I don't have any errors but when I run the program it prints "Display it to the standard output" instead of whats on my text file could I please have some assistance with this.

import java.io.File;
import java.util.Scanner;



public class GPAapp {

	
	
	public static void main(String[] args) {
		
			Scanner reader = null;
		
		
			
			try{
				reader = new Scanner(new File("Transcript.txt"));
			}
			
			catch(Exception e){
				System.out.println("File does not exist");
			}
		
	
	
			while(reader.hasNext()){
				
				
				System.out.print(reader.next());
			}
	
	
	
			reader.close();
	
	
	}
		
		
			
		
		
		
	}

Your code works for me. It prints one line with all the file's lines in it.

Can you copy the console for when you execute the program and paste it here?

Edited 5 Years Ago by NormR1: n/a

Your code works for me. It prints one line with all the files lines in it.

Can you copy the console for when you execute the program and paste it here?

Sure now its saying this whenever I execute it.

TestingScanner1012.2onetruetwofalse

Oh sorry Here is whats in the file:

A123456 John Doe
Term Fall 2011
CS 411 Object Oriented Programming
3 A
CS 211 Introduction to Programming
3 A
CS 560 Data Structures and Algorithms
3 B
CS 680 Software Engineering
3 B+

B654431 Jill Great
Term Fall 2011
CS 411 Object Oriented Programming
3 B+
CS 211 Introduction to Programming
3 A
CS 560 Data Structures and Algorithms
3 A
CS 680 Software Engineering
3 B+
Term Spring 2011
Eng 101 English
3 A
Phy 254 Physics I
3 B+

Are you sure that is the file? Can there be another one with the same name and different data?
Like:
TestingScanner
1012.2
one
true
two
false

Edited 5 Years Ago by NormR1: n/a

I'm positive its the only one on my computer and when I try and read different files it always catches my exception and says the file doesn't exist.

I got it to work I had to add the path for it to work

C:\\Users\\Billy Jr\\Documents\\Transcript.txt

The code works for me so I assume that you are having problems connecting to the file to be read.

How are you executing the program? Where is the file that you are trying to read?

To see the full path to the file where your program is trying to read the file, create a File object for the file and print out the absolute path to that file object. See the File class's API doc for the correct spelling of the method that returns the File's absolute path.

This article has been dead for over six months. Start a new discussion instead.