Question
(2) get all the quotes by an author entered by the user if they exist, (3) get all the quotes containing a search phrase entered
(2) get all the quotes by an author entered by the user if they exist, (3) get all the quotes containing a search phrase entered by the user,
when I try to hit 2 I get this error :
Exception in thread "main" java.lang.NullPointerException
at quotes.ReadQuotes.main(ReadQuotes.java:53)
Enter an author name :C:\Users\user\AppData\Local\NetBeans\Cache\8.2rc\executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 1,395 minutes 41 seconds)
The next error I get is when I click 3
Exception in thread "main" java.lang.NullPointerException
Enter a pharse : at quotes.ReadQuotes.main(ReadQuotes.java:63)
C:\Users\user\AppData\Local\NetBeans\Cache\8.2rc\executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
heres my code,
import java.io.*;
import java.util.*;
public class ReadQuotes { public static void main(String args[]) {
int choice; String author;
Console c=System.console();System.out.println("Random Quote is :");
randQuote();
while(true) {
System.out.println("Menu"); System.out.println("******************* "); System.out.println("(1) get another random quote "); System.out.println("(2) get all the quotes by an author entered by the user if they exist"); System.out.println("(3) get all the quotes containing a search phrase entered by the user");
System.out.println("(4) exit.");
//creating object of the Scanner class
Scanner sc = new Scanner(System.in);
//reading input from the user
System.out.print("Enter a number ( 1 - 4 ):");
choice = sc.nextInt();
else if (choice == 2)
{
System.out.print("Enter an author name :");
author = c.readLine();
System.out.println("Quote by Author : "+author);
// Calling getQuotebyAuthor() to genearte display quote by author name
getQuotebyAuthor(author);
//c.close();
}
else if (choice == 3)
{
System.out.print("Enter a pharse :");
String pharse = c.readLine();
System.out.println("Quote by pharse : "+pharse);
// Calling getQuotebyAuthor() to genearte display quote by pharse
getQuotebyPharse(pharse);
}
else if (choice == 4)
{
System.out.println("exit");
break;
}
else
{
System.out.println("Invalid Option..");
}
}
}
// getQuotebyAuthor() definition
static void getQuotebyAuthor(String author)
{
try
{
//file to be opened in reading mode
FileInputStream fs=new FileInputStream("quotes.txt");
Scanner sc=new Scanner(fs);
//Iterates each line upto end of the line
while(sc.hasNextLine())
{
String line = sc.nextLine();
// using contains()
boolean result = line.contains(author);
if(result) {
System.out.println(line);
}
}
//closes the scanner object
sc.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
// getQuotebyPharse() definition
static void getQuotebyPharse(String pharse)
{
try
{
//file to be opened in reading mode
FileInputStream fs=new FileInputStream("quotes.txt");
Scanner sc=new Scanner(fs);
//Iterates each line upto end of the line
while(sc.hasNextLine())
{
String line = sc.nextLine();
// using contains()
boolean result = line.contains(pharse);
if(result) {
System.out.println(line);
}
}
//closes the scanner object
sc.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
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