I need to write a program that will organize the information

I need to write a program that will organize the information in the text file called \"balances.txt\"into a report and find the total of the three balances.

I\'m getting an error every time I use nextDouble to read text file. The text file contains the following info:

JAKIE JOHNSON,2051.59
SAMUEL PAUL SMITH,10842.23
ELISE ELLISON,720.54
  

I am not allowed to use loops.

So far, I have the following:

import java.io.File;

import java.text.NumberFormat;

import java.text.DecimalFormat;

import java.io.FileInputStream ;

import java.io.FileNotFoundException ;

import java.io.IOException ;

import java.util.Scanner ;

public class Lab2

{

    public static void main(String[] args) throws IOException

    {      

            Scanner fileIn = null;

            try

            {         

                        String filename = \"balances.txt\";

                                    File newFile = new File(filename);

                                    Scanner in = new Scanner(newFile);

                            in.useDelimiter(\",|\ \");

           

                      String name1 = in.next();

                        double money1 = in.nextDouble();

                        System.out.println(name1 + \"\\t\" + money1);

                        String name2 = in.next();

                        String money2 = in.next();

                        System.out.println(name2 + \"\\t\" + money2);

                        String name3 = in.next();

                        String money3 = in.next();

                        System.out.println(name3 + \"\\t\" + money3);

                        System.out.println(money1 + money2);

            }

            catch (FileNotFoundException e)

            {

                        System.out.println(\"File not found.\");

                        System.exit(0);

            }

    }

}

  

I tried using next() instead of nextDouble() to store the balance of each person as a string, and it works. Its just I need to add all three balances into a total sum and for that, I need to store them into a double variable.

Solution

Hi,

I have fixed the issue. Highlighed the code changes below.

Lab2.java

import java.io.File;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.util.Scanner ;
public class Lab2
{
public static void main(String[] args) throws IOException
{
Scanner fileIn = null;
try
{   
String filename = \"D:\\\\balances.txt\";
File newFile = new File(filename);
Scanner in = new Scanner(newFile);
in.useDelimiter(\",|\ \");

String name1 = in.next();
double money1 = Double.parseDouble(in.next());
System.out.println(name1 + \"\\t\" + money1);
String name2 = in.next();
double money2 = Double.parseDouble(in.next());
System.out.println(name2 + \"\\t\" + money2);
String name3 = in.next();
double money3 = Double.parseDouble(in.next());
System.out.println(name3 + \"\\t\" + money3);
System.out.println(money1 + money2);
}
catch (FileNotFoundException e)
{
System.out.println(\"File not found.\");
System.exit(0);
}
}
}
  

Output:

JAKIE JOHNSON   2051.59
SAMUEL PAUL SMITH   10842.23
ELISE ELLISON   720.54
12893.82

I need to write a program that will organize the information in the text file called \
I need to write a program that will organize the information in the text file called \
I need to write a program that will organize the information in the text file called \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site