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();