Concept explainers
How would I get this code to read multiple files?
JAVA
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BabyName
{
private int y;
private char g;
private String n;
int getbabyYear()
{
return y;
}
String getbabyName()
{
return n;
}
void getuserInput()
{
Scanner ns= new Scanner(System.in);
System.out.println("Enter the year: ");
y=ns.nextInt();
System.out.println("Enter the gender: ");
g=ns.next().charAt(0);
System.out.println("Enter the name: ");
n=ns.next();
ns.close();
}
int findRank()
{
File file=new File("/Users/apers/OneDrive/Desktop/babyranking/babynameranking_1980s.txt");
try
{
Scanner nf = new Scanner(file);
//declare the variable 'r' for rank
int r;
//declare the variable 'bn' for boyname and 'gn' for girl name
String bN,gN;
while(nf.hasNext())
{
r=nf.nextInt();
bN=nf.next();
nf.next();
gN=nf.next();
nf.next();
if(g=='M'){
if(bN.equals(n)){
return r;
}
}
if(g=='F'){
if(gN.equals(n)){
return r;
}
}
}
nf.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return -1;
}
public static void main(String[] args)
{
BabyName sn=new BabyName();
sn.getuserInput();
int r=sn.findRank();
if(r!=-1){
System.out.println(sn.getbabyName()+" is ranked #"+r+" in year "+sn.getbabyYear());
}else{
System.out.println("The name "+sn.getbabyName()+" is not ranked in year "+sn.getbabyYear());
}
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- I need help fixing this java program below: import java.util.Scanner;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.IOException; public class LabProgram { public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); String fileName; String[] lastNames, firstNames; int[] midterm1, midterm2, finalScores; // Read a file name from the user and read the tsv file fileName = scnr.next(); try (Scanner fileScanner = new Scanner(new FileInputStream(fileName))) { int numStudents = Integer.parseInt(fileScanner.nextLine()); lastNames = new String[numStudents]; firstNames = new String[numStudents]; midterm1 = new int[numStudents]; midterm2 = new int[numStudents]; finalScores = new int[numStudents]; for (int i = 0; i < numStudents; i++) { lastNames[i] =…arrow_forwardI'm getting this progm to run but vis producing no out put. import java.util.Scanner;import java.lang.Math; public class CarValue { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Car myCar = new Car(); int userYear = scnr.nextInt(); int userPrice = scnr.nextInt(); int userCurrentYear = scnr.nextInt(); myCar.setModelYear(userYear); myCar.setPurchasePrice(userPrice); myCar.calcCurrentValue(userCurrentYear); myCar.printInfo(); }} the calculations are performed in this prigram. private int currentValue; public void setModelYear(int userYear){ modelYear = userYear; } public int getModelYear() { return modelYear; } // Define setPurchasePrice() method public double setPurchasePrice(int userPrice){ return purchasePrice; } // Define getPurchasePrice() method public double getPurchasePrice(int userPrice){ return purchasePrice; }…arrow_forwardis there another way to code this line : public static void main(String[] args) throws FileNotFoundException i haven't learned that in my java class yet.arrow_forward
- please see image for instructions starter code for Main.java import java.io.*;import java.util.*;; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(countConnectedComponents("data.txt")); } public static int countConnectedComponents(String fileName) { try { //the file to be opened for reading FileInputStream fis=new FileInputStream(fileName); Scanner sc=new Scanner(fis); //file to be scanned //returns true if there is another line to read ArrayList<int []> edge = new ArrayList<int[]>(); Set<String> set = new HashSet<String>(); while(sc.hasNextLine()) { int temp [] = new int[2]; int index = 0; for(String s : sc.nextLine().split(" ")) { temp[index] =…arrow_forwardI need this in JAVAarrow_forwardHow do I make: Code 1: import java.util.Scanner; public class ProjectEliza { public static void main(String[] args) { while(true){ Scanner s=new Scanner(System.in); System.out.println("ELIZA: Hello, my name is Eliza. What is your name? "); System.out.print("USER: "); String name=s.next(); System.out.println("ELIZA: Hello, "+name+". Tell me what is on your mind today in 1 sentence."); System.out.print("USER: "); String sentence=s.next(); sentence += s.nextLine(); String[] words = sentence.split("\\s+"); // for (int i = 0, l = words.length; i + 1 < l; i++)// System.out.println(words[i]); //System.out.println("ELIZA: "+question2(words)); System.out.print("USER: "); String exit=s.next(); if(exit.equals("EXIT")){ System.out.println("ELIZA: Do you want to run the session…arrow_forward
- there are 2 errors in this code can you fix it for me? public class Exercise09_10 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a, b, c: "); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); QuadraticEquation equation = new QuadraticEquation(a, b, c); double discriminant = equation.getDiscriminant(); if (discriminant < 0) { System.out.println("The equation has no roots"); } else if (discriminant == 0) { System.out.println("The root is " + equation.getRoot1()); } else // (discriminant >= 0) { System.out.println("The roots are " + equation.getRoot1() + " and " + equation.getRoot2()); } } }arrow_forwardI need help with a Java program described below: import java.util.Scanner;import java.io.FileInputStream;import java.io.IOException; public class LabProgram { public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); /* Type your code here. */ }}arrow_forwardin java Write code that prints: firstNumber … 2 1Print a newline after each number. Ex: If the input is: 3 the output is: 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Scanner; public class ForLoops { publicstaticvoidmain (String [] args) { intfirstNumber; inti; Scannerinput=newScanner(System.in); firstNumber=input.nextInt(); for () { System.out.println(i); } } }arrow_forward
- Given string inputStr on one line and integers idx1 and idx2 on a second line, output "Match found" if the character at index idx1 of inputStr is equal to the character at index idx2. Otherwise, output "Match not found". End with a newline. Ex: If the input is: eerie 4 1 then the output is: Match found Note: Assume the length of string inputStr is greater than or equal to both idx1 and idx2.arrow_forwardimport java.util.Scanner; public class CircleAndSphereWhileLoop{ public static final double MAX_RADIUS = 500.0; public static void main(String[] args) { Scanner in = new Scanner(System.in); // Step 2: Read a double value as radius using prompt // "Enter the radius (between 0.0 and 500.0, exclusive): " // Step 3: While the input radius is not in the ragne (0.0, 500.0) // Display a message on one line (ssuming input value -1) // "The input number -1.00 is out of range." // Read a double value as radius using the same promt double circumference = 2 * Math.PI * radius; double area = Math.PI * radius * radius; double surfaceArea = 4 * Math.PI * Math.pow(radius, 2); double volume = (4 / 3.0) * Math.PI * Math.pow(radius, 3); // Step 4: Display the radius, circle circumference, circle area, // sphere surface area, and…arrow_forwardSolution question number 2arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education