So ive got my text file which is seperated as so:
Title - Author - Price - Publisher - ISBN (it needs to be like this)
and what I want to do is get all of the price values out of this text document:
Windows XP - Graham Winter - 32.50 - O'Reilly - 0471974555
Windows XP Unwired - Wei Meng Lee - 24.95 - O'Reilly - 0596005369
CCDA Exam Guide - Anthony Bruno - 49.95 - Cisco Press - 0735700745
Multimedia Comms - Fred Halsall - 53.99 - Addison Wesley - 0201398184
Guide to Networks - Tamara Dean - 34.50 - Course Tech - 1439055661
802.11 Security - Jon Edney and William Hal - 68.99 - Addison Wesley - 0321136209 Wireless Hacks - Rob Weeks - 29.50 - O'Reilly - 0596101442
Large Scale LANs - Kevin Dooley - 39.00 - O'Reilly - 0596001509
Learning Java - William Lane -12.00 - Wiley - 0811234561
This code is what I have so far but I am really stuck as to how to get the total values from the splitArray[2] which is a string type.
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
public class Part_2 {
private static int count;
public static void main(String[] args){
int D = 0;
String line = "";
int num = 0;
Scanner keyboard = new Scanner (System.in);
{
// Allow the user to enter the name of text file that the data is stored in
System.out.println("Type in name of the file");
String fileName = keyboard.nextLine();
File Fileobject = new File (fileName);
{
if (!Fileobject.exists())
{
System.out.println("Error - File does not exist");
System.exit(0);
}
try {
count = 0;
if (count < 5)
{
Scanner fileReader = new Scanner (Fileobject);
System.out.println("\n"+String.format("%-30s", "Title") +
String.format("%-25s", "Author") +
String.format("%-25s", "Price") +
String.format("%-25s", "Publisher") +
String.format("%-25s", "ISBN"));
System.out.println(String.format("%-30s", "=====")+
String.format("%-25s", "=====")+
String.format("%-25s", "=====")+
String.format("%-25s", "======")+
String.format("%-25s", "===="));
while(fileReader.hasNext())
{
line = fileReader.nextLine();// Read a line of data from text file
num = num +1;
// The format of each line of data in the text file is as follow:
// Title - Author - Price - Publisher - ISBN
// Need to split each line read from file before can process it
try {
String [] splitArray = line.split("-");
// check to make sure there are 4 parts in splitArray
if(splitArray.length == 4)
{
// remove spaces
splitArray[0] = splitArray[0].trim();
splitArray[1] = splitArray[1].trim();
splitArray[2] = splitArray[2].trim();
splitArray[3] = splitArray[3].trim();
splitArray[4] = splitArray[4].trim();
}
if (splitArray[0].isEmpty())
{
D++;
}
if (splitArray[1].isEmpty())
{
D++;
}
if (splitArray[2].isEmpty())
{
D++;
}
if (splitArray[3].isEmpty())
{
D++;
}
if (splitArray[4].isEmpty())
{
D++;
}
System.out.println(String.format(" %-30s", splitArray[0])+
String.format(("%-25s"), splitArray[1])+
String.format(("%-25s"), splitArray[2])+
String.format(("%-25s"), splitArray[3])+
String.format(("%-25s"), splitArray[4]));
}
catch (Exception e) {
System.out.println("Line delimiter error");
D++;
}
}
System.out.println("");
System.out.println("totals");
System.out.println("-----------------------------");
System.out.println("The total number of books: ");
System.out.println("The total costs of books: ");
System.out.println("Maximum cost of a book: " );
System.out.println("Minimum cost of a book: " );
System.out.println("Average cost of a book: " );
System.out.println("there are " + D + " error(s) within the text document");
}
}
catch (IOException e) {
// IO error while reading from the file.
System.err.println("Error while reading from file ");
}
}
}
}
}
All of the data in the file gets split and then is put into the different arrays which are all string types, however this is a problem as I am trying to find the total price of all of the books.
Any help on this problem would be appreciated, I have tried multiple ways but I always seem to get back a null value or 0.0