Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,475 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,740 people online right now. Registration is fast and FREE... Join Now!




dont know why i'm not getting output. help

 

dont know why i'm not getting output. help

carman69

12 Nov, 2008 - 07:41 PM
Post #1

New D.I.C Head
*

Joined: 19 Aug, 2008
Posts: 20



Thanked: 1 times
My Contributions
the program runs in eclipse but displays no output. Anyone know why??




CODE
package Readafile;

import java.util.Scanner;

public class Readafile {

    
    
    public static void main(String[] args) {
        Scanner inputFile = new Scanner("books.txt");
        while(inputFile.hasNext());
        System.out.println(inputFile.next() + "");
        
        

    }

}


This post has been edited by Jayman: 12 Nov, 2008 - 07:47 PM

User is offlineProfile CardPM
+Quote Post


Jayman

RE: Dont Know Why I'm Not Getting Output. Help

12 Nov, 2008 - 07:48 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,544



Thanked: 226 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Do you have anything in the books.txt file?
User is offlineProfile CardPM
+Quote Post

carman69

RE: Dont Know Why I'm Not Getting Output. Help

12 Nov, 2008 - 07:50 PM
Post #3

New D.I.C Head
*

Joined: 19 Aug, 2008
Posts: 20



Thanked: 1 times
My Contributions
QUOTE(Jayman @ 12 Nov, 2008 - 07:48 PM) *

Do you have anything in the books.txt file?




yes the file contains a list for books
User is offlineProfile CardPM
+Quote Post

n8wxs

RE: Dont Know Why I'm Not Getting Output. Help

12 Nov, 2008 - 08:47 PM
Post #4

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 230 times
My Contributions
Change
java

while(inputFile.hasNext());
System.out.println(inputFile.next() + "");

to
java

while(inputFile.hasNext()) {
System.out.println(inputFile.next() + "");
}


User is offlineProfile CardPM
+Quote Post

Jayman

RE: Dont Know Why I'm Not Getting Output. Help

12 Nov, 2008 - 08:49 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,544



Thanked: 226 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Remove the semi-colon from the end of your while loop.

CODE

while(inputFile.hasNext())

User is offlineProfile CardPM
+Quote Post

carman69

RE: Dont Know Why I'm Not Getting Output. Help

13 Nov, 2008 - 05:09 AM
Post #6

New D.I.C Head
*

Joined: 19 Aug, 2008
Posts: 20



Thanked: 1 times
My Contributions
QUOTE(Jayman @ 12 Nov, 2008 - 08:49 PM) *

Remove the semi-colon from the end of your while loop.

CODE

while(inputFile.hasNext())







Thanks i'll give that a try.

QUOTE(carman69 @ 13 Nov, 2008 - 05:04 AM) *

QUOTE(Jayman @ 12 Nov, 2008 - 08:49 PM) *

Remove the semi-colon from the end of your while loop.

CODE

while(inputFile.hasNext())







Thanks i'll give that a try.


The program does output noe but it outputs the file name. How do I get it to read the file??
User is offlineProfile CardPM
+Quote Post

ajaymatrix

RE: Dont Know Why I'm Not Getting Output. Help

13 Nov, 2008 - 06:21 AM
Post #7

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 409



Thanked: 1 times
Dream Kudos: 100
My Contributions
you have to create a new File (pathname) in your scanner

CODE

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


public class ReadAFile {

    public static void main(String[] args) {
        Scanner inputFile;
        try {
//don't know how to give relative path..try solving that
            inputFile = new Scanner(new File("C:\\workspace\\FileReader\\src\\books.txt"));

            
            while(inputFile.hasNext()){
                    System.out.println(inputFile.next() + "");
             }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
        
        

    }

}



and remember to use try catch, so that you know where the error is coming
User is offlineProfile CardPM
+Quote Post

pbl

RE: Dont Know Why I'm Not Getting Output. Help

13 Nov, 2008 - 06:07 PM
Post #8

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Because you create a Scanner on the String "books.txt"
You have to make the scanner on the File

CODE

    public static void main(String[] args) {
        Scanner inputFile = new Scanner(new File("books.txt"));
        while(inputFile.hasNext())
        System.out.println(inputFile.next() + "");



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:25AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month