Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Using File Data

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Using File Data

    So our assignment calls for us to copy data into a text file and use the file with the data in it to calculate some stuff. Everything works EXCEPT for the calculation of percent increase/decrease between the numbers. Below the code is my current output.

    import java.io.*;
    import java.util.*;

    public class Cert{
    public static void main(String[] args)
    throws FileNotFoundException{
    Scanner certFile = new Scanner(new File("C:\\Users\\Zofia\\Documents\\Courses\\COSC 236\\Programs\\Lab5\\cert1.txt"));

    int total = 0;
    int newVul = 1;
    int oldVul;
    while (certFile.hasNextInt()){
    int year = certFile.nextInt();
    oldVul = newVul;
    newVul = certFile.nextInt();
    if (oldVul!=newVul){
    double pChange = (double)((newVul-oldVul)/oldVul)*100;
    total +=newVul;
    System.out.println("In the year " + year + ", there were " + newVul + " vulnerabilities.");
    if (total!=newVul){
    System.out.println("There is a " + pChange + "% change between " + (year-1) +" and " + year);
    }
    }
    }
    System.out.println("The total vulnerabilities is " + total);
    }
    }






    In the year 1995, there were 171 vulnerabilities.
    In the year 1996, there were 345 vulnerabilities.
    There is a 100.0% change between 1995 and 1996
    In the year 1997, there were 311 vulnerabilities.
    There is a 0.0% change between 1996 and 1997
    In the year 1998, there were 262 vulnerabilities.
    There is a 0.0% change between 1997 and 1998
    In the year 1999, there were 417 vulnerabilities.
    There is a 0.0% change between 1998 and 1999
    In the year 2000, there were 1090 vulnerabilities.
    There is a 100.0% change between 1999 and 2000
    In the year 2001, there were 2437 vulnerabilities.
    There is a 100.0% change between 2000 and 2001
    In the year 2002, there were 4129 vulnerabilities.
    There is a 0.0% change between 2001 and 2002
    In the year 2003, there were 3784 vulnerabilities.
    There is a 0.0% change between 2002 and 2003
    In the year 2004, there were 3780 vulnerabilities.
    There is a 0.0% change between 2003 and 2004
    In the year 2005, there were 5990 vulnerabilities.
    There is a 0.0% change between 2004 and 2005
    In the year 2006, there were 8064 vulnerabilities.
    There is a 0.0% change between 2005 and 2006
    In the year 2007, there were 7236 vulnerabilities.
    There is a 0.0% change between 2006 and 2007
    The total vulnerabilities is 38016.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using File Data

    This is the fault of integer division.

    In this statement:
    double pChange = (double)((newVul-oldVul)/oldVul)*100;

    You are parsing it to a double AFTER dividing. Which means that it is doing integer division before parsing into a double, resulting in either 1 or 0, where 1 is 100% and 0 is everything below 100%. That is not good. To change that, you need to parse the numerator into a double BEFORE dividing. This is what it should be:
    double pChange = (((double)(newVul-oldVul))/oldVul)*100;

    It is truly amazing what moving around a few parentheses can do.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    computercoder (October 19th, 2010)

Similar Threads

  1. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 09:16 PM
  2. [SOLVED] Compressed data from XML file
    By eclipse in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: July 15th, 2010, 02:39 PM
  3. Retrieving Meta-Data from a File
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: February 9th, 2010, 08:10 AM
  4. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM
  5. Virutal File Directory from Given dAta
    By hugsheals in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 28th, 2009, 03:39 AM