import java.io.*; public class SimpleReader { public void readAndPrintFile(Strin
ID: 3615503 • Letter: I
Question
import java.io.*;public class SimpleReader
{
public void readAndPrintFile(String fileName)
{
String line = null;
// try to do the following
try
{
// create the buffered reader
BufferedReader reader =
new BufferedReader(newFileReader(fileName));
// Loop while there is more data
while((line = reader.readLine())!=null)
{
// print the currentline
System.out.println(line);
}
// close the reader
reader.close();
}
catch(FileNotFoundException ex)
{
SimpleOutput.showError("Couldn'tfind " + fileName + " please pick it.");
fileName =FileChooser.pickAFile();
readAndPrintFile(fileName);
}
catch(Exception ex)
{
SimpleOutput.showError("Errorreading file " + fileName);
ex.printStackTrace();
}
}
public static void main(String[] args)
{
SimpleReader reader = new SimpleReader();
reader.readAndPrintFile("SHIPS.txt");
}
}
Let's say SHIPS.txt file says the following:
Name: Norman, age: 10, height: 5.5
Name: John, age: 15, height 5.4,
Now I want to be able to say some kind of greetigs to thesepersons. Tell them what their age will be in 10 years from now.Tell them they will grow .1 every year and that Norman will be 5.6next year and John will be 5.5 next year.
Please make it so I am able to hit the RUN and be able to see allthis info.
Thank you
Explanation / Answer
//Dear, you can use tokenizer , hopethis will help you.
import java.io.*; import java.util.*; public class SimpleReader { public void readAndPrintFile(StringfileName) { String line = null; String name=null,age=null,height=null; // try to do the following try { // create the buffered reader BufferedReader reader = new BufferedReader(newFileReader(fileName)); // Loop while there is moredata while((line = reader.readLine())!=null) { StringTokenizer st,s1; st = new StringTokenizer ( line, "," );
while ( st.hasMoreTokens ( ) ) { String s = newString(st.nextToken()); s1 = new StringTokenizer ( s, ": " ); if(s1.hasMoreTokens ( ) ) { s=s1.nextToken(); //System.out.println(s); if(s1.hasMoreTokens()) { if(s.equals("Name")) name = s1.nextToken(); if(s.equals("age")) age = s1.nextToken(); if(s.equals("height")) height = s1.nextToken(); } } } //System.out.println(line); System.out.println("Name is " + name + " , ageafter 10 will be " + (Integer.valueOf(age) + 10) + " and height is"+ height); } // close the reader reader.close(); } catch(FileNotFoundException ex) { SimpleOutput.showError("Couldn'tfind " + fileName + " please pick it."); fileName =FileChooser.pickAFile(); readAndPrintFile(fileName); } catch(Exception ex) { SimpleOutput.showError("Errorreading file " + fileName); ex.printStackTrace(); } } public static void main(String[] args) { SimpleReader reader = newSimpleReader(); reader.readAndPrintFile("SHIPS.txt"); } }
import java.io.*; import java.util.*; public class SimpleReader { public void readAndPrintFile(StringfileName) { String line = null; String name=null,age=null,height=null; // try to do the following try { // create the buffered reader BufferedReader reader = new BufferedReader(newFileReader(fileName)); // Loop while there is moredata while((line = reader.readLine())!=null) { StringTokenizer st,s1; st = new StringTokenizer ( line, "," );
while ( st.hasMoreTokens ( ) ) { String s = newString(st.nextToken()); s1 = new StringTokenizer ( s, ": " ); if(s1.hasMoreTokens ( ) ) { s=s1.nextToken(); //System.out.println(s); if(s1.hasMoreTokens()) { if(s.equals("Name")) name = s1.nextToken(); if(s.equals("age")) age = s1.nextToken(); if(s.equals("height")) height = s1.nextToken(); } } } //System.out.println(line); System.out.println("Name is " + name + " , ageafter 10 will be " + (Integer.valueOf(age) + 10) + " and height is"+ height); } // close the reader reader.close(); } catch(FileNotFoundException ex) { SimpleOutput.showError("Couldn'tfind " + fileName + " please pick it."); fileName =FileChooser.pickAFile(); readAndPrintFile(fileName); } catch(Exception ex) { SimpleOutput.showError("Errorreading file " + fileName); ex.printStackTrace(); } } public static void main(String[] args) { SimpleReader reader = newSimpleReader(); reader.readAndPrintFile("SHIPS.txt"); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.