java - Why will my code read a file when run on netbeans but not when run on CMD? -
my code working fine , supposed on netbeans when use cmd doesn't execute on netbeans. first try/catch supposed count number of times reads on txt file. on netbeans works fine on cmd keeps declared value of 0.
import java.io.*; import java.io.filenotfoundexception; import java.util.scanner; import java.util.nosuchelementexception; public class calories { public static void main(string[] args) throws nosuchelementexception { string file = "input.txt"; string text = ""; double [] breakfast = new double [7]; double [] lunch = new double [7]; double [] dinner = new double [7]; int counter = 0; try { scanner input = new scanner(new file("input.txt")); while(input.hasnextint()) { int number = input.nextint(); system.out.println(number); counter++; } } catch (exception ex) { } //if file missing number or has program tell user , exit if (counter != 21) { system.out.println("counter " + counter); system.out.println("your file not work program. please try again."); system.exit(0); } //this going attempt read file "input.txt" //if file cannot found user told cannot found try { scanner s = new scanner(new file(file)); while (s.hasnextint()) { (int = 0;i<7;i++) { breakfast[i] = s.nextint(); lunch[i] = s.nextint(); dinner[i] = s.nextint(); } } } //this catch execute if file name incorrect catch(filenotfoundexception e) { system.out.println("file not found"); } //calling of methods created here getcal(breakfast, lunch, dinner); getdays(breakfast, lunch, dinner); getavg(breakfast, lunch, dinner); //exits program once has finished executing methods system.exit(0); }
i've tried knowledge still cannot figure out.
when running command line code expects input.txt
file in same directory running java [program_name] [file_name]
command
if place file in same directory work fine.
on other hand netbeans handle resolution of file path if place in project root directory example.
"input.txt"
name not telling file placed. upto netbeans or command line try find file.
if use project resources path or relative path try find there , have info find file.
similarly if put absolute path file file searched in path.
but here have not provided information path.
Comments
Post a Comment