I am attempting to count the unique strings (a.k.a flowers) in the array along with a count of any duplicates. An example is embedded in my code below. I've only pasted the part of the program I am having trouble with. I can't figure out what I am doing incorrectly.
private void displayFlowers(String flowerPack[]) {
// TODO: Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
I am trying to count the number of occurrences of a string in an array list. I used the following code:
int count = Collections.frequency(strings, search);
strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)
Why am I getting this error message and how do I fix it?
I have to write some code to take names from the user and then order them in alphabetical order and then print them out which i have managed to do. However, i can't get it to count the characters in the names that the user enters or count the amount of vowels in the names.
This is the code ive written:
import javax.swing.JOptionPane; import java.util.Arrays; String[] names = new String[9]; int i; names[0] = JOptionPane.showInputDialog("Please enter a name"); names[1] = JOptionPane.showInputDialog("Please enter a name");
I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}
now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'} Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.
How would one go about this using the counting elements technique?
I tried this but not sure how to use the counting elements technique.
char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'}; char[] bucket = new char[(arr[0] * arr.length)]; for (int i = 0; i < len; i++) bucket[arr[i]]++;
Which string method to search a particular character in a string whether a particular character is there or not . Suppose my String is like............
String s = "Java.";
How I can find the character " . " in above string "Java." is present or not . What is the string method to search that "."
I am currently trying to count and display all the vowels in a set of given strings and can't seem to figure out what to do. I was able to print the line with the most vowels, but i also need to display them. The code is listed below and the given output.
public class Strings { public static void main(String[] args) { String sentence = "I am currently studing Computer Science." + "My name is whatever and I am orginaially from the state of Virginia." + "I recenetly separated from the Air Force where I served on the Presidental Honor Guard.";
[Code] ....
The output that i am getting is:
I am currently studying Computer Science . My name is whatever and I am originally from the state of Virginia. I recently separated from the Air Force where I served on the Presidential Honor Guard.
The line with most vowels is:
I recently separated from the Air Force where I served on the Presidential Honor Guard.
I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:
hello world how are you There are 0 words of length 0 There are 0 words of length 1 There are 0 words of length 2 There are 3 words of length 3 There are 0 words of length 4 There are 2 words of length 5
This is my code so far it sort of does the job but not the way i want it too
import java.util.Scanner; import java.util.StringTokenizer; public class Brown_Matthew_13117002{ public static int count(String s, int len){ int result=0; StringTokenizer st=new StringTokenizer(s,"[ ,;]");
[Code] ....
The output would end up being :
hello There are 0 words of length 0 world There are 0 words of length 1 how There are 0 words of length 2 are There are 3 words of length 3 you There are 0 words of length 4
Aeparated by blanks in a string. For simplicity, use strings without punctuation or other white space characters(tabs, newlines etc). Use a JTextArea to allow the user to enter the text and allow the text area to scroll if necessary. when the user clicks a button to count the words , the total number of words counted is displayed in a textbox that cannot be modified by the user.
now my problem is that i am not getting the counted number to display in the un-editable textbox. i also have the problem where the cusrsor is showing in the middle of the input screen instead of at the top.
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; import java.util.*; public class WordCounter extends JFrame implements ActionListener
I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:
hello world how are you There are 0 words of length 0 There are 0 words of length 1 There are 0 words of length 2 There are 3 words of length 3 There are 0 words of length 4 There are 2 words of length 5
ithis is my code so far it sort of does the job but not the way i want it too
import java.util.Scanner; import java.util.StringTokenizer; public class Brown_Matthew_13117002{
I am trying to count the number of non_blank characters in a string. If there are no leading blank spaces it works fine but say i add three spaces in front it doubles the non blank characters.
import java.io.*; import java.util.*; public class countCharacters { public static void main(String[] args) throws Exception { String str1; int count; count = 0;
So I need to write a program that reads through a String and counts how many vowels there are and prints them out as it finds them. This is what I have so far:
I have this source code that is supposed to count the repeated occurrences of certain strings in the text file, "string.txt". The contents of the file are listed below:
(Count occurrence of numbers) Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time
Note that if a number occurs more than one time, the plural word "times" is used in the output.
Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)
Example: Enter a string: "Hello, JAVA is my favorite programming language." Enter a character: e The number of times the specified character appears in the string: 3
I don't even know where to begin I've only got this
import java.util.Scanner; public class letterCounter { public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string"); String myString = sc.nextLine(); System.out.println("Enter a letter"); String letter = sc.nextLine(); } }
I wrote a program to read a .txt file and return how many times a, e, s, and t occur in the .txt file. I am getting an error that I do not know how to fix. It says Error: FileNotFoundException cannot be resolved to a type...
import java.util.Scanner; import java.io.File; import java.io.IOException; import java.io.BufferedReader; public class Count { public static void main (String[] args) throws FileNotFoundException { String phrase; // a string of characters
In this array so many values are duplicates means 33 comes twice & 5 also comes twice & 9 comes three times. But I want to count the first value which is duplicate means 33 is first value which comes twice so answer would be 2.
I try:
public class FindFirstDuplicate { public static void main(String[] args) { int c=0; int[] a = {33,33,5,5,9,8,9,9}; outerloop: for(int i = 0; i < a.length; i++) {
I'm trying to write a program that counts the number of times each number in an array occurs. In theory I understand how it work, you take the first element in the array, compare it again the rest of the array and any time it occurs in the array you increment a counter by 1. Then move onto the next element A[1] and so on...
This is what I've done so far :
public static void main(String[] args) { int[] array = {5,6,2,5,2,2,0,1,6}; int count = 0; for(int i = 0; i <array.length; i++) { int val = array[i]; for(int j = i+1; j<array.length; j++)
[Code] .....
I think I'm on the right track as it correctly prints out the frequency of the first 3 elements, but I'm definitely going wrong somewhere as it just doesn't do it right after that!
I'm trying to generate random 100 numbers, from 0 to 9, in an array using Math.random, but it only outputs 0s which is very confusing to me. Also, I need to be able to count how many different integers there are like 0s, 1s, 2s... 8s, 9s.
Here's my code, I only got as far as the array then got stumped on the counting part.
import java.util.Arrays; public class countDigits { public static void main(String[] args) { //Create random generator and values int numbers = (int)(Math.random() * 10); int arrayCount = 1;
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {
//read the file //make the numbers 1 string line //count the number of repetitiveness in the string for the numbers //display four lowest ones
import java.io.*; import java.util.*; public class Lottery2
[Code] ....
when I run it the array gets sorted but how do i keep the data in other words
what it is supposed to do is grab numbers from a file, and give me the lowest 4 numbers back. I know the numbers using the array but how do i pull out the lowest 4 and keep the data true no matter what numbers or how many of them are in the file.
I am looking for a program to generate a unique alphanumerical identifier that is not too long; for example would start out with 6 digits like a licence plate or a postal code ex: AAA001 and use up all the possible combinations until 999ZZZ (just an example) and then when the possibilities are exhausted a 7th digit is added and so on. It matters not if they are sequential, the identifier just needs to be unique and not be too hard to remember (also i don't want to use ip adress or any personal identification). How I can accomplish this using java.
Is it a good idea to use the date and time with the first or last few values of the session ID. Or should I just use the complete session ID value for my "unique id"?
I want a completely unique GUI with unique buttons, like I could make it a giraffe if I wanted to! (not going to, but a giraffe seemed like a pretty irregular shape) ....