Results 1 to 5 of 5
- 03-06-2010, 08:32 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
Need help storing arrays from a file
Hello to all.. New member here hoping to learn as much about Java(programming as possible).
I am stuck on a program though.. Anyone whom can help, i just want you to know that your help is HIGHLY appreciative ahead of time and thank you...
Ok here we go...
This program is required to manage a collection of bonds for some individual. For each bond, the program has to store the name(string), principal(double), annual interest rate(double), and the maturity(int)year) all to be stored in parallel arrays and to be read from a input file.
A small example of the input file that i'm reading from looks something like this
Java Code:Google 150000.00 7.5 2018 US Government 50000.00 3.25 2020
The program is menu-driven. Such as:
Java Code:1. List organization names 2. List the organization names for Jumbo bonds only 3. List principals 4. Show the largest principal 5. List annual interest rates 6. List annual interest rates in ascending order 7. Show the average annual interest rate 8. Show the total of all semiannual interest payments 9. List maturities 10. Show information pertaining to a given bond 11. Purchase a new bond 12. Quit
As it stands now, I'm REALLY confused on option 8, 10 and 11, but I'll try and cross those paths when i get to them.. Right now, I'm trying to figure out how to read the files in my input txt file and place them into their proper array variables. I know how to read a file of ALL integers into an int array, a file of ALL doubles into a double array, but how do i read a file such as the one that i have, and place them into separate arrays(String,Double,Double,Int)? Meaning, read the first line of the file(Org name(string)) and place it into the organization name array and then the next line(Principal(double)) and place it into the Principal array, and the next line(Annual Interest rate(double)) and put read it into the annual interest array, and so forth until the cursor is at the end of the txt file?
The codes that we've learned so far for reading a file have been pretty much:
orJava Code:File infile = new File("txt file"); Scanner fsc = new Scanner(infile); // read first data file int num_hours = fsc.nextLine(); fsc.nextLine(); // read 2nd data file double hourly_rate = fsc.nextDouble(); fsc.nextLine();
So any other java code other than something like those two, i really haven't learned anything about yet..Java Code:int num_grades = 0; while (fsc.hasNext()) { grade[num_grades] = fsc.nextInt(); num_grades ++; }
I'm still pretty much in the beginning process of the program, just trying to do bits and pieces at a time, so forgive me for my program being pretty empty as of right now. I have quite a few things written down on paper that i haven't passed over to my program yet.
What i have so farJava Code:import java.util.*; import java.io.*; public class projectone { public static void main(String[] args) throws java.io.IOException { // declare variables int choice, i; boolean finished; String org_name [] = new String [50]; double principal [] = new double [50]; double annual_intrest [] = new double [50]; int maturity [] = new int [50]; //establish keyboard input stream Scanner sc = new Scanner(System.in); // Get required data items from user // Prompt user for input file name System.out.print("Enter name of the input file: "); String infilename = sc.nextLine(); // Open the file for reading File infile = new File("projection.txt"); Scanner fsc = new Scanner(infile); // Read the data in the file org_name = fsc.nextLine(); fsc.nextLine(); // Read the second data file principal = fsc.nextDouble(); fsc.nextLine(); // Read the third data file double aunnual_intrest = fsc.nextDouble(); fsc.nextLine(); // Read the fourth data file int mataurity = fsc.nextInt(); // start of menu options finished = false; do { // display the menu display_menu(); // get the user's choice, and carry it out System.out.print(" Enter your choice: "); choice = sc.nextInt(); sc.nextLine(); System.out.println(); if(choice == 1) { } else if(choice == 2) { } else if(choice == 3) { } else if(choice == 4) { } else if(choice == 5) { } else if(choice == 6) { } else if(choice == 7) { } else if(choice == 8) { } else if(choice ==9) { } else if(choice == 10) { } else if(choice == 11) { } else if(choice == 12) // Quit menu finished = true; // test for invalid user input else { System.out.println(" Option selected, is not valid. "); System.out.println(" Please make another selection. "); } } while(!finished); // output results }// end of main method //////////////////////////////////////////////////////////////////// public static void display_menu() { // display the menu of user choices System.out.println(); System.out.println(" 1. List organization names: "); System.out.println(" 2. List the organization names for Jumbo bonds only: "); System.out.println(" 3. List principals: "); System.out.println(" 4. Show the largest principal: "); System.out.println(" 5. List annual interest rates: "); System.out.println(" 6. List annual interest rates in ascending order: "); System.out.println(" 7. Show the average annual interest rate: "); System.out.println(" 8. Show the total of all semiannual interest payments: "); System.out.println(" 9. List maturities: "); System.out.println(" 10. Show information pertaining to a given bond: "); System.out.println(" 11. Purchase a new bond: "); System.out.println(" 12. Quit: "); System.out.println(); } //end display_menu ////////////////////////////////////////////////////////////////// } // end of class
- 03-06-2010, 01:27 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Once you read the file, line by line, check whether it's a number or a string. Just search how to do it first, then later update your status here. We can comment on you.
Then what you've to do is store them in separate arrays. Hope you knows about Arrays and stuff in Java.
- 03-06-2010, 01:27 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Once you read the file, line by line, check whether it's a number or a string. Just search how to do it first, then later update your status here. We can comment on you.
Then what you've to do is store them in separate arrays. Hope you knows about Arrays and stuff in Java.
- 03-06-2010, 08:51 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
Thats what i'm not understanding at the moment.. I know how to read a file, but mainly only if it's all of the same data type or if it's a little file. The file i'm reading from is fairly big and its of 3 different data types(String, double, double and an Int)
Java Code:Google 150000.00 7.5 2018 US Government 5000.00 3.25 2020 NYC Water AUTH 250000.00 5.15 2022 Harrah's 52000.00 15.75 2018 Qualcomm 12000.00 9.35 2012 Bergen County 18000.00 4.375 2027 Delaware River Port Auth 135000.00 5.175 2026 San Jacinto CMNTY CLG 30000.00 4.7 2020 GE Capital 400000.00 5.62 2020 Union UTIL AUTH 45000.00 5.0 2023 Dow Chemical 175000.00 8.55 2019 Minneapolis BRD ED 105000.00 4.25 2040 East Brunswick TWP 45000.00 6.25 2024 CIT Group 95000.00 10.375 2017 Newpage 8000.00 14.0 2014
So using the code that i know of, which is:would be too long right?Java Code:File infile = new File("txt file"); Scanner fsc = new Scanner(infile); // read first data file int num_hours = fsc.nextLine(); fsc.nextLine(); // read 2nd data file double hourly_rate = fsc.nextDouble(); fsc.nextLine();
- 03-08-2010, 04:11 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Since your file have specific pattern it's not difficult, and within a loop you can easily do it.
Similar Threads
-
Storing data to file :s
By idi in forum New To JavaReplies: 3Last Post: 02-13-2010, 08:54 PM -
Reading a txt file and then storing it in a 2d array
By blkshp1990 in forum New To JavaReplies: 2Last Post: 11-05-2009, 12:31 AM -
Storing and managing huge arrays
By trust in forum Java SoftwareReplies: 2Last Post: 08-22-2009, 02:47 PM -
Storing the data in a file
By vasavi.singh in forum New To JavaReplies: 4Last Post: 02-20-2009, 04:01 PM -
storing and retrieving a file as such
By anil_manu in forum Advanced JavaReplies: 0Last Post: 03-11-2008, 01:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks