BabyNames how to return only one line - java

I have an assignment where the user is asked for baby name using a scanner. Then it reads through files names.txt and meanings.txt and returns the popularity of the name for each decade ranging from 1890 - 2010 then it prints out the meaning. Some names have multiple meanings and some are used in both genders. The assignment states to print only the first line where the name is found. I am having trouble only returning the first line in which the name is found. PLEASE HELP ME!
import java.io.*;
import java.util.*;
public class BabyNames4 {
public static void main(String[] args) throws FileNotFoundException {
printIntro();
Scanner console = new Scanner(System.in);
System.out.print("Name: ");
String searchWord = console.next();
Scanner fileScan = new Scanner(new File("names.txt"));
String dataLine = find(searchWord, fileScan);
if (dataLine.length() > 0) {
while (dataLine.length() > 0) {
printName(dataLine);
dataLine = find(searchWord, fileScan);
}
}
Scanner fileScan2 = new Scanner(new File("meanings.txt"));
String dataLine2 = find(searchWord, fileScan2);
if (dataLine2.length() > 0) {
while (dataLine2.length() > 0) {
printMeaning(dataLine2);
dataLine2 = find(searchWord, fileScan2);
}
}
}
public static void printIntro() {
System.out.println("This program allows you to search through the");
System.out.println("dada from the Social Security Administration");
System.out.println("to see how popular a particular name has been");
System.out.println("since 1890");
System.out.println();
}
public static String find(String searchWord, Scanner fileScan) {
while (fileScan.hasNextLine()) {
String dataLine = fileScan.nextLine();
String dataLineLC = dataLine.toLowerCase();
if (dataLineLC.contains(searchWord.toLowerCase())) {
return dataLine;
//} else { runs a continuous loop
//System.out.println(search" not found.");
}
}
return "";
}
public static void printName(String dataLine) {
Scanner lineScan = new Scanner(dataLine);
String name = lineScan.next();
String gender = lineScan.next();
String rank = "";
while (lineScan.hasNext()) {
rank += lineScan.next() + " ";
}
System.out.println(name + (" ") + gender + (" ") + rank);
}
public static void printMeaning(String dataLine2) {
Scanner lineScan2 = new Scanner(dataLine2);
String name2 = lineScan2.next();
String gender2 = lineScan2.next();
String language = lineScan2.next();
String meaning = "";
while (lineScan2.hasNext()) {
meaning += lineScan2.next() + " ";
}
System.out.println(name2 + (" ") + gender2 + (" ") + language + (" ") + meaning);
}
}

It looks like sushain hit it with his comment.
The loop:
while (dataLine2.length() > 0) {
printMeaning(dataLine2);
dataLine2 = find(searchWord, fileScan2);
}
could be changed to:
while (dataLine2.length() > 0) {
printMeaning(dataLine2);
break;
}
This way you do not find the second definition and do not print it.

In this loop, you don't need to find the next line, correct?
if (dataLine.length() > 0) {
while (dataLine.length() > 0) {
printName(dataLine);
dataLine = find(searchWord, fileScan); // remove this line
}
}
If you remove the next find to dataLine and remove the while blocks in both instances where you search the file, you won't need a break, and you'll only end up printing one instance.
Do this:
String dataLine = find(searchWord, fileScan);
if (dataLine.length() > 0) {
printName(dataLine);
}

Related

Java programming project

I am in the middle of a university project, the task being to use a scanner to read the appropriate data of several data files. The project involves a superclass and several subclasses. So far the method below works perfectly and reads data corresponding to a class called Tool and all its fields. However I have recently added a subclass ElectricTool which extends class Tool and also which has introduced two new fields which need reading in the same way as before but within the same method shown below. I have tried a number of things but I can't seem to figure it out. Any suggestions? Preferably as clean/simple code as possible, I think it needs to be a read statement but I am struggling. The method is below:
public void readToolData()
{
Frame myFrame = null;
FileDialog fileBox = new FileDialog(myFrame,"Open", FileDialog.LOAD);
fileBox.setVisible(true);
String directoryPath = fileBox.getDirectory();
String fileName = fileBox.getFile();
File dataFile = new File(fileName);
System.out.println(fileName +" "+ directoryPath);
Scanner scanner = null;
try
{
scanner = new Scanner(dataFile);
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
while( scanner.hasNextLine() )
{
String lineOfText = scanner.nextLine().trim().replaceAll("\\s+","");
if(!lineOfText.isEmpty() && !lineOfText.matches("^//.*") && !lineOfText.substring(0,1).equals("["))
{
System.out.println(lineOfText);
}
else{
continue;
}
Scanner scanner2 = new Scanner(lineOfText).useDelimiter("\\s*,\\s*");
while(scanner2.hasNext())
{
Tool tool = new Tool();
tool.readData(scanner2);
storeToolList(tool);
}
}
scanner.close();
}
electric tool class
tool class
data file
public void readToolData() {
Frame myFrame = null
FileDialog fileBox = new FileDialog(myFrame, "Open", FileDialog.LOAD);
fileBox.setVisible(true);
String directoryPath = fileBox.getDirectory();
String fileName = fileBox.getFile();
File dataFile = new File(directoryPath + fileName);
System.out.println(fileName + " " + directoryPath);
Scanner scanner = null;
try {
scanner = new Scanner(dataFile);
} catch (FileNotFoundException e) {
System.out.println(e);
}
// Current tool type
String toolType = null;
while( scanner.hasNextLine() ) {
String lineOfText = scanner.nextLine().trim();
// Skip empty lines and commentaries
if(lineOfText.isEmpty() || lineOfText.startsWith("//")) {
continue;
}
if (lineOfText.startsWith("[")) {
// Extract the tool type name
String withoutBracket = lineOfText.substring(1);
// Split by spaces and take the first word
String[] words = withoutBracket.split(" ");
toolType = words[0];
System.out.println("Reading information about " + toolType);
continue;
}
System.out.println(lineOfText);
Scanner scanner2 = new Scanner(lineOfText).useDelimiter("\\s*,\\s*");
Tool tool = null;
if ("ElectricTool".equals(toolType)) {
tool = new ElectricTool();
}
// In the future here will come more cases for different types, e.g.:
// else if ("HandTool".equals(toolType)) {
// tool = new HandTool();
// }
if (tool != null) {
tool.readData(scanner2);
storeToolList(tool);
}
}
scanner.close();
}
Remove scanner.skip line in Tool.readData:
public class Tool {
public void readData(Scanner scanner) {
toolName = scanner.next();
itemCode = scanner.next();
timesBorrowed = scanner.nextInt();
onLoan = scanner.nextBoolean();
cost = scanner.nextInt();
weight = scanner.nextInt();
scanner.skip(".*"); // Remove this line
}
}
And implement readTool method in ElectricTool:
#Override
public void readData(Scanner scanner) {
super.readData(scanner);
rechargeable = scanner.nextBoolean();
power = scanner.next(); // Or nextInt? what is the type of power field?
}
To print the information about the tools you should use polymorphism.
Modify your printAllTools method in Shop.java like this:
public void printAllTools() {
System.out.println("Information");
System.out.println("---------->");
for (Tool t : toolList) {
System.out.println("You have selected:\n");
t.printDetails();
}
}
Now, your method printDetails in Tool.java must be looking like this:
public void printDetails() {
System.out.println("Tool name: " + toolName + "\n" +
"Item code: " + itemCode + "\n" +
"Times borrowed: " + timesBorrowed + "\n" +
"On load: " + onLoan + "\n" +
"Cost: " + cost + "\n" +
"Weight: " + weight + "g\n"
);
}
and in the ElectricTool.java:
public void printDetails() {
super.printDetails();
System.out.println("Rechargeable: " + rechargeable + "\n" +
"Power: " + power + "\n"
);
}

.equalsIgnoreCase Strings are not comparing correctly

This project is a dictionary program that is editable. Short and sweet i am trying to loop through the words already added to the dictionary and compare the users input with a scanner to the words already added.
My issue is that no matter how accurate the input, the exception i created is always being called.
I am not sure if the exception is wrong or if the strings are not comparing correctly. The dictionary words are read from a file called "dictionary.txt" The program uses input/output if that helps. Here is the code....
Please help!!
import java.util.ArrayList;
public class Dictionary {
ArrayList <Word> Words;
public Dictionary(){
Words = new ArrayList <Word>();
}
public void addWord(String name, String definition){
Word word = new Word(name, definition);
Words.add(word);
}
public String findWord(String name){
String word = "";
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
Words.get(i);
}
word += Words.get(i).getName();
}
return word;
}
public String findDefinition(String name){
String def = "";
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
Words.get(i);
}
def += Words.get(i).getDefinition();
}
return def;
}
public String toString(){
String temp = "";
for (int i = 0; i < Words.size(); i++ ){
temp += Words.get(i).getName() + " - " + Words.get(i).getDefinition() + "\n";
}
return temp;
}
public class Word {
String name;
String definition;
public Word(String name, String definition){
this.name = name;
this.definition = definition;
}
public String getName(){
return name;
}
public String getDefinition(){
return definition;
}
}//End Word
}//End dictionary
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class Read {
private static Scanner in;
public static void main(String[] args) throws FileNotFoundException {
File Read = new File ("Dictionary.txt");
in = new Scanner (Read);
//ArrayList called save
Dictionary save = new Dictionary();
while (in.hasNext()){
String w1 = in.next();
String w2 = in.nextLine();
save.addWord(w1, w2);
}
System.out.println(save);
String newLine = System.getProperty("line.separator");
System.out.println("Press 1 for Menu");
Scanner en = new Scanner(System.in);
int choice = en.nextInt();
while (choice == (1)){
System.out.println("What would you like to do?"
+ newLine + "1 - List all Words and Definitions"
+ newLine + "2 - List definition for word"
+ newLine + "3 - Change definition"
+ newLine + "4 - Add new word"
+ newLine + "5 - Exit");
int input = en.nextInt();
switch(input){
case 1: System.out.println(save);
break;
case 2: System.out.println("Which word definition would you like to see?");
try{
String type = en.next();
if (type.equalsIgnoreCase(save.findWord(type))){
System.out.println("The word you chose is: " + save.findWord(type));
}
else{
throw new InvalidNameException();
}
}
catch (InvalidNameException n){
}
finally {System.out.println("Sorry, that word cannot be found.");
}
break;
case 3: System.out.println("Which word definition would you like to change?");
try{
String def = en.next();
if (def.equalsIgnoreCase(save.findWord(def))){
System.out.println("What will be the new definition for: " + save.findWord(def));
String define = en.next();
save.addWord(def, define);
}
else{
throw new InvalidNameException();
}
}
catch (InvalidNameException n){
}
finally {System.out.println("Sorry, that word cannot be found.");
}
break;
case 4: System.out.println("Enter your new word");
String wrd = en.next();
System.out.println("What is the definition of this word?");
String define = en.next();
save.addWord(wrd, define);
break;
case 5: PrintStream fin = new PrintStream(new File("Dictionary.txt"));
fin.println(save);
break;
}
}
}
}//End Read
public class InvalidNameException extends Exception{
public InvalidNameException() {
System.out.println("Not a valid word");
}
}//End exception
I changed te findWord metod to this:
public Word findWord(String name){
Word word = new Word("","");
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
word = Words.get(i);
}
}
return word;
}
but now there is an issue with the object types in my Read class in this area:
try{
String def = en.next();
if (def.equalsIgnoreCase(save.findWord(def))){
System.out.println("What will be the new definition for: " + save.findWord(def));
String define = en.next();
save.addWord(def, define);
}
Scanner#next() just returns the next token. What you want is Scanner#nextLine().
your findWord/findDef is wrong,
public String findWord(String name){
String word = "";
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
Words.get(i);
}
word += Words.get(i).getName();
}
return word;
}
The if block doesn't actually do anything useful, try moving the line below into your if block (do this for both methods)

How would I change this code to make it so that it asks me the location of the file that I want to load?

So my code currently has the user specify the name of the file that they want to load within the code itself but how would I make it so that when the program is run then the user will enter the location of the file that they want to load?
import java.io.*;
import java.util.*;
public class reader {
static int validresults = 0;
static int invalidresults = 0;
//used to count the number of invalid and valid matches
public static boolean verifyFormat(String[] words) {
boolean valid = true;
if (words.length != 4) {
valid = false;
} else if (words[0].isEmpty() || words[0].matches("\\s+")) {
valid = false;
} else if ( words[1].isEmpty() || words[1].matches("\\s+")) {
valid = false;
}
return valid && isInteger(words[2]) && isInteger(words[3]);}
//checks to see that the number of items in the file are equal to the four needed and the last 2 are integers
//also checks to make sure that there are no results that are just whitespace
public static boolean isInteger( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception e ) {
return false;
}
}
//checks to make sure that the data is an integer
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
int totgoals = 0;
Scanner s = new Scanner(new BufferedReader(
new FileReader("fbscores.txt"))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
while (s.hasNext()) {
String line = s.nextLine();
String[] words = line.split("\\s*:\\s*");
//splits the file at colons
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
totgoals = totgoals + hscore;
ascore = Integer.parseInt(words[3]); //read the away team score
totgoals = totgoals + ascore;
validresults = validresults + 1;
System.out.println(hteam + " " + "[" + hscore + "]" + " " + ateam + " " + "[" + ascore + "]");
//output the data from the file in the format requested
}
else{
invalidresults = invalidresults + 1;
}
}
System.out.println("Total number of goals scored was " + totgoals);
//displays the the total number of goals
System.out.println("Valid number of games is " + validresults);
System.out.println("Invalid number of games is " + invalidresults);
System.out.println("EOF");
}
}
One approach would be to use a main loop asking for a file name and quitting program execution when no input is given.
Therefore I'd refactor most code of your main method into another function e.g. processFile(String fileName).
Then your main only deals with user input
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
while(true){ //keep running till we break
System.out.println("Enter filename or return blank line to quit");
String fileName = sc.nextLine();
if(fileName != null && !fileName.isEmpty()){
processFile(fileName)
}else{
break; //no user input => exit
}
}
System.out.println("bye");
}
private static processFile(String fileName){
String hteam;
String ateam;
int hscore;
int ascore;
int totgoals = 0;
Scanner s = new Scanner(new BufferedReader(
new FileReader(fileName))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
while (s.hasNext()) {
… //rest of your original code
}

Implemented method getting NullPointerException

I have an implemented method which visits the node of a binary tree. I have a word class which implements this through an interface called TreeComparable.
Here is the visit method:
#Override
public void visit() {
System.out.printf("%-15s%-7s", getWord(), count);
pw.printf("%-15s%-7s", getWord(), count);
ObjectListNode p = list.getFirstNode();
while (p != null) {
System.out.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + " ");
pw.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + " ");
p = p.getNext();
}
System.out.println();
pw.println();
}
I am getting errors with the PrintWriter Object. I am getting a nullPointerException. I have a default constructor that is part of the class that is implementing this visit method.
This is the default constructor being used when creating a word object.
public Word(PrintWriter pw) {
this.pw = pw;
}
Everything works if I comment out trying to write to the text file:
#Override
public void visit() {
System.out.printf("%-15s%-7s", getWord(), count);
//pw.printf("%-15s%-7s", getWord(), count);
ObjectListNode p = list.getFirstNode();
while (p != null) {
System.out.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + " ");
//pw.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + " ");
p = p.getNext();
}
System.out.println();
//pw.println();
}
however, for this assignment I need to print out the outputs to a text file. Why is the printwriter object not getting passed in correctly?Thanks for any input!
EDIT:
Here are a few places where I tried to call the constructor:
public class Query {
PrintWriter pw;
public Query(PrintWriter pw) {
this.pw = pw;
}
public void performQuery(ObjectBinaryTree t) {
Scanner userInput = new Scanner(System.in);
System.out.println("Search for word: ");
pw.println("Search for word: ");
Word word = new Word(pw);
String input = userInput.next();
do {
word = new Word(input);
if (t.searchBST(word) != null) {
ObjectTreeNode p = t.searchBST(word);
ObjectListNode q = ((Word) p.getInfo()).getList().getFirstNode();
System.out.printf("%-15s%-5s", ((Word) p.getInfo()).getWord(), ((Word) p.getInfo()).getCount());
pw.printf("%-15s%-5s", ((Word) p.getInfo()).getWord(), ((Word) p.getInfo()).getCount());
while (q != null) {
System.out.print(((LinePosition) q.getInfo()).getLineNumber() + "-" + ((LinePosition) q.getInfo()).getPosition() + " ");
q = q.getNext();
}
System.out.println("\nType 1 to exit, or press enter for new search: ");
pw.println("\nType 1 to exit, or press enter for new search: ");
input = userInput.next();
continue;
}
else
System.out.print("Word Not Found");
System.out.println("\nType 1 to exit, or press enter for new search: ");
pw.print("Word Not Found");
pw.println("\nType 1 to exit, or press enter for new search: ");
input = userInput.next();
continue;
} while (!input.equals("1"));
}
}
and:
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new File("csis.txt"));
Word word = new Word(pw);
Xref xref = new Xref(pw);
Query query = new Query(pw);
xref.readWords();
System.out.println();
pw.println();
query.performQuery(xref.getBinaryTree());
pw.close();
}
}
EDIT:
The visit() method is part of this interface (not sure if this helps but...):
public interface TreeComparable {
int compareTo(Object o);
void operate(Object o);
void visit();
}
The problem is here:
String input = userInput.next();
do {
word = new Word(input);
You should be getting a compile-time error about that, but evidently Word has a constructor that takes a String rather than a PrintWriter, and that constructor isn't setting pw correctly. This assignment is throwing away the (correct) instantiation above:
Word word = new Word(pw);
It looks like you might be misunderstanding what is and isn't a static member of an object. All of the Word objects you instantiate with a PrintWriter carry around a non-null value for pw, but the rest have null unless you fix it. My guess is that your Word(String) constructor ignores pw and expects it to be set by some other call -- it won't.

Getting code error for a simple ChatBot program

I'm getting an error at the 2nd class c.getResponse
The method getResponse(String) is undefined for the type BotTest
If anyone wants to see what the assignment was heres the pdf:
http://www.cs.stonybrook.edu/~tashbook/fall2013/cse110/project-1.pdf
import java.util.*;
public class ChatBot {
public String getResponse(String input) {
int i = 0;
int found = input.indexOf("you", i);
if (found == -1)
return "I'm not important. Let's talk about you instead.";
int x = longestWord(input).length();
if (x <= 3) {
return "Maybe we should move on. Is there anything else you would like to talk about?";
}
if (x == 4) {
return "Tell me more about" + " " + longestWord(input);
}
if (x == 5) {
return "Why do you think" + " " + longestWord(input) + " "
+ "is important?";
} else if (x > 5) {
return "Now we are getting somewhere. How does" + " "
+ longestWord(input) + " " + "affect you the most";
}
else
return "I don't understand";
}
private String longestWord(String input) {
String word, longestWord = "";
Scanner turtles = new Scanner(input);
while (turtles.hasNext()) {
word = turtles.next();
if (word.length() > longestWord.length())
longestWord = word;
}
return longestWord;
}
}
Second Class to test the code
import java.util.*;
public class BotTest {
public static void main(String[] args) {
Scanner newturtles = new Scanner(System.in);
System.out.println("What would you like to talk about?");
String input = newturtles.nextLine();
BotTest c = new BotTest();
while (!input.toUpperCase().equals("GOODBYE")) {
System.out.println(c.getResponse(input));
input = newturtles.nextLine();
}
}
}
getResponse is defined for ChatBot not BotTest
ChatBot c = new ChatBot();
The Class BotTest indeed does NOT have the .getResponse(String) function. ChatBot does though.

Categories

Resources