Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating variables from a text file

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Creating variables from a text file

    public class Main {

    public static void main(String[] args) throws FileNotFoundException {
    double score;
    String line;
    String fname;
    String lname;

    String filename = JOptionPane.showInputDialog("Filename: ");

    FileInputStream input_file = new FileInputStream(filename);
    Scanner input = new Scanner(input_file);

    while (input.hasNextLine()) {
    line = input.nextLine();
    System.out.println(line);
    }
    }
    }

    Scanner is reading a text file that is formatted like this:
    Bob Smith 99.0
    John Doe 89.5
    Douglas Adams 42.0

    The program as is works when I enter the name of a txt file that's in the root directory.

    However, I'm supposed to use printf(); to get the first/last names and scores to line up.
    In order to do this I'm guessing (probably wrong) that I need to create 2 string variables for first and last names and 1 double variable for the scores.

    I can't figure out how to do this.
    Is there a way I can use scanner.next.. to separate each line in the text file into individual strings/doubles?

    Thanks in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating variables from a text file

    yep:

    String firstName = input.next();
    String lastName = input.next();
    double score = input.nextDouble();

Similar Threads

  1. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  2. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  3. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  4. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM
  5. [SOLVED] Enhancement in program of removing whitespace from text file
    By John in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 27th, 2009, 09:36 AM

Tags for this Thread