I am trying to calculate the percentage of valid inputs of words. I'm stuck and every method I've tried doesn't work. I was starting learning java two months ago,so i am new in this and i am not shure if I have done the right code. Could someone give me some advice on how to word it.Thanks in Advance
public class Subject3
{
public static void main (String[]args) {
Scanner scan = new Scanner(System.in);
//Creating scanner object
boolean valid = true;
int numOfStrings=0;
do {
valid = true;
System.out.print("How many strings?: ");
try{
numOfStrings = Integer.parseInt(scan.nextLine());
}catch (NumberFormatException e){
System.out.println("Not a word");
valid = false;
}
}while (!valid);
String[] stringPali = new String [numOfStrings];
String input;
for (int i=1; i<numOfStrings 1 ; i ) {
do {
valid = true;
System.out.print("Enter string no." i );
System.out.print(":");
input = scan.nextLine();
if (!input.matches("[A-Za-z0-9] ")){
System.out.println("Not a word");
}
}while (!valid);
}
System.out.println("Results");
System.out.println("Total number of strings: " numOfStrings);
System.out.println("Percentage of words:" (percentage)("%"));
System.out.println("Words starting with capital letter: " ("%"));
}
}
CodePudding user response:
System.out.println("Not a word"); this meesage is wrong. it should be not a number.
You created an array stringPali but never used it? why?
loops starting with 1 is confusing. But it's up to you.
You should first add the words to the array finally you can count. You need the full user input to calculate percentage. So no point of counting valid strings while user is still entering input. Do it at the end.
You can use regex to match the strings that only contain letters and the strings that start with a capital letter.
import java.util.Scanner; public class ExampleCase { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Creating scanner object boolean valid = true; int numOfStrings = 0; do { valid = true; System.out.print("How many strings?: "); try { numOfStrings = Integer.parseInt(scan.nextLine()); } catch (NumberFormatException e) { System.out.println("Not a number"); valid = false; } } while (!valid); String[] stringPali = new String[numOfStrings]; for (int i = 0; i < numOfStrings; i ) { System.out.print("Enter string no." i); System.out.print(":"); String input = scan.nextLine(); stringPali[i] = input; } System.out.println("Results"); System.out.println("Total number of strings: " numOfStrings); int validStringCount = countStrings(stringPali, "^[a-zA-Z]*$"); double percentage = (double) validStringCount / numOfStrings; System.out.println(String.format("Percentage of words: %f%%", percentage)); int startWithCapitalCount = countStrings(stringPali, "^[A-Z].*"); percentage = (double) startWithCapitalCount / numOfStrings; System.out.println(String.format("Words starting with capital letter: %f%%", percentage)); } private static Integer countStrings(String[] stringPali, String pattern) { int count = 0; for (String str : stringPali) { if (str.matches(pattern)) { count ; } } return count; } }