Java Programing I have a stack of objects with some records for a day (recoreds
ID: 3604197 • Letter: J
Question
Java Programing
I have a stack of objects with some records for a day (recoreds and companys are registered from the companys agents and they are changing):
Company Name: Microsoft / Stock Prices:84$ / Time of Last Changes: 11:00
Company Name: Apple / Stock Prices:162$ / Time of Last Changes: 14:00
Company Name: TWC / Stock Prices:99$ / Time of Last Changes: 14:00
Company Name: Verizon / Stock Prices:44$ / Time of Last Changes: 11:00
Company Name: Apple / Stock Prices:160$ / Time of Last Changes: 12:00
Company Name: TWC / Stock Prices:96$ / Time of Last Changes: 10:00
Company Name: Apple / Stock Prices:150$ / Time of Last Changes: 10:00
Company Name: google / Stock Prices:1044$ / Time of Last Changes: 10:00
I need to save the last stock price changes of the companies (only last prices) all to a file
and then read the file and then print them from them (java code )
Explanation / Answer
import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
try
{
int con=1;
Scanner in = new Scanner(System.in);
FileWriter writer = new FileWriter("stock.txt"); // creating a file stock.txt to write data
//Reading data from console and writing the same to file
System.out.println(" writing data in to file : ");
while(con == 1)
{
System.out.println("Please Enter Company Name : ");
String s = in.next();
System.out.println("Please Enter Stock Price : ");
int p = in.nextInt();
System.out.println("Please Enter Time of Last Change : ");
String t = in.next();
//writing the above fields in the given format in to file
writer.write("Company Name: "+s+" / Stock Prices: "+p+"$"+" / Time of Last Changes: "+t+" ");
//User can press 1 if he want to add more records
System.out.println("Press 1 to continue : ");
con=in.nextInt();
}
writer.close(); // closing the writer after writing data to file
System.out.println(" Writing ends......... ");
}catch(Exception e){
System.out.println(e);
}
// Reading data from the file
try
{
BufferedReader in = new BufferedReader(new FileReader("stock.txt"));
String str;
System.out.println(" Reading data from file : ");
while ((str = in.readLine()) != null)
{
String splitstr[] = str.split("/");
System.out.println(splitstr[0]);
System.out.println(splitstr[1]);
System.out.println(splitstr[2]);
System.out.println(" ");
}
in.close();
}catch(Exception e){
System.out.println(e);
}
System.out.println(" Reading ends......... ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.