简体   繁体   中英

Java do/while loop problem? How to count the string inside the loop

Here is the problem I was given: Write a program that takes website names as keyboard input until the user types the word 'stop' also count how many of the website names are commercial website names (ie, end with .com), and output that count.

I can't figure out how to count the commercial website inside the loop and how to keep adding it if the user keeps adding more.

This is my code

public class WebsiteNames {
    public static void main(String[] args) {

        //variables
        int commercialWebsite = 0;
        String commercialNames = "com";
        String website;
        final String SENTINEL = "stop";

        //scanner
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter a website url ");
        website = scan.next();

        //lenght of the website
        String substring = website.substring(website.length() - 3);

        do {
            System.out.print("Please enter a website url ");
            website = scan.next();
            substring = website.substring(website.length() - 3);

        } while (!SENTINEL.equals(website));
        System.out.println("There were " + commercialNames + " commercial websites entered");

    }
}
import java.util.Scanner;

public class WebsiteNames {

    public static void main(String[] args) {
        // variables
        int commercialWebsite = 0;
        String commercialNames = ".com";
        String website;
        final String SENTINEL = "stop";

        // scanner
        Scanner scan = new Scanner(System.in);

        do {
            System.out.print("Please enter a website url (or 'stop' to terminate): ");
            website = scan.nextLine();
            if (website.endsWith(commercialNames)) {
                commercialWebsite++;
            }
        } while (!SENTINEL.equals(website));

        System.out.println("There were " + commercialWebsite + " commercial websites entered");
    }
}
  • Use method nextLine instead of method next() .
  • Use method endsWith rather than method substring() since you are looking for Web sites whose names end with .com .
  • You are using a do-while loop so no need to ask the user for input before entering the loop.
  • Increment the counter, ie commercialWebsite if the entered Web site ends with the desired suffix.

i have done this with for loop.Its working as your requirements.

public static void main (String[] args) {
  Scanner scan = new Scanner(System.in);
    int count=0;
    System.out.println("Enter website names:");
    String websites = scan.nextLine();
    String com = "com";
    String[] website = websites.split(" ");
   for (int i=0;i<website.length ;i++ ) 
    {
        if(website[i].substring(website[i].length()-3).equals(com)){
            count++;
        }  
        System.out.println("Names "+website[i]);
    }
    System.out.println("website Names="+websites);
    System.out.println("count "+count);
}

A simple way to achieve this is to use string.contains()

Example for your application with some changes by my side:

 String website;
    while(true)
    {
        System.out.print("Please enter a website url ");
        website = scan.nextLine();
        if(website.equalsIgnoreCase("stop"))
            break; // Go out of the loop

        if(website.contains(".com"))// if it contains the .com we increment the counter
            commercialWebsites++;

    }

    System.out.println("There were " + commercialWebsites + " commercial websites entered" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM