Humza
Humza

Reputation: 13

Need help in Java with reading doubles in an input file

Input File:

John 100000.555

Mary 278.0

Joe 43.7956

Chris .25

Essentially, what I am having trouble with is trying to get the double values integrated into my code, I have tried and searched this up for the past few hours, but have had no luck.

**Here is my code:**import java.io.*;

import java.util.Scanner;

public class Treasury {

public static void main (String[]args)throws Exception{

    Scanner input = new Scanner(new File("Treasury.txt.txt"));

    System.out.print(String.format("%15s", "Balance"));
    System.out.print(String.format("%2s", ""));
    System.out.println(String.format("%-10s", "Name"));
    System.out.println("---------------  ----------");

    int count = 0;      

    while(input.hasNext())
    {
        String name = input.next();
        input.next();
        String s = String.format("%-10s", "                 " + name);
        System.out.println(s);
    }

    while(input.hasNextLine())
    {      
        double amount = input.nextDouble();
        System.out.printf("%15.2f", amount);      
    }

    input.close();

}
}

NOT PRINTING NUMBERS, NEED TO PRINT NUMBERS WITH RIGHT JUSTIFY.

Upvotes: 0

Views: 63

Answers (1)

M. Kuijpers
M. Kuijpers

Reputation: 11

What you could do is read the next line first as a string and then parse it to a double, I can't try it right noe but it could work: double amount = Double.parseDouble(input.next());

Upvotes: 1

Related Questions