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: Program works fine but with friend it doesn't work

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program works fine but with friend it doesn't work

    This is odd. Really odd. It works just fine on my computer. But on my friends computer, everything is the same. Except the file location. And when it compiles. There are no errors. But when it runs, it runs the first half of the program normally. But gives an error on the second half.

    For this to work, you have to create a file with 6 double values ahead of time.

    package program5;
     
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.Scanner;
     
    /**
     * Name: Bilal Hasan
     * Course: CS 1336
     * Section: 502
     * Date: 11/28/2011
     * IDE: NetBeans 7.0.1
     * Operating System: Ubuntu 11.10
     */
    public class Program5 {
        /*
         * Description: This program will take a file with 6 double values which are "earnings"
         * and outputs the contents of the file. It will then sort the double values in
         * numberical order and out put that information also.
         */
     
        public static void main(String[] args) throws IOException {
     
            File inFile = new File ("/home/kudafi/Desktop/Java/Program5/earnings4.txt");
            Scanner inputFile = new Scanner(inFile);
            DecimalFormat format = new DecimalFormat ("##.00");
     
            double [] numbersFromFile = new double [6];
     
            System.out.println("The earnings from your file are:");
     
            //--------------------Takes the file and sends it to method
            numbersFromFile = displayEarningsFromFile ( inputFile, numbersFromFile );
     
            System.out.println("\n");
     
            //--------------Sorts the array
            Arrays.sort(numbersFromFile);
     
            //-----------------------Displays sorted order
            System.out.println("After sorting the numbers are: ");
     
            for (int j = 0; j < numbersFromFile.length; j++) {
     
                System.out.println(format.format(numbersFromFile[j]));
            }//end for loop
     
        }//end main
     
        ///////////////////////////////////////////-------------method to display earnings from file
     
        public static double[] displayEarningsFromFile (Scanner inputFile, double[] valuesFromFile)
        {
     
            //---------------------for loop to display contents from file
            for (int i = 0; i < valuesFromFile.length; i++)
            {
                String line = inputFile.nextLine();
     
                //converts string to double
                double num = Double.parseDouble(line);
                valuesFromFile[i] = num;
     
                System.out.println(line);
            }//end for loop
     
            return valuesFromFile;
        }//end displayEarningsFromFile method
    }//end class

    It gets an error at this line. String line = inputFile.nextLine();


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Program works fine but with friend it doesn't work

    What error does it generate? Where is it looking for the file? Where is the file actually located?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: October 4th, 2011, 09:03 PM
  2. The program working fine but the "NO" button doesn't work
    By ahcenpg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2011, 07:32 PM
  3. This program works but Want to improve
    By SHStudent21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 8th, 2011, 05:53 PM
  4. New friend
    By TarunN in forum Member Introductions
    Replies: 5
    Last Post: April 29th, 2010, 11:21 PM
  5. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM