Question:
Java Question: Need help?
Humanity
2013-05-17 15:46:25 UTC
b. Modify the “BabyNames.java” so it will do the following:
i. The “names.txt” file contains data about popular baby names over the last 80 years in the United States (http://www.ssa.gov/OACT/babynames). Every 10 years, the data gives the 1000 most popular boy names and girl names for children born in the US. The data can be summarized in the following format:
...
Sam 99 131 168 236 278 380 467 408 466
Samantha 0 0 0 0 272 107 26 5 7
Samara 0 0 0 0 0 0 0 0 886
Samir 0 0 0 0 0 0 920 0 798
...
Each line has a name followed by the rank of that name in 1920, 1930, …, 2000 (9 numbers). A rank of 1 was the most popular name for that decade, while a rank of 907 was not that popular. A 0 means that the name was out of the 1000 popular names for that decade.
ii. Your program will give an introduction and then prompt the user for a name. Then it will read through the data file searching for that name. The search should be case-insensitive, meaning that you should find the name even if the file and the user use capital letters in different places. As an extreme example, SaMueL would match sAMUel.
iii. If your program finds the name, it should print out the statistics for that name onto screen. You are to reproduce this format exactly:
** Popularity of a baby name since year 1920 **
name? ada
1920: 154
1930: 196
1940: 244
1950: 331
CS 210 W. Li Assignment 6
1960: 445
1970: 627
1980: 962
1990: 0
2000: 0
Then, you need to save the statistics into a result file for that baby name. For the example above, the result file should be named as “Ada.txt”, which should contain contents in the following format exactly:
Ada,
1920: 154,
1930: 196,
1940: 244,
1950: 331,
1960: 445,
1970: 627,
1980: 962,
1990: 0,
2000: 0
iv. If the name is not in the data file, your program should generate a short message onto screen indicating the name was not found. You are to reproduce this format exactly:
** Popularity of a baby name since year 1920 **
name? kumar
name not found.

HOW DO I CHECK IF NAME EXIST, AND CONTINUE ONLY IF THE NAME EXIST, BELOW ARE MY CODES:

public class BabyNames
{
public static void main(String[] args)
throws FileNotFoundException
{

Scanner input = new Scanner(new File("names.txt"));
Scanner console = new Scanner(System.in);
Boolean bool = false;

System.out.println("** Popularity of a baby name since year 1920 **");
System.out.print("name? ");
String given = console.next();

PrintStream out = new PrintStream(new File(given+".txt"));
out.println(given + ",");
results(given, out, input);
}

public static void results(String given, PrintStream out, Scanner input)
{

while (input.hasNextLine())
{
String name = input.nextLine();
Scanner lnScan = new Scanner(name);
String line = lnScan.next();
line.toString();
int x = -1;

while (line.equalsIgnoreCase(given) && x <= 7)
{
int pop = lnScan.nextInt();
x++;
int year = 1920 + (10 * x);
System.out.println(year + ": " + pop);

if (x<=7)
{
out.println(year + ": " + pop + ",");
}

else if (x <= 8)
{
out.println(2000 + ": " + pop);
}
}
}
}

}
Three answers:
John
2013-05-18 01:17:30 UTC
//HOW DO I CHECK IF NAME EXIST



public class BabyNames {

private static String fileInPath = "D:/code/java/names.txt";

private static String fileOutPath = "d:/code/java/";



public static void main(String[] args) {

String name = read("name? ");

NameInfo info = null;

try {

info = find(fileInPath, name);

} catch (IOException ex) {

ex.printStackTrace();

}



// AND CONTINUE ONLY IF THE NAME EXIST

if (info != null) {

System.out.println(print(info));

try {

String fileName = fileOutPath + info.getName() + ".txt";

write(fileName, print(info));

} catch (FileNotFoundException ex) {

ex.printStackTrace();

}

} else {

System.err.print("name not found");

}



System.exit(0);

}



private static NameInfo find(String filePath, String name) throws IOException {

NameInfo info = null;

Scanner fileIn = new Scanner(new FileReader(filePath));

while (fileIn.hasNextLine()) {

String[] fields = fileIn.nextLine().split(" ");

if (fields[0].equalsIgnoreCase(name)) {

//

}

}

fileIn.close();

return info;

}

}
shelby
2016-12-16 10:38:32 UTC
Babynames.java
anonymous
2016-03-18 08:32:24 UTC
for(i=2;i<21;i++) { system.out.print(i+": "); for(j=1;j<(i/2)+1;j++) { if(i%j == 0) system.out.print(j+" "); system.out.println(""); }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...