Issue with detecting an empty index in a Java char array - java

I am currently working on a project where I am running into an issue with detecting an empty index in a char array. The issue is that it doesn't detect whether or not there is an empty index in the array. I have tried detecting different things people have said where the placeholders for an empty char but none of them seem to work.
Here are the different things I have tried:
0, '0', 'u0000', '\u0000\, and null.
public class TestArrayChecker {
public static void main(String[] args) {
char array1[] = new char[] {'F', 'P', 'S', 'R'};
char array2[] = new char[] {'S', 'P', 'O', 'R'};
char c1 = '\u0000';
char arrayOfCorrect[] = new char[array1.length];
int correct = 0;
int counter = 0;
int close = 0;
int index = 0;
for (int i = 0; i < 4; i++) {
if (getColorAt(i, array1) == getColorAt(i, array2)) {
correct++;
arrayOfCorrect[index] = getColorAt(i, array1);
index++;
}
}
for (int i = 0; i < 4; i++) {
for (int n = -i; n < 4 - i; n++) {
if(getColorAt(i, array1) == getColorAt(i + n, array2)) {
for (int f = 0; f < arrayOfCorrect.length; f++) {
System.out.println(getColorAt(f, arrayOfCorrect) + " " + f);
System.out.println(arrayOfCorrect.length);
System.out.println(getColorAt(f, arrayOfCorrect) != c1);
if (getColorAt(f, arrayOfCorrect) != c1) {
System.out.println(getColorAt(f, arrayOfCorrect) + " " + f);
System.out.println("No Void in this array");
if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
counter++;
break;
}
} else {
counter++;
System.out.println("We got here!");
}
if (counter == 4) {
close++;
}
}
}
}
}
System.out.println(arrayOfCorrect[0] + " " + arrayOfCorrect[1] + " " + arrayOfCorrect[2] + " " + arrayOfCorrect[3]);
System.out.println(correct);
System.out.println(close);
System.out.println(counter);
}
public static char getColorAt(int index, char array[]) {
// TODO: Fill-in code to return the color at a particular position
return array[index];
}
}
This is the main part of the code that is not working:
if (getColorAt(f, arrayOfCorrect) != c1) {
System.out.println(getColorAt(f, arrayOfCorrect) + " " + f);
System.out.println("No Void in this array");
if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
counter++;
break;
}
} else {
counter++;
System.out.println("We got here!");
}
Here is the output of the console, the last two numbers are supposed to print out 1 and 4.
P 0
4
true
P 0
No Void in this array
R 1
4
true
R 1
No Void in this array
This number is close: R
P 0
4
true
P 0
No Void in this array
This number is close: P
P 0
4
true
P 0
No Void in this array
This number is close: P
P R
2
0
3
I haven't removed all of my debug coding so please ignore that.
Thanks all for the help.

hmmm, okay. First of all, the issue isn't in detecting an empty char, I've tested it myself with this code :
public static void main(String[] args) {
char array[] = new char[10];
int emptyCounter = 0;
for(int i = 0; i < array.length; i++) {
if (array[i] == 0) {
emptyCounter++;
}
}
System.out.println(emptyCounter);
}
and I get an output of 10, as I left all chars empty.
Now, looking at your code, we can see why "getColorAt(f, arrayOfCorrect) != c1" will always be true : you break out of the for loop before it can encounter an empty string. You've got "arrayOfCorrect = ['P', 'R', empty, empty].
This is the part of your code that makes it so :
if(getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)) {
System.out.println("This number is close: " + getColorAt(f, arrayOfCorrect));
counter++;
break;
}
As you iterate through array1 with i, the condition is true when i is equal to 0 and 2. As we can see by your results, f is never going higher than 1 but is not the source of the problem.
As we go higher to get to the source of the problem, we come across this :
for (int n = -i; n < 4 - i; n++) {
if(getColorAt(i, array1) == getColorAt(i + n, array2)) {
...
}
}
Btw, this isn't a good practice... this is like doing 2 + 10 - 10 + 2 = 4. As far as I can see, this would do the same work :
for (int n = 0; n < 4; n++) {
if(getColorAt(i, array1) == getColorAt(n, array2)) {
...
}
}
And it is now more easy to understand what you are trying to do here.
So, for every char in array1, you check every char of array2 until you find a correspondence. So basically, the condition will be true only 3 times : when array1[1] = array2[1], array1[2] = array2[0] and array1[3] = array2[3].
When i = 1, "getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)" is true when f = 1 ('P' != 'R') and you exit the nested loop there.
When i = 2, "getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)" is true when f = 0 ('S' != 'P') and you exit the nested loop there.
When i = 3, "getColorAt(i, array1) != getColorAt(f, arrayOfCorrect)" is true when f = 0 ('R' != 'P') and you exit the nested loop there.
So yeah, counter++ gets called only 3 times and you never encounter an empty char.
Hope this helps.

