We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading from a file

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();
	
	
	}
		
		
			
		
		
		
	}
2
Contributors
9
Replies
26 Minutes
Discussion Span
1 Year Ago
Last Updated
10
Views
Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You have not shown what is in the file. Is the program working correctly now?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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+

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

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

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks for your help too it was much appreciated :D

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0872 seconds using 2.72MB