2021年3月20日星期六

Count input without spaces, commas, or periods - Java

import java.util.Scanner;    public class LabProgram {     public static void main(String[] args) {        Scanner scnr = new Scanner(System.in);        String userText;        int numPun = 0; //number of punctiation and spaces        // Add more variables as needed             userText = scnr.nextLine();  // Gets entire line, including spaces.          for (int i = 0; i < userText.length(); i++){          if ((userText.charAt(i) != ' ') && (userText.charAt(i) != ',') && (userText.charAt(i) != '.'));{             numPun++;          }       }       System.out.println(numPun);     }  }  

My current output is to 29, which is the total amount of characters in the whole line. Expected output is supposed to be 21. Where am i going wrong here?

https://stackoverflow.com/questions/66727954/count-input-without-spaces-commas-or-periods-java March 21, 2021 at 09:05AM

没有评论:

发表评论