Hey guys,
First time poster so quick background on my project. I've been asked to make a program that accesses a file and outputs what you've searched for. The file contains a list of the worlds capitols and their population. If you enter "North" it will spit out anything with that word in it, obviously. Problem is if there is no search result it does nothing...I want it to be able to display a message saying "Sorry, no results were found" but don't know where to start apart from the fact I know it would be a boolean varibale and an if statement in the end..guidance would be greatly appreciated
Code was written in Bluej
Source:
__________________________________________________ __________________________________import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Capitals { public static void main(String[] args) throws FileNotFoundException { // ask the user for the search string Scanner keyboard = new Scanner(System.in); System.out.print("Please enter part of the country name: "); String searchString = keyboard.next().trim().toLowerCase(); // open the data file File countryFile = new File("CountryData.csv"); // create a scanner from the file Scanner countryInput = new Scanner(countryFile); // set up the scanner to use "," as the delimiter countryInput.useDelimiter("[\\r,]"); // read one line of data at a time, processing each line while(countryInput.hasNext()) { // read the 3 parts of the line // first the country and capital String country = countryInput.next(); String capital = countryInput.next(); // then the population info int population = countryInput.nextInt(); // print out the info if the country name contains the input search string if(country.toLowerCase().contains(searchString)) { System.out.println(country + "\t" + capital + "\t" + population); } } // be polite and close the file countryInput.close(); } }
Thank you in advance