In Java Formatting Output a.First name: left aligned,10 characters b.Last name:
ID: 3871016 • Letter: I
Question
In Java
Formatting Output
a.First name: left aligned,10 characters
b.Last name: left aligned,21 characters
c.GPA: display 3 decimal places
d.GPA: scientific notation with 5 decimal places
e.Favorite movie: maximum 15 characters
f.2 spaces between each column
*This is what is inside Unformatted Information.txt:
First Name Last Name Age GPA GPA-sn Favorite Movie Rubble Flintstone Barney Fred Wall Anastasia Nicholaevna Jefferson Airplane John Baldasarre Smith Vivienne Leigh Ike 56 3.457 3.45670e+00 Year One 60 2.385 2.38475e+00 The Land Befor 125 5.000 4.99999et00 A Space Odysse 89 3.900 3.90000e+00 From Russia wi 63 1.890 1.89000e+00 Woodstoc]k 5 0.036 3.57000e-02 The Lego Movie 1 1.235 1.23450e+00 Three Men and 99 2.400 2.40000e+00 Gone with the 19 3.771 3.77120e+00 Guardians of t Doe MinExplanation / Answer
Bean class Code:
package com;
public class DataBean {
private String fname;
private String lname;
private int age;
private double gPA;
private double gPA_sn;
private String favMovie;
public String getFname() {
return fname;
}
public void setFname(String fname) {
if(fname.length()<=10){
this.fname = fname;
}
else{
fname=fname.substring(0, 21);
this.fname = fname;
}
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
if(lname.length()<=21){
this.lname = lname;
}
else{
lname=lname.substring(0, 21);
this.lname = lname;
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGPA() {
return gPA;
}
public void setGPA(double gPA) {
gPA=RoundingCounterExample(gPA,3);
this.gPA = gPA;
}
public double getGPA_sn() {
return gPA_sn;
}
public void setGPA_sn(double gPA_sn) {
gPA_sn=RoundingCounterExample(gPA,5);
this.gPA_sn = gPA_sn;
}
public String getFavMovie() {
return favMovie;
}
public void setFavMovie(String favMovie) {
if(favMovie.length()<=15){
this.favMovie = favMovie;
}
else{
favMovie=favMovie.substring(0, 15);
this.favMovie = favMovie;
}
}
public static double RoundingCounterExample(double x, int position)
{
double a = x;
double temp = Math.pow(10.0, position);
a *= temp;
a = Math.round(a);
return (a / temp);
}
@Override
public String toString() {
return "DataBean [fname=" + fname + ", lname=" + lname + ", age=" + age
+ ", gPA=" + gPA + ", gPA_sn=" + gPA_sn + ", favMovie="
+ favMovie + "]";
}
}
Main Class code:
package com;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<DataBean> list =new ArrayList<DataBean>();
String FILENAME="D:\unformatedFile.txt.txt";
BufferedReader br = null;
FileReader fr = null;
try {
//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String temp="";
DataBean d=new DataBean();
//System.out.println(sCurrentLine);
String data []=sCurrentLine.split("\s+");
//System.out.println("Length " + data.length);
d.setFname(data[0]);
d.setLname(data[1]);
d.setAge(Integer.parseInt(data[2]));
d.setGPA(Double.parseDouble(data[3]));
d.setGPA_sn(Double.parseDouble(data[3]));
for(int i=4;i<data.length;i++){
temp=temp+" " + data[i];
}
//System.out.println(temp);
d.setFavMovie(temp);
list.add(d);
}
System.out.println("First name Last Name Age GPA GPA-sn Favourite Move");
for(DataBean db:list){
System.out.println(db.getFname()+ " " + db.getLname() + " " + db.getAge() + " " + db.getGPA() + " " + db.getGPA_sn() + " " + db.getFavMovie());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Out put :
First name Last Name Age GPA GPA-sn Favourite Move
Barney Rubble 56 3.457 3.457 Year One
Fred Flintstone 60 2.385 2.385 The Land Befor
Wall E 125 5.0 5.0 A Space Odysse
Anastasia Nicholaevna 89 3.9 3.9 From Russia wi
Jefferson Airplane 63 1.89 1.89 Woodstock
John Doe 5 0.036 0.036 The Lego Movie
Baldasarre Smith 1 1.235 1.235 Three Men and
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.