i am having problems in reading the contents of my .dat file.
the contents of my .dat file are the following:
10.20 60.20 70.20 25.25
1.1 2.2 3.3 4.4


my expected output should look like this:

--------- --------- --------- ---------
10.20 60.20 70.20 25.25
1.10 2.20 3.30 4.40

but when i run it, it displays like this O_O:

--------- --------- --------- ---------
10.20 60.20 70.20 25.25
10.20 60.20 70.20 25.25 1.10 2.20 3.30 4.40

how come the second set of numbers were out of the line...
im confused... i need help

import java.util.*;
import java.io.*;
class roll
{
public static void main(String[]args)throws Exception
{
Scanner ah=new Scanner(new FileReader("text.dat"));
Formatter fmt = new Formatter();
double w,x,y,z;
System.out.println("--------  --------  --------  --------");

while(ah.hasNext())
{
w=ah.nextDouble();
x=ah.nextDouble();
y=ah.nextDouble();
z=ah.nextDouble();
fmt.format("%8.2f  %8.2f  %8.2f  %8.2f",w,x,y,z);
System.out.println(fmt);
}

}
}

Recommended Answers

All 7 Replies

Have you considered using:
while(hasNextDouble)

Formatter lets you pass an Appendable object. This is a interface that defines append methods so that the formatter can store its results in a text collector such as a stream object.
Formatter formatter = new Formatter();
creates a formatter and the destination of the formatted output is a StringBuilder where the formatted output is appended.
To avoid appending you will need to create the instance of formatter in the while loop
e.g.

Formatter fmt = new Formatter();
fmt = fmt.format("%8.2f  %8.2f  %8.2f  %8.2f",w,x,y,z);
System.out.println(fmt);
fmt.close();
commented: Correct solution +6

Hello, my problem is solved regarding the double...

but my next problem is.. how can i read a String?
i added some names inside the .dat file.
i have succesfully displayed the numbers before when there were no names. but when i added names in my .dat file. in resulted into an error.

here's the new contents of my text.dat file

John Smith
9.45 40 15
Jane Doe
12.50 45 15
Harry Morgan
20.00 40 20
Carmen Martinez
25.00 35 25
Jacintha Washington
50.85 60 34

btw here's my new program code...

import java.util.*;
import java.io.*;
class roll
{
public static void main(String[]args)throws Exception
{
printEmployeeInfo();
}


public static void printEmployeeInfo()throws Exception
{
Scanner ah=new Scanner(new FileReader("text.dat"));
Formatter a= new Formatter();
String t;
char p;
double w,x,y,z,v,FULL_TIME=40.0;
while(ah.hasNext())
{

 if(ah.hasNextDouble());
 {
 w=ah.nextDouble();
 x=ah.nextDouble();
 y=ah.nextDouble();
 z=w*x;
 v=z-(z*(y/100.00));
     if(x<=FULL_TIME)
System.out.format("%8.2f  %8.2f  %8.2f  %8.2f  %8.2f\n",w,x,y,z,v);
    else
System.out.format("%8.2f  %8.2f  %8.2f  %8.2f  %8.2f  OT\n",w,x,y,z,v);
  }

}
}

}

i wanted to place the names before the numbers.
and left-justify it within 20characters...

i tried using %20.c and %20.s for the letters but it says error.

This is the problem

if(ah.hasNextDouble())[B];[/B]

When you add the ; it ends the command. Meaning that you ended the if command. Meaning that the rest:

{

}

Weren't "connected" with the if. It was like writing this:

if(ah.hasNextDouble()) { 
}

{
w=ah.nextDouble();
 x=ah.nextDouble();
 y=ah.nextDouble();
....
}

oh sry bout that i accidentally pressed the ";" key while posting.

i have solved the problem about reading the names
i remembered using "ah.next();"
but i dont know how to format them in left-justify within 20characters

To left justify names with 20 characters use following syntax to format the string in the formatter

%-20.20s

thanks for everything. this was the last activity given to us. thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.