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 4 of 4

Thread: Reading lines from a txt file

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading lines from a txt file

    I'm having trouble with my code.
    I have a text file with the following input
    99.99 67 178843.00
    87.4 3 8983.00

    how do i read these input and store them in separate variables?
    this is the code that i have:-
    Scanner inputFile = new Scanner(myFile);
    while (inputFile.hasNext())
    {
    interest = inputFile.nextDouble();
    years = inputFile.nextInt();
    amount = inputFile.nextDouble();

    bank.setInterestRate(interest);
    bank.setYears(years);
    bank.setAmount(amount);
    System.out.println();
    System.out.println("The monthly payment is: " + money.format(bank.getMonthlyPayment()));
    System.out.println("The total payment is: " + money.format(bank.getTotalPayment()));

    }
    inputFile.close()


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading lines from a txt file

    I'm having trouble with my code.
    Can you explain what the problem is? What does the code do when executed?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading lines from a txt file

    <Scanner inputFile = new Scanner(myFile);
    while (inputFile.hasNext())
    {
    interest = inputFile.nextDouble();
    years = inputFile.nextInt();
    amount = inputFile.nextDouble();
     
    bank.setInterestRate(interest);
    bank.setYears(years);
    bank.setAmount(amount);
    System.out.println();
    System.out.println("The monthly payment is: " + money.format(bank.getMonthlyPayment()));
    System.out.println("The total payment is: " + money.format(bank.getTotalPayment()));
     
    }
    inputFile.close()>
    it reads numbers from the text file to calculate a loan.
    Last edited by Norm; April 6th, 2013 at 01:52 PM. Reason: Removd spaces from code tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading lines from a txt file

    The posted code needs formatting. All statements should NOT start in the first column. Statements within {} should be indented 3-4 spaces.

    Can you explain what the problem is?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to skip and read lines in txt file
    By saran123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 26th, 2012, 08:07 AM
  2. reading a certain amount of lines from a file?
    By welikedogs in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2010, 01:28 PM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM
  5. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 02:40 AM