Related

Printing out numbers with only specific digits

I'm trying to print out the numbers that are below a specific number entered as a command line argument (e.g 430) that contain specific digits (e.g 2 and 3).
So that my program prints only numbers containing 2 and 3 and are below 430, so the answer would be : 2,3,23,32, etc.
I've written a piece of code but for some reason I can't get it to work.
Any help is appreciated !
Here's my code:
public static void main(String[] args) {
int input = Integer.parseInt(args[0]);
for(int i=0; i<input; i++) {
String test= Integer.toString(i);
for(int j=0; j<test.length(); j++) {
if((test.charAt(j) != '2') || (test.charAt(j)!='3')) {
}
else {
System.out.println("The digit is " + i);
}
}
}
}
You'll never reach the else block.
if((test.charAt(j) != '0')
|| (test.charAt(j)!='1')) {
}
Should be:
if((test.charAt(j) != '0')
&& (test.charAt(j)!='1')) {
}
Here is working code. In your code, why are you checking for 0 and 1 instead of 2 and 3.
public static void main(String[] args) {
int input = Integer.parseInt(args[0]);
int two = 0, three = 0;
for (int i = 0; i < input; i++) {
String test = Integer.toString(i);
if (i < 10 && (test.equals("2") || test.equals("3"))) {
System.out.println("The digit is " + i);
} else {
for (int j = 0; j < test.length(); j++) {
if (test.charAt(j) == '2') {
two++;
} else if ((test.charAt(j) == '3')) {
three++;
}
}
if (two >= 1 && three >= 1) {
System.out.println("The digit is " + i);
}
two = 0;
three = 0;
}
}
}

in java to test if the brackets in a formula is matching, is my algorithm right?

Here is my analysis to this problem: There are four kinds of conditions where the brackets is matching: {{()}}, {}[]<>, <{}[]>, {<>[]}<>
So it could be complicated if I just think about these 4 matching forms. So I try to find out when is the brackets is not matching. if I let { and } be a pair, I find out if one bracket is on the odd position then his pair must be in a even position, vice versa. Take {<>[]}<> as an example, { is at the 1st position which is an odd position and } is at the 6th position which is an even position. Therefore I use numbers to mark them which '()'--1,9; '[]'--2,8; '<>' --3,7; '{}' --4,6 so if two number adds up is equals to 10 then these two numbers represents a pair. Then I use those numbers to represent bracket structure. and I pull out bracket in odd position and bracket in even position(use number to represent them) and I add each items in odd position and even position with each other to see if there is a match which adds up is 10, if not, I say it's a match. My code is as below:
/** Matching Brackets
* Tony
*/
import java.util.*;
public class Solution19 {
public static String process(String n) {
/** build a condition combination: */
String newString = "";
for (int i = 0; i < n.length(); i++) {
if (n.charAt(i) == '(' || n.charAt(i) == ')' || n.charAt(i) == '[' || n.charAt(i) == ']'
|| n.charAt(i) == '<' || n.charAt(i) == '>' || n.charAt(i) == '{' || n.charAt(i) == '}') {
newString += n.charAt(i);
}
}
return newString;
}
public static String numForm(String s) {
String newone = "";
for (int i = 0; i < s.length(); i++) {
switch(s.charAt(i)) {
case '(': newone += "1 ";break;
case ')': newone += "9 ";break;
case '[': newone += "2 ";break;
case ']': newone += "8 ";break;
case '<': newone += "3 ";break;
case '>': newone += "7 ";break;
case '{': newone += "4 ";break;
case '}': newone += "6 ";break;
}
}
return newone;
}
public static int[] intArray(String m) {
String[] stringArray = m.split(" ");
int[] intArr = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
intArr[i] = Integer.parseInt(stringArray[i]);
}
return intArr;
}
public static void printArray (int[] array) {
for (int n : array) {
System.out.print(n + " ");
}
}
public static int[] oddPosition (int[] array) {
int [] oddNumbers = new int[array.length / 2];
int j = 0;
for (int i = 0; i < array.length; i++) {
if ((i + 1) % 2 != 0) {
oddNumbers[j] = array[i];
j ++;
}
}
return oddNumbers;
}
public static int[] evenPosition (int[] array) {
int [] evenNumbers = new int[array.length / 2];
int j = 0;
for (int i = 0; i < array.length; i++) {
if ((i + 1) % 2 == 0) {
evenNumbers[j] = array[i];
j ++;
}
}
return evenNumbers;
}
public static boolean addsUpten (int [] array) {
boolean conditionSum = false;
boolean conditionSingle = false;
for (int i = 0; i < array.length; i++) {
int d = 0;
while (i + d < array.length) {
if (array[i] + array[i+d] == 10) {
conditionSingle = true;
}
conditionSum = (conditionSum || conditionSingle);
d ++;
}
}
return conditionSum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
String voider = sc.nextLine();
for (int i = 0; i < times; i ++) {
String formula = sc.nextLine();
String processed = process(formula);
String numFormed = numForm(processed);
// System.out.println(numFormed);
int[] numArray = intArray(numFormed);
if (numArray.length % 2 != 0) {
System.out.print("0 ");
}
else {
int[] oddNumbers = oddPosition(numArray);
int[] evenNumbers = evenPosition(numArray);
if (addsUpten(oddNumbers) || addsUpten(evenNumbers) == true) {
System.out.print("0 ");
}
else {
System.out.print("1 ");
}
}
}
}
}
as I expected, it should work and it does work when I input:
4
(a+[b*c]-{d/3})
(a + [b * c) - 17]
(((a * x) + [b] * y) + c
auf(zlo)men [gy<psy>] four{s}
it gives me output:1 0 0 1 (1 represent it is a match, 0 represent it's not a match). However when I input something longer like [^]<t>(z){<[^]<w>[{f}c]y[-][v]{<y>g<+( )>(c){w{a{t}}}}>((a)w)} it is a match but it gives me 0. I wonder the way I determine if it's a match or not is right or wrong? What did I miss? Sorry for the long code, just wondering ("I find out if one bracket is on the odd position then his pair must be in a even position, vice versa.") this way to describe a bracket match is right or wrong? Thx!
Your approach has a fundamental problem - it does not support nesting very well.
You can solve this by creating a stack of parentheses:
When you see an opening parenthesis, you push it on the stack
When you see a closing parenthesis, you pop an opening parenthesis that is supposed to pair with it off the stack
If the pair from the stack matches, continue
If the pair does not match, or the stack is empty, report a mismatch
If the stack is not empty at the end of the loop, also report a mismatch.

Finding Consecutive Duplicate integers in an array

I have a problem in which I need to ask for user input for how many times they wish to roll a die and to create and print that an array that has the rolls requested. So far I can create the array, however another part of the problem is that whenever there are consecutive duplicate rolls I must put parentheses around them. For example inputting 11, creates the array
{1 , 2 , 1 , 4 , 4, 6 , 2 , 3 , 5 , 5 , 5} would print 1 2 1 ( 4 4 ) 6 2 3 ( 5 5 5 )
So far I have written
import java.util.Scanner;
import java.util.Random;
public class HW0603 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many times would you like to roll: ");
System.out.println();
int x = input.nextInt();
run(rolls(x), x);
}
public static int[] rolls(int x) {
Random random = new Random();
int y[] = new int[x];
for (int i = 0; i < x; i++) {
int z = random.nextInt(6) + 1;
y[i] = z;
}
return y;
}
public static void run(int a[], int b) {
for (int i = 1; i < b; i++) {
System.out.print(a[i] + " ");
}
}
}
As for the parentheses I honestly don't know how to start. Using if statements didn't work for me, my if statement variations seem to give me out of bound errors since I compare a[i] to a[i+1] and a[i-1]. Could anyone give me a place to start or some tips to being extracting consecutive duplicates?
you need to compare current item with next item
if equal, print "(" then print the item
make flag paranOpened that you have opened (, so you don't reopen ( again, to avoid this: 1 (2(2(2..., then when curr!=next, based on that flag either print the item or print the item then close the ")"
at end of loop
print lat item (b-1) that was excluded from the loop ..;i < b - 1;.., and check if you have opened "("
your run() method will be like this
static boolean paranOpened = false;
public static void run(int a[], int b) {
for (int i = 0; i < b - 1; i++) {
if (a[i] == a[i + 1]) {
if (!paranOpened) {
paranOpened = true;
System.out.print(" (");
}
System.out.print(a[i] + " ");
} else {
System.out.print(a[i] + " ");
if (paranOpened) {
System.out.print(") ");
paranOpened = false;
}
}
}// for loop
// print last item in array #(b-1)
System.out.print(a[b - 1] + " ");
// check if opened ( , then close it
if (paranOpened) {
System.out.print(") ");
}
}// run()
this is a quick solution, there could be better algorithms
The first problem with you program is that the counter in your run method starts
from 1 which should be zero. Your current program does not print the first element of the array.
then you need to check each element with the next one to see if they are duplicate and if they are open the parenthesis and vice versa.
The last element does not need to be checked so print it outside the loop and close the parenthesis if needed.
By the way you do not need to pass the array size element with it. Just use the array.length method.
public static void run(int a[], int b)
{
boolean pOpen = false;//keep track if parenthesis is open
for (int i = 0; i<a.length; i++)
{
if (i < a.length-1)//prevent out of bound exception
{
if (a[i] == a[i+1] && !pOpen )// check if it is needed to `open or close the parenthesis`
{
System.out.print("(");
pOpen = true;
}
System.out.print(a[i] + " ");
if (a[i] != a[i+1] && pOpen)
{
System.out.print(")");
pOpen = false;
}
}
}
System.out.print(a[a.length-1]);//print the last element
if (pOpen)//close the parenthesis if open
{
System.out.print(")");
}
}
Iterate through your array and keep a boolean that marks if parenthesis have opened.
import java.util.*;
class Ideone
{
public static int[] rolls(int x) {
Random random = new Random();
int y[] = new int[x];
for (int i = 0; i < x; i++) {
int z = random.nextInt(6) + 1;
y[i] = z;
}
return y;
}
public static void run(int a[], int b) {
StringBuilder sb = new StringBuilder();
String out = "";
boolean parens = false;
for (int j = 0; j < a.length; j++)
{
out = "" + a[j]; //by default just add an element
//check for duplicate and build parenthesis
if (j + 1 < a.length && a[j] == a[j+1]) //duplicate found
{
if (!parens) // if no parenthesis
{
parens = true; //start parenthesis
out = "( " + a[j];
}
}
else
{
if (parens) //if parenthesis already started
{
out = a[j] + " )";
parens = false; //stop parenthesis
}
}
sb.append(" " + out);
}
// if the last element occured multiple times
if (parens) //should end parens
{
sb.append(a[a.length-1] + " )");
}
//print out the result
System.out.println(sb.toString());
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(System.in);
System.out.print("How many times would you like to roll: ");
System.out.println();
int x = input.nextInt();
run(rolls(x), x);
}
}
You need to use boolean to check whether your parenthesis is open or no.
Here I've tried to create a clean and readable example:
Sample code:
public class HelloWorld {
public static void main(String[] args) {
int arr[] = { 1, 2, 1, 4, 4, 6, 2, 3, 5, 5, 5 };
printConsecutiveInBrace(arr);
}
public static void printConsecutiveInBrace(int arr[]) {
int printFrom = 0;
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1 || arr[i] != arr[i + 1]) {
print(arr, printFrom, i);
printFrom = i + 1;
}
}
}
public static void print(int arr[], int printFrom, int printTo) {
if (printFrom < printTo) //Here check: Consecutive Duplicate
System.out.print("( ");
for (int i = printFrom; i <= printTo; i++)
System.out.print(arr[i] + " ");
if (printFrom < printTo)
System.out.print(") ");
}
}
Output:
1 2 1 ( 4 4 ) 6 2 3 ( 5 5 5 )

Unable to identify nzec error in code

I was challenged with the following question from a coding site :
==================================================================================
Given K prime numbers and T queries of form Ai, Bi, for each query print the number of integers between Ai and Bi (both inclusive) that are divisible by atleast one of the K given primes.
Input
First line: K and T.
Second line: K primes.
Next T lines, each contain Ai, Bi.
Output
Print T lines, denoting the answer to each of the T queries.
Constraints
1 ≤ K ≤ 10
1 ≤ T ≤ 100
1 ≤ A ≤ B ≤ 109
Each prime ≤ 107
Sample Input (Plaintext Link)
2 1
2 3
1 10
Sample Output (Plaintext Link)
7
Explanation
2,3,4,6,8,9,10 are the 7 numbers.
=======================================================================
When I run the following code (in system compiler) it works well, but when I submitted it to the site, all test cases throw nzec runtime error. Unfortunately the site does not share test cases but they can be created using the question.
Can any one please explain why there is a nzec error? I have used java so code is throwing an exception that needs to be caught:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner scan = new Scanner(System.in);
String[] line1 = scan.nextLine().split(" ");
int fPrime,first,last,count=0,cur;boolean duplicate=false;
int K = Integer.parseInt(line1[0]);
int T = Integer.parseInt(line1[1]);
int[][] num=new int[2][T];
// int arr[];
String[] line2 = scan.nextLine().split(" ");
if(T<=100 )
{
for(int k=0;k<T;k++)
{
if(scan.hasNextLine())
{
String[] line3 = scan.nextLine().split(" ");
num[0][k] = Integer.parseInt(line3[0]);
num[1][k] = Integer.parseInt(line3[1]);
// System.out.print("num0 = " + num[0][k]);
// System.out.println("num1 = " + num[1][k]);
}
else
{
System.out.println("Lines of Ai and Bi are missing. Make sure T lines exist");
}
}
}
else
{
System.out.print("Error! T>100");
}
if(T<=100)
{
for(int l = 0; l < T; l++)
{
first = num[0][l];
last = num[1][l];
int arr[] = new int[last];
cur=0;
// System.out.println("first = " +first);
// System.out.println("last = " +last);
for(int i = first; i<=last; i++ )
{
for(int j = 0; j < K; j++)
{
fPrime = Integer.parseInt(line2[j]);
// System.out.println("fPrime = " +fPrime);
if(( fPrime<=1000000) && (first<=1000000000 && last<=1000000000)){
if(i%fPrime==0)
{
// System.out.println("Gotcha num " + i);
for(int a = 0; a < arr.length; a++)
{
if (arr[a] == i)
{
duplicate=true;
// System.out.print("arr"+a+" = " + i);
}
}
if(duplicate==true)
{
duplicate=false;
}
else
{
arr[cur++]=i;
count++;
}
}
}
else
{
System.out.println("Make Sure 1 ? A ? B ? 10^9 Each prime ? 10^7" );
}
}
}
System.out.println(count);
}
}
else System.out.println("T can not be greater than 100");
}
}

Count vowels in a list of strings and print the counts

I have a code that must print only vowels from my strings in the array list but I'm not sure if I'm doing it right in my method. How do I resolve this? Its only printing out 5 of them because I'm not sure how to directly get each specific vowels. Please find the below code that I have tried.
import java.util.*;
public class vowels {
public static void main(String[] args) {
ArrayList<String> vowels = new ArrayList<String>();
vowels.add("mitsubishi");
vowels.add("subaru");
vowels.add("nissan");
vowels.add("honda");
vowels.add("toyota");
averageVowels(vowels);
}
public static void averageVowels(ArrayList<String> vowels) {
System.out.println(vowels);
int number = 0;
for (int i = 0; i < vowels.size(); i++)
{
if (vowels.get(i).contains("a") || vowels.get(i).contains("e") || vowels.get(i).contains("i") ||vowels.get(i).contains("o") || vowels.get(i).contains("u"))
{
number++;
}
}
System.out.println("a count: " +number);
System.out.println("e count: " +number);
System.out.println("i count: " +number);
System.out.println("o count: " +number);
System.out.println("u count: " +number);
}
}
You can do without any loops, quite easily so
public static void averageVowels(ArrayList<String> vowels) {
System.out.println(vowels);
String arrayToString = vowels.toString();
int length = arrayToString.length();
System.out.println("a count: " + (length - arrayToString.replace("a", "").length()));
System.out.println("e count: " + (length - arrayToString.replace("e", "").length()));
System.out.println("i count: " + (length - arrayToString.replace("i", "").length()));
System.out.println("o count: " + (length - arrayToString.replace("o", "").length()));
System.out.println("u count: " + (length - arrayToString.replace("u", "").length()));
}
It prints
[mitsubishi, subaru, nissan, honda, toyota]
a count: 4
e count: 0
i count: 4
o count: 3
u count: 3
You want to count five types of things, so you need five variables:
int aCount = 0;
int eCount = 0;
int iCount = 0;
int oCount = 0;
int uCount = 0;
There are many different ways you could loop through each of the words, and then each of the characters in each of the words. Here's one way:
for (int i = 0; i < vowels.size(); i++) {
String lowerCaseWord = vowels.get(i).toLowerCase(); //get lowercase version so we don't have to check each letter twice
for (int j=0; j<lowerCaseWord.length(); j++){ //loop through each char in the string
char c = lowerCaseWord.charAt(j);
if (c == 'a') aCount++;
else if (c == 'e') eCount++;
else if (c == 'i') iCount++;
else if (c == 'o') oCount++;
else if (c == 'u') uCount++;
}
}
Make 5 different variables to count the number of the vowel. For example numbera, number e etc. Then you will need 5 if statements (one for each vowel) each of which will increase its respective count by 1.
for (int i = 0; i < vowels.size(); i++)
for (int j = 0; j<vowels.get(j).length(); j++) {
if (vowels.get(i).charAt('a'))
{
numbera++;
}
if (vowels.get(i).charAt('e'))
{
numbere++;
}
if (vowels.get(i).charAt('i'))
{
numberi++;
}
if (vowels.get(i).charAt('o'))
{
numbero++;
}
if (vowels.get(i).charAt('u'))
{
numberu++;
}}
This
if (vowels.get(i).contains("a") || vowels.get(i).contains("e") || vowels.get(i).contains("i") ||vowels.get(i).contains("o") || vowels.get(i).contains("u"))
only checks if the string contains a, e, i, o, or u. If it found one of these, it won't bother to check the rest of the string. And since you are using ||, in your if statement, it will not evaluate the next conditions if the current condition is already true, so it will proceed to increment number.
If you want to find the number of each vowel, One way is to loop through the string by turning it into a char array and check if a character is a vowel. Then you should create a counter for each vowel and a separated if/switch statement for each. For example with an if statement.
int aCount = 0;
int eCount = 0;
int iCount = 0;
int oCount = 0;
int uCount = 0;
for (int i = 0; i < vowels.size(); i++) {
for (char c : vowels.get(i).toCharArray()) {
if (c == 'a') {
aCount++;
} else if (c == 'e') {
eCount++;
} else (c == 'i') {
iCount++;
} else if (c == 'o') {
oCount++;
} else if (c == 'u') {
uCount++;
} else {
continue;
}
}
}
The following implementation will be efficient. Maintaining a single char array of size 256 would be good enough, which works not only for vowels but for any ASCII character.
import java.util.*;
public class Vowels {
public static void main(String[] args) {
ArrayList<String> vowels = new ArrayList<String>();
vowels.add("mitsubishi");
vowels.add("subaru");
vowels.add("nissan");
vowels.add("honda");
vowels.add("toyota");
averageVowels(vowels);
}
public static void averageVowels(ArrayList<String> vowels) {
System.out.println(vowels);
int[] chars = new int[256];
int number = 0;
for (int i = 0; i < vowels.size(); i++)
{
for (char c : vowels.get(i).toCharArray()) {
chars[c]++;
}
}
System.out.println("a count: " +chars['a']);
System.out.println("e count: " +chars['e']);
System.out.println("i count: " +chars['i']);
System.out.println("o count: " +chars['o']);
System.out.println("u count: " +chars['u']);
}
}

Categories