I'm using the following code to add a guessed consonant to a string of stars if the guessed consonant is part of the original word. Initially I was keeping wordWithGuess between calls to getCurrentResult. But the result of this was that the new content was added to the end, and wordWithGuess kept getting longer (instead of just replacing the most recently guessed letter).
When running the code below, the output is
After guessing r: *****r******
After guessing s: ************
After guessing t: **tt********
After guessing l: ********ll**
After guessing n: ***********n
My goal is for it to be:
After guessing r: *****r******
After guessing s: *****r******
After guessing t: **tt*r******
After guessing l: **tt*r**ll**
After guessing n: **tt*r**ll*n
Sample code follows:
public class Sample {
String targetWord;
String wordWithGuess = "";
public Sample(String targetWord) {
this.targetWord = targetWord;
}
public void guess(String consonant) {
wordWithGuess = "";
for (int i = 0; i < targetWord.length(); i++) {
if (targetWord.substring(i, i + 1).equals(" ")) {
wordWithGuess += " ";
} else if (targetWord.substring(i, i + 1).equals(consonant)) {
wordWithGuess += consonant;
} else {
wordWithGuess += "*";
}
}
}
public String getCurrentResult() {
return wordWithGuess;
}
public static void main(String args[]) {
String targetWord = "bitterbollen";
Sample sample = new Sample(targetWord);
String[] guesses = { "r", "s", "t", "l", "n" };
for (String guess : guesses) {
sample.guess(guess);
System.out.println("After guessing " + guess + ": "
+ sample.getCurrentResult());
}
}
}
you should store all consonants guessed, and change
word.substring(i, i + 1).equals (consonant)
to something like
word.substring(i, i + 1) exists in the consonant guessed. (it is pusedo-code of course)
Some more hints: have a look in Set (or more precisely, HashSet), or String's contains() or indexOf() method.
Some extra opinions to you:
you are calling word.substring(i, i + 1) without storing the returned string, that's a meaningless call.
Instead calling word.substring(i, i + 1) that many times, you can call it once and use the returned string for multiple comparison.
And, as you are comparing one char each time, you should use char to store the character, and using charAt() to get the character at certain position.
The problem is that you need to keep some information between calls to guess(). This means either storing all values of consonant, or finding a way to merge the old value of wordWithGuess with the new consonant.
The first option means something like
import java.util.Set;
import java.util.HashSet;
class Sample {
// ...
Set<String> guesses = new HashSet<String>();
public void guess(String consonant) {
guesses.add(consonant);
wordWithGuess = "";
for (int i = 0; i < targetWord.length(); i++) {
String cursor = targetWord.substring(i, i + 1);
if (cursor.equals(" ")) {
wordWithGuess += " ";
} else if (guesses.contains(cursor)) {
wordWithGuess += cursor;
} else {
wordWithGuess += "*";
}
}
}
// ...
}
This stores the old guesses as a Set. Instead of just checking for the last guess, guess() now includes any letter that has been guessed.
In fact you could even add a constructor to initialize the set with any characters that you want to include by default. This will let you eliminate the check for a space, as it'll be in the initial set of guesses:
import java.util.Set;
import java.util.HashSet;
class Sample {
// ...
Set<String> guesses;
public Sample() {
this.guesses = new HashSet<String>();
guesses.add(" ");
}
public void guess(String consonant) {
guesses.add(consonant);
wordWithGuess = "";
for (int i = 0; i < targetWord.length(); i++) {
String cursor = targetWord.substring(i, i + 1);
if (guesses.contains(cursor)) {
wordWithGuess += cursor;
} else {
wordWithGuess += "*";
}
}
}
// ...
}
The other option would be to update wordWithGuess to include the new guess. In C it's easy to do this, because strings can be modified just like character arrays (for example, wordWithGuess[i] = consonant. Java guards its strings more closely, but there's no reason why one can't use an array of char to the same effect.
public class Sample {
String targetWord;
char[] currentResult;
public Sample(String targetWord) {
this.targetWord = targetWord;
currentResult = new char[targetWord.length()];
for (int i = 0; i < targetWord.length(); i++) {
if(targetWord.charAt(i) == ' ') {
currentResult[i] = ' ';
} else {
currentResult[i] = '*';
}
}
}
public void guess(String consonant) {
for (int i = 0; i < targetWord.length(); i++) {
String cursor = targetWord.substring(i, i + 1);
if (cursor.equals(consonant)) {
currentResult[i] = consonant.charAt(0);
}
}
}
public String getCurrentResult() {
return new String(currentResult);
}
// ...
}
You'll want to store the result of the previous iteration. Have the code oldWordWithGuess = wordWithGuess at the end of the for loop.
Then, in your loop you'll want the following code:
...
if(oldWordWithGuess[i] != `*`) {
wordWithGuess += oldWordWithGuess[i];
} else if (word.substring(i, i + 1).equals (" ")) {
...
That way it will put any previous guesses in.
I've actually found a different solution where I use a char Array and store every letter in a different element of that array. Am I actually allowed to do this? Does this not require too much resources for what it does?
Related
Create a program with the lowest amount of characters to reverse each word in a string while keeping the order of the words, as well as punctuation and capital letters, in their initial place.
By "Order of the words", I mean that each word is split by an empty space (" "), so contractions and such will be treated as one word. The apostrophe in contractions should stay in the same place. ("Don't" => "Tno'd").
(Punctuation means any characters that are not a-z, A-Z or whitespace*).
Numbers were removed from this list due to the fact that you cannot have capital numbers. Numbers are now treated as punctuation.
For example, for the input:
Hello, I am a fish.
it should output:
Olleh, I ma a hsif.
Notice that O, which is the first letter in the first word, is now capital, since H was capital before in the same location.
The comma and the period are also in the same place.
More examples:
This; Is Some Text!
would output
Siht; Si Emos Txet!
I've tried this:
public static String reverseWord(String input)
{
String words[]=input.split(" ");
StringBuilder result=new StringBuilder();
for (String string : words) {
String revStr = new StringBuilder(string).reverse().toString();
result.append(revStr).append(" ");
}
return result.toString().trim();
}
I have tried to solve your problem. It's working fine for the examples I have checked :) Please look and let me know :)
public static void main(String[] args) {
System.out.println(reverseWord("This; Is Some Text!"));
}
public static boolean isAlphaNumeric(String s) {
return s != null && s.matches("^[a-zA-Z0-9]*$");
}
public static String reverseWord(String input)
{
String words[]=input.split(" ");
StringBuilder result=new StringBuilder();
int startIndex = 0;
int endIndex = 0;
for(int i = 0 ; i < input.length(); i++) {
if (isAlphaNumeric(Character.toString(input.charAt(i)))) {
endIndex++;
} else {
String string = input.substring(startIndex, endIndex);
startIndex = ++endIndex;
StringBuilder revStr = new StringBuilder("");
for (int j = 0; j < string.length(); j++) {
char charToAdd = string.charAt(string.length() - j - 1);
if (Character.isUpperCase(string.charAt(j))) {
revStr.append(Character.toUpperCase(charToAdd));
} else {
revStr.append(Character.toLowerCase(charToAdd));
}
}
result.append(revStr);
result.append(input.charAt(i));
}
}
if(endIndex>startIndex) // endIndex != startIndex
{
String string = input.substring(startIndex, endIndex);
result.append(string);
}
return result.toString().trim();
}
Call the reverseWord with your test string.
Hope it helps. Don't forget to mark it as right answer, if it is :)
Here is a proposal that follows your requirements. It may seem very long but its just comments and aerated code; and everybody loves comments.
public static String smartReverseWords(String input) {
StringBuilder finalString = new StringBuilder();
// Word accumulator, resetted after each "punctuation" (or anything different than a letter)
StringBuilder wordAcc = new StringBuilder();
int processedChars = 0;
for(char c : input.toCharArray()) {
// If not a whitespace nor the last character
if(!Character.isWhitespace(c)) {
// Accumulate letters
wordAcc.append(c);
// Have I reached the last character? Then finalize now:
if(processedChars == input.length()-1) {
reverseWordAndAppend(wordAcc, finalString);
}
}
else {
// Was a word accumulated?
if(wordAcc.length() > 0) {
reverseWordAndAppend(wordAcc, finalString);
}
// Append non-letter char to final string:
finalString.append(c);
}
processedChars++;
}
return finalString.toString();
}
private static void reverseWordAndAppend(StringBuilder wordAcc, StringBuilder finalString) {
// Then reverse it:
smartReverse(wordAcc); // a simple wordAcc.reverse() is not possible
// Append word to final string:
finalString.append(wordAcc.toString());
// Reset accumulator
wordAcc.setLength(0);
}
private static class Marker {
Integer position;
String character;
}
private static void smartReverse(StringBuilder wordAcc) {
char[] arr = wordAcc.toString().toCharArray();
wordAcc.setLength(0); // clean it for now
// Memorize positions of 'punctuation' + build array free of 'punctuation' in the same time:
List<Marker> mappedPosOfNonLetters = new ArrayList<>(); // order matters
List<Integer> mappedPosOfCapitals = new ArrayList<>(); // order matters
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if(!Character.isLetter(c)) {
Marker mark = new Marker();
mark.position = i;
mark.character = c+"";
mappedPosOfNonLetters.add(mark);
}
else {
if(Character.isUpperCase(c)) {
mappedPosOfCapitals.add(i);
}
wordAcc.append(Character.toLowerCase(c));
}
}
// Reverse cleansed word:
wordAcc.reverse();
// Reintroduce 'punctuation' at right place(s)
for (Marker mark : mappedPosOfNonLetters) {
wordAcc.insert(mark.position, mark.character);
}
// Restore capitals at right place(s)
for (Integer idx : mappedPosOfCapitals) {
wordAcc.setCharAt(idx,Character.toUpperCase(wordAcc.charAt(idx)));
}
}
EDIT
I've updated the code to take all your requirements into account. Indeed we have to make sure that "punctuation' stay in place (and capitals also) but also within a word, like a contraction.
Therefore given the following input string:
"Hello, I am on StackOverflow. Don't tell anyone."
The code produces this output:
"Olleh, I ma no WolfrEvokcats. Tno'd llet enoyna."
I need to fetch a sub string that lies between two same or different delimiters. The delimiters will be occurring multiple times in the string, so i need to extract the sub-string that lies between mth occurrence of delimiter1 and nth occurrence of delimiter2.
For eg:
myString : Ron_CR7_MU^RM^_SAF_34^
What should i do here if i need to extract the sub-string that lies between 3rd occurrence of '_' and 3rd occurence of '^'?
Substring = SAF_34
Or i could look for a substring that lies between 2nd '^' and 4th '_', i.e :
Substring = _SAF
An SQL equivalent would be :
substr(myString, instr(myString, '',1,3)+1,instr(myString, '^',1,3)-1-instr(myString, '',1,3))
I would use,
public static int findNth(String text, String toFind, int count) {
int pos = -1;
do {
pos = text.indexOf(toFind, pos+1);
} while(--count > 0 && pos >= 0);
return pos;
}
int from = findNth(text, "_", 3);
int to = findNth(text, "^", 3);
String found = text.substring(from+1, to);
If you can use a solution without regex you can find indexes in your string where your resulting string needs to start and where it needs to end. Then just simply perform: myString.substring(start,end) to get your result.
Biggest problem is to find start and end. To do it you can repeat this N (M) times:
int pos = indexOf(delimiterX)
myString = myString.substring(pos) //you may want to work on copy of myString
Hope you get an idea.
You could create a little method that simply hunts for such substrings between delimiters sequentially, using (as noted) String.indexOf(string); You do need to decide whether you want all substrings (whether they overlap or not .. which your question indicates), or if you don't want to see overlapping strings. Here is a trial for such code
import java.util.Vector;
public class FindDelimitedStrings {
public static void main(String[] args) {
String[] test = getDelimitedStrings("Ron_CR7_MU'RM'_SAF_34'", "_", "'");
if (test != null) {
for (int i = 0; i < test.length; i++) {
System.out.println(" " + (i + 1) + ". |" + test[i] + "|");
}
}
}
public static String[] getDelimitedStrings(String source,
String leftDelimiter, String rightDelimiter) {
String[] answer = null;
;
Vector<String> results = new Vector<String>();
if (source == null || leftDelimiter == null || rightDelimiter == null) {
return null;
}
int loc = 0;
int begin = source.indexOf(leftDelimiter, loc);
int end;
while (begin > -1) {
end = source
.indexOf(rightDelimiter, begin + leftDelimiter.length());
if (end > -1) {
results.add(source.substring(begin, end));
// loc = end + rightDelimiter.length(); if strings must be
// returned as pairs
loc = begin + 1;
if (loc < source.length()) {
begin = source.indexOf(leftDelimiter, loc);
} else {
begin = -1;
}
} else {
begin = -1;
}
}
if (results.size() > 0) {
answer = new String[results.size()];
results.toArray(answer);
}
return answer;
}
}
I'd like to retrieve whatever is in quotes that someone enters as a string, i'm assuming it's substring that I need but i'm not sure how.
When the user inputs a string mixed with words and numbers all separated by one space:
hey 110 say "I am not very good at Java" but " I can fish pretty well"
Then I want to be able to take the "I am not very good at Java" and the "I can fish pretty well" and print out what's inside the quotes so there can be multiple quotes in the string.
right now I have if( userInput=='"') then I do something with substring but i'm not sure what.
I can't use split, trim, tokenizer, regex or anything that would make this really easy unfortunatley.
it's all in this method where I try to identify if something in the string is a word, number or a quote:
public void set(String userInput)// method set returns void
{
num=0;// reset each variable so new input can be passed
String empty="";
String wordBuilder="";
userInput+=" ";
for(int index=0; index<userInput.length(); index++)// goes through each character in string
{
if(Character.isDigit(userInput.charAt(index)))// checks if character in the string is a digit
{
empty+=userInput.charAt(index);
}
else
{
if (Character.isLetter(userInput.charAt(index)))
{
wordBuilder+=userInput.charAt(index);
}
else
{
if(userInput.charAt(index)=='"')
{
String quote=(userInput.substring(index,);
}
}
//if it is then parse that character into an integer and assign it to num
num=Integer.parseInt(empty);
word=wordBuilder;
empty="";
wordBuilder="";
}
}
}
}
Thanks!
Try the next:
public static void main(String[] args) {
String input = "\"123\" hey 110 say \"I am not very good at Java\" but \" I can fish pretty well\"";
int indexQuote = -1;
boolean number = true;
String data = "";
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isWhitespace(ch)) {
if (data.length() > 0 && indexQuote == -1) {
if (number) {
System.out.println("It's a number: " + data);
} else {
System.out.println("It's a word: " + data);
}
// reset vars
number = true;
data = "";
} else if (indexQuote != -1) {
data += ch;
}
} else if (ch == '"') {
if (indexQuote == -1) {
number = false;
indexQuote = i;
} else {
System.out.println("It's a quote: " + data);
// reset vars
number = true;
data = "";
indexQuote = -1;
}
} else {
if (!Character.isDigit(ch)) {
number = false;
}
data += ch;
if (data.length() > 0 && i == input.length() - 1) {
if (number) {
System.out.println("It's a number: " + data);
} else {
System.out.println("It's a word: " + data);
}
}
}
}
}
Output:
It's a word: hey
It's a number: 110
It's a word: say
It's a quote: I am not very good at Java
It's a word: but
It's a quote: I can fish pretty well
I'm not sure if this quite what you are looking for, but it will strip down the quoted parts in steps...
String quote = "I say: \"I have something to say, \"It's better to burn out then fade away\"\" outloud...";
if (quote.contains("\"")) {
while (quote.contains("\"")) {
int startIndex = quote.indexOf("\"");
int endIndex = quote.lastIndexOf("\"");
quote = quote.substring(startIndex + 1, endIndex);
System.out.println(quote);
}
}
Which outputs...
I have something to say, "It's better to burn out then fade away"
It's better to burn out then fade away
Updated
I don't know if this is cheating or not...
String quote = "I say: \"I have something to say, \"It's better to burn out then fade away\"\" outloud...\"Just in case you don't believe me\"";
String[] split = quote.split("\"");
for (String value : split) {
System.out.println(value);
}
Which outputs...
I say:
I have something to say,
It's better to burn out then fade away
outloud...
Just in case you don't believe me
Updated
Okay, fake String#split
StringBuilder sb = new StringBuilder(quote.length());
for (int index = 0; index < quote.length(); index++) {
if (quote.charAt(index) == '"') {
System.out.println(sb);
sb.delete(0, sb.length());
} else {
sb.append(quote.charAt(index));
}
}
Updated
Okay, this is basically fake split with options...
String quote = "blah blah 123 \"hello\" 234 \"world\"";
boolean quoteOpen = false;
StringBuilder sb = new StringBuilder(quote.length());
for (int index = 0; index < quote.length(); index++) {
if (quote.charAt(index) == '"') {
if (quoteOpen) {
System.out.println("Quote: [" + sb.toString() + "]");
quoteOpen = false;
sb.delete(0, sb.length());
} else {
System.out.println("Text: [" + sb.toString() + "]");
sb.delete(0, sb.length());
quoteOpen = true;
}
} else {
sb.append(quote.charAt(index));
}
}
if (sb.length() > 0) {
if (quoteOpen) {
System.out.println("Quote: [" + sb.toString() + "]");
} else {
System.out.println("Text: [" + sb.toString() + "]");
}
}
Which generates...
Text: [blah blah 123 ]
Quote: [hello]
Text: [ 234 ]
Quote: [world]
Know, I don't know how you are storing the results. I would be tempted to create some basic classes which were capable of storing the String results and add them to a List so I could maintain the order and maybe use a flag of some kind to determine what type they are...
Iterate over the string and use a temporary int variable to store when the quoted string started. When you see that it ends, you can extract that substring and do what you want with it.
Use StringUtils.subStringBetween
public class MyTestSecond {
public static void main(String...args){
String a = "hey 110 say \"I am not very good at Java\"";
// Method 1
if(a.contains("\""))
System.out.println(a.substring(a.indexOf("\""),a.lastIndexOf("\"")+1));
//Method 2
String[] array = a.split(" ");
for(int i=0;i<array.length;i++){
if(array[i].startsWith("\""))
System.out.println(a.substring(a.indexOf("\""),a.lastIndexOf("\"")+1));
}
}
}
public String getNextQuote(int index, String sentence){
return sentence.substring(sentence.indexOf("\"", index + 1), sentence.indexOf("\"", index + 2));
}
usage: call the method with an index as parameter. This index resembles the index of the last " that you've encountered.
Afterwards, it will return everything between the next two quotes.
this is the program that I have to write but I get this error,
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
50
Write a complete program using two arrays, upper and lower to keep the upper
And lower alphabet respectively.
Ask the user to enter string example:
This is a test from Jupiter. Soon you will see who is from
Jupiter!!! May be Dr. D.
Your program should parse the string and keep track of number of alphabet. Both arrays are indexed from 0 to 25. The logical way to do this is to use upper[0] to
Count the number of ‘A’, and upper[1] to count number of ‘B’ and so on. Likewise
For the lower array.
Output should look like:
A: 0 a:2
B: 0 b:0
.
.
.
Z:0 z:0
Code
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Letter {
public static void main(String[] args) {
// this is get results
char[] chars = userEnters();
System.out.println();
System.out.println("Occurrences of each letter are:");
PrintArray(countLow(chars), countUp(chars));
}
public static char[] userEnters() {
String inputX = JOptionPane.showInputDialog("Enter line of text: ");
char[] chars = inputX.toCharArray();
return chars;
}
public static int[] countLow(char[] input) {
int[] counts = new int[26];
for (int i = 0; i < input.length; i++) {
counts[input[i] - 'a']++;
}
return counts;
}
public static int[] countUp(char[] input2) {
int[] countsUp = new int[26];
for (int i = 0; i < input2.length; i++) {
countsUp[input2[i] - 'A']++;
}
return countsUp;
}
public static void PrintArray(int[] counts, int[] countsUp) {
for (int i = 0; i < counts.length; i++) {
System.out.print(counts[i] + " " + (char) ('a' + i) + " ");
System.out.print(countsUp[i] + " " + (char) ('A' + i) + "\n");
}
}
}
If you enter a character that is not a large cap, countUp will throw an exception and if you enter a character that is not a small cap, countLow will throw an exception.
Example: if you call countLow on a A, you calculate 'A' - 'a' which returns -32 and a negative index is not allowed.
You need to review your logic and call either countLow or countUp depending on the case of the letter and filter invalid characters out.
Or refactor the whole thing and use a char[52] for example where you hold both small and large caps.
I Hope you don't mind I did refactor your code a bit.
Please have a look at this alterantive solution to your problem and then read the comments at the bottom of the answer.
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JOptionPane;
public class LetterCounter {
//Hash maps don't allow duplication.
//The letter will be the Key and the repetitions the value(Your goal!)
private Map<Character, Integer> resultsMap = new HashMap<Character, Integer>();
public static void main(String[] args) {
LetterCounter letterCounter = new LetterCounter();
letterCounter.fillMap();
letterCounter.showMapContents();
}
private void showMapContents() {
for (Entry<Character, Integer> entry : resultsMap.entrySet())
{
System.out.println("'" + entry.getKey() + "' - " + entry.getValue() + " times");
}
}
private void fillMap() {
char[] userInputAsArray = getUserInputAsLetterArray();
for (int currentLetter = 0; currentLetter < userInputAsArray.length; currentLetter++) {
int count = getOccurences(userInputAsArray[currentLetter],userInputAsArray);
resultsMap.put(userInputAsArray[currentLetter], count);
}
}
private int getOccurences(int letter, char[] userInputAsArray) {
int counter = 0;
for (int currentIndex = 0; currentIndex < userInputAsArray.length; currentIndex++) {
if(userInputAsArray[currentIndex] == letter)
counter++;
}
return counter;
}
public char[] getUserInputAsLetterArray() {
String userInput = JOptionPane.showInputDialog("Enter line of text: ");
char[] chars = userInput.toCharArray();
return chars;
}
}
Whenever you want to do an exercise where you need to manipulate data, you should pick the best data structure for the job. In your case, I think the hash map could be interesting because it avoids duplicates and will do a big part of the job for you. Find a very good cheat sheet in this link: http://www.janeve.me/articles/which-java-collection-to-use
I noticed that you used a lot static and that is not a very Object Oriented thing to do. As an alternative, when you want to just on the run do some quick examples like this one, you can just initialize the class inside itself.
I hope this was useful.
You should probably consider moving from arrays to a more complex and powerful data structure like Map<Character,Integer>.
With that data structure the code you need would look something like
public Map<Character,Integer> countOccurrencies(String inputString){
Map<Character,Integer> occurrencies = new HashMap<Character,Integer>();
for(Character c : inputString){
if(occurrencies.containsKey(c)){
occurrencies.put(c, occurrencies.containsKey(c) + 1);
} else {
occurrencies.put(c, 1);
}
}
return occurrencies;
}
Answer in java:
Here, countOfOccurances("pppggggkkkkpgaaaa") gives you count of occurrences of each character from a String
public static void countOfOccurances(String mainStr)
{
String temp = "";
for (int i = 0 ; i < mainStr.length();i++)
{
CharSequence ch = String.valueOf(mainStr.charAt(i));
temp=mainStr.replace(ch, "");
int count = (mainStr.length()-temp.length());
System.out.println(ch+" = "+count);
mainStr = temp;
i = -1;
}
}
Output of method:
p = 4
g = 5
k = 4
a = 4
This should be quite simple (I think), but I just can't get it right...:|
The task is as follows:
Ask the user for some input. The input must be split in to single words and put into an array. All words should be counted. If equal words exists, they get a "+1" on the output.
Finally I want to print out and hopefully the right amount of counted words in a list. I got the first two columns right, but the word-counter of equal words gave me a headache. If a word is found to be equal, it mustnt appear twice in the generated list! :!
I am a complete JAVA newbie so please be kind on the code-judging. ;)
Here is my code so far:
package MyProjects;
import javax.swing.JOptionPane;
public class MyWordCount {
public static void main(String[] args) {
//User input dialog
String inPut = JOptionPane.showInputDialog("Write som text here");
//Puts it into an array, and split it with " ".
String[] wordList = inPut.split(" ");
//Print to screen
System.out.println("Place:\tWord:\tCount: ");
//Check & init wordCount
int wordCount = 0;
for (int i = 0; i < wordList.length; i++) {
for (int j = 0; j < wordList.length; j++){
//some code here to compare
//something.compareTo(wordList) ?
}
System.out.println(i + "\t" + wordList[i]+ "\t" + wordCount[?] );
}
}
}
You can use Hashmap to do that. A Hashmap stores key-value pairs and each key has to be unique.
So in your case, a key will be a word of the string you have split and value will be it's count.
Once you have split the input into words and put them into a string array, put the first word,as a key, into the Hashmap and 1 as it's value. For each subsequent word, you can use the function containsKey() to match that word with any of the existing keys in the Hashmap. If it returns true, increment the value (count) of that key by one, else put the word and 1 as a new key-value pair into the Hashmap.
So in order to compare two strings, you do:
String stringOne = "Hello";
String stringTwo = "World";
stringOne.compareTo(stringTwo);
//Or you can do
stringTwo.compareTo(stringOne);
You can't compare a String to a String array like in your comment. You would have to take an element in this string array, and compare that (So stringArray[elementNumber]).
For counting how many words there are, if you are determining the number of repeated words, you would want to have an array of integers (So make a new int[]). Each place in the new int[] should correspond to the word in your array of words. This would allow you to count the number of times a word is repeated.
import java.util.ArrayList;
import java.util.regex.PatternSyntaxException;
import javax.swing.JOptionPane;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
//Print to screen
System.out.println("Place:\tWord:\tCount: ");
//User input dialog
String inPut = JOptionPane.showInputDialog("Write som text here");
//Puts it into an array, and split it with " ".
String[] wordList;
try{
wordList = inPut.split(" ");
}catch(PatternSyntaxException e) {
// catch the buggy!
System.out.println("Ooops.. "+e.getMessage());
return;
}catch(NullPointerException n) {
System.out.println("cancelled! exitting..");
return;
}
ArrayList<String> allWords = new ArrayList<String>();
for(String word : wordList) {
allWords.add(word);
}
// reset unique words counter
int uniqueWordCount = 0;
// Remove all of the words
while(allWords.size() > 0) {
// reset the word counter
int count = 0;
// get the next word
String activeWord = allWords.get(0);
// Remove all instances of this word
while(doesContainThisWord(allWords, activeWord)) {
allWords.remove(activeWord);
count++;
}
// increase the unique word count;
uniqueWordCount++;
// print result.
System.out.println(uniqueWordCount + "\t" + activeWord + "\t" + count );
}
}
/**
* This function returns true if the parameters are not null and the array contains an equal string to newWord.
*/
public static boolean doesContainThisWord(ArrayList<String> wordList, String newWord) {
// Just checking...
if (wordList == null || newWord == null) {
return false;
}
// Loop through the list of words
for (String oldWord : wordList) {
if (oldWord.equals(newWord)) {
// gotcha!
return true;
}
}
return false;
}
}
Here's a solution using a map of WordInfo objects that records locations of the words within the text and uses that as a count. The LinkedHashMap preserves the order of keys from when they are first entered so simply iterating through the keys gives you the "cast in order of appearance"
You can make this case insensitive while preserving the case of the first appearance by storing all keys as lower case but storing the original case in the WordInfo object. Or just convert all words to lower case and leave it at that.
You may also want to think about removing all , / . / " etc from the first text before splitting, but you'll never get that perfect anyway.
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JOptionPane;
public class MyWordCount {
public static void main(String[] args) {
//User input dialog
String inPut = JOptionPane.showInputDialog("Write som text here");
Map<String,WordInfo> wordMap = new LinkedHashMap<String,WordInfo>();
//Puts it into an array, and split it with " ".
String[] wordList = inPut.split(" ");
for (int i = 0; i < wordList.length; i++) {
String word = wordList[i];
WordInfo wi = wordMap.get(word);
if (wi == null) {
wi = new WordInfo();
}
wi.addPlace(i+1);
wordMap.put(word,wi);
}
//Print to screen
System.out.println("Place:\tWord:\tCount: ");
for (String word : wordMap.keySet()) {
WordInfo wi = wordMap.get(word);
System.out.println(wi.places() + "\t" + word + "\t" + wi.count());
}
}
}
And the WordInfo class:
import java.util.ArrayList;
import java.util.List;
public class WordInfo {
private List<Integer> places;
public WordInfo() {
this.places = new ArrayList<>();
}
public void addPlace(int place) {
this.places.add(place);
}
public int count() {
return this.places.size();
}
public String places() {
if (places.size() == 0)
return "";
String result = "";
for (Integer place : this.places) {
result += ", " + place;
}
result = result.substring(2, result.length());
return result;
}
}
Thanks for trying to help me. -This is what I ended up doing:
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class MyWordCount {
public static void main(String[] args) {
// Text in
String inText = JOptionPane.showInputDialog("Write some text here");
// Puts it into an array, and splits
String[] wordlist = inText.split(" ");
// Text out (Header)
System.out.println("Place:\tWord:\tNo. of Words: ");
// declare Arraylist for words
ArrayList<String> wordEncounter = new ArrayList<String>();
ArrayList<Integer> numberEncounter = new ArrayList<Integer>();
// Checks number of encounters of words
for (int i = 0; i < wordlist.length; i++) {
String word = wordlist[i];
// Make everything lowercase just for ease...
word = word.toLowerCase();
if (wordEncounter.contains(word)) {
// Checks word encounter - return index of word
int position = wordEncounter.indexOf(word);
Integer number = numberEncounter.get(position);
int number_int = number.intValue();
number_int++;
number = new Integer(number_int);
numberEncounter.set(position, number);
// Number of encounters - add 1;
} else {
wordEncounter.add(word);
numberEncounter.add(new Integer(1));
}
}
// Text out (the list of words)
for (int i = 0; i < wordEncounter.size(); i++) {
System.out.println(i + "\t" + wordEncounter.get(i) + "\t"
+ numberEncounter.get(i));
}
}
}