I need to use a boolean variable to keep track of whether any countries were found, and then check this with an “if”
statement at the end within this code.
import java.io.*; import java.util.Scanner; /** * Write a description of class capital here. * * @author (your name) * @version (a version number or a date) */ public class capital { 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(); } }
I am not looking for advice. Im looking for a solution because I am very stuck. I know if I put an else statement under this part of the code
{
System.out.println(country + "\t" +
capital + "\t" + population);
}
it puts the text I want, but repeats it. So it kinda did what i want. Other than that, im completely stuck. So no asking me to check my code and stuff, just a solution in code form
thankyou