I am new to java but I have looked for an answer but unfortunately haven't found one specific to my situation. I'd like to know where to place my file "icecream.txt" so that netbeans can read it. My programs works if I code the absolute the path but I'd prefer not to do that as it needs to run on the other student's computers without changes. I have attached an image of where I have placed my file. Any help would be appreciated. neatbeans file folders
My code is bellow if that helps
package icecreamsales;
/**
*
* @author anonymous
*/
public class IceCreamSales {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
TextIO.readFile("icecream.txt");
}
catch (IllegalArgumentException e) {
System.out.println("Can't open file \"icecream.txt\" for reading!");
System.out.println("Please make sure the file is present before");
System.out.println("running the program.");
System.exit(1); // Terminates the program.
}
int totalIceCreamSales = 0;
int strawberryIceCreamSales = 0;
while (!TextIO.eof()) {
String readLines = TextIO.getln();
totalIceCreamSales++;
if (readLines.equals("Strawberry")) {
strawberryIceCreamSales++;
}
}
System.out.println("Icecream cone sales totalled " + totalIceCreamSales);
System.out.println("Strawberry icecream sales totalled " + strawberryIceCreamSales);
System.out.println("Strawberry icecream is " + ((double) strawberryIceCreamSales/totalIceCreamSales*100) + "%%" + " of total sales");
}
}
You need to move the file icecream.txt
into the netbeans project. For e.g.
IceCreamSales (Project)
|
+--icecream.txt
|
+--src
|
+--icecreamsales
|
+--IceCreamSales.java