Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Question about Java

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
This topic is archived.
Home » Discuss » The DU Lounge Donate to DU
 
ButterflyBlood Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 05:42 PM
Original message
Question about Java
Edited on Sat Feb-26-05 05:42 PM by ButterflyBlood
OK, let's say I have a basic text file with one sentence on it. How do I get that sentence to be printed through the program?

I know this code could input it:

public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("fileName"));
}
catch (FileNotFoundException ex) {
System.exit(1);

But then how do you print the contents of the file put in?
Printer Friendly | Permalink |  | Top
Huckebein the Raven Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 05:48 PM
Response to Original message
1. Check this out:
Printer Friendly | Permalink |  | Top
 
ButterflyBlood Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 05:59 PM
Response to Reply #1
2. have looked at it, unfortunately
it only tells how to input a file, not print the contents.
Printer Friendly | Permalink |  | Top
 
Huckebein the Raven Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:05 PM
Response to Reply #2
3. self-delete
Edited on Sat Feb-26-05 06:11 PM by Dark_Leftist
Printer Friendly | Permalink |  | Top
 
Huckebein the Raven Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:09 PM
Response to Original message
4. Using your code, couldn't you do something like this :
Edited on Sat Feb-26-05 06:13 PM by Dark_Leftist
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("fileName"));
System.out.println(sc.nextLine());
sc.close();

}
catch (FileNotFoundException ex) {
System.exit(1);

I used the link I posted earlier as reference

-hope this helps, haven't worked with Java in some time
Printer Friendly | Permalink |  | Top
 
ButterflyBlood Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:12 PM
Response to Reply #4
5. thank you, it worked!
n/t
Printer Friendly | Permalink |  | Top
 
Huckebein the Raven Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:13 PM
Response to Reply #5
6. you're welcome
:)
Printer Friendly | Permalink |  | Top
 
ButterflyBlood Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:16 PM
Response to Reply #6
7. another question though
that only prints the first line of the file. How do you get it to print every line? I assume it could be done with some sort of while loop, but only if the program knew how many lines they were in the file already. Is there any way to get that?
Printer Friendly | Permalink |  | Top
 
Huckebein the Raven Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:24 PM
Response to Reply #7
8. You could do it with a loop but you don't have to know
Edited on Sat Feb-26-05 06:25 PM by Dark_Leftist
how many lines there are in the file beforehand. The Scanner class has a method that looks to see if there is another line in whatever you are scanning:

while ( sc.hasNextLine() )
{
do want you need to do


}

if there are no more lines it should break out of the loop

//close the scanner here

The link I posted is a very good reference, but it takes a while to search what you are looking for:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Printer Friendly | Permalink |  | Top
 
ButterflyBlood Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:33 PM
Response to Reply #8
9. thank you
I have read those documents and my text book, the problem is they often don't describe the type of things you need to do next to each other and you have to keep searching.
Printer Friendly | Permalink |  | Top
 
Huckebein the Raven Donating Member (1000+ posts) Send PM | Profile | Ignore Sat Feb-26-05 06:42 PM
Response to Reply #9
10. You're welcome. Believe me I know how it feels
Printer Friendly | Permalink |  | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Tue Apr 30th 2024, 07:25 AM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » The DU Lounge Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC