Question
I need help. I am trying to run this code that is suppose to find a file in my computer. I have the file, but
I need help. I am trying to run this code that is suppose to find a file in my computer. I have the file, but netbeans will not find the file. Where do I need to put the file. I have a dell laptop running windows 10. I am about to pull my hair out. Right now the code is in my downloads.
This is the code:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
//Class public class BabyNames { //Declare the needed variables private int birthyear; private char babygender; private String babyname; //Method getbirthyear() int getbirthyear() { //Return return birthyear; } //Method getbabyname() String getbabyname() { //Return return babyname; } //Method getuserinput() void getuserinput() { //Create scanner object Scanner scObj= new Scanner(System.in); //Prompt System.out.print("Enter the year: "); //Read birthyear=scObj.nextInt(); //Prompt System.out.print("Enter the gender: "); //Read babygender=scObj.next().charAt(0); //Prompt System.out.print("Enter the name: "); //Read babyname=scObj.next(); //Close scanner scObj.close(); } //Method computeRank() int computeRank() { //Input file File inFile=new File("babynameranking"+birthyear+".txt"); //Try block try { //Create a new scanner object Scanner scObj1 = new Scanner(inFile); //Declare the needed variables int rank; String boybabyname,girlbabyname; //Loop while(scObj1.hasNext()) { //Read and initialize variables from file rank=scObj1.nextInt(); boybabyname=scObj1.next(); scObj1.next(); girlbabyname=scObj1.next(); scObj1.next(); //Check condition if(babygender=='M') { //Check condition if(boybabyname.equals(babyname)) { //Return return rank; } } //Check condition if(babygender=='F') { //Check condition if(girlbabyname.equals(babyname)) { //Return return rank; } } }
//Close scanner object scObj1.close(); } //Catch catch (FileNotFoundException e) { //Exception e.printStackTrace(); } //Return return -1; } //Driver public static void main(String[] args) { //Create a new object BabyNames o=new BabyNames(); //Function call o.getuserinput(); //Function call int rank=o.computeRank(); //Check condition if(rank!=-1) { //Display System.out.println(o.getbabyname()+" is ranked #"+rank+" in year "+o.getbirthyear()); } //Otherwise else { //Display System.out.println("The name "+o.getbabyname()+" is not ranked in year "+o.getbirthyear()); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started