Java Count Words IndexOf - java

I'm trying to use simply a while loop and the String method indexOf() to count how many times a certain word appears in a String given by the user.
The method I created seems to count how many times a certain letter appears, but not how many times a word appears. I think this is because the indexOf can't differentiate between a set of letters and spaces. So would I have to create another loop for the computer to understand what I consider words?
This is what I have so far:
public static void countWord(String sentenceEntered, String badWord){
int number = sentenceEntered.toUpperCase().indexOf(badWord, 0);
while (number >= 0){
System.out.print(number);
number = sentenceEntered.indexOf(badWord, number + 1);
}
}//end of countWord
But when I run my program, nothing gets printed.

There are two main problems in your method:
The code converts the sentence to uppercase, but not the input word. Also in the while loop, the sentence is not converted to uppercase. So there is an inconsistency here.
The offset in the call to String#indexOf should be the previous index plus the length of the word, i.e. number + badWord.length() instead of number + 1.
number = sentenceEntered.indexOf(badWord, number + badWord.length());
So after the changes, and assuming you no conversion to uppercase is done, the method should be as follows:
public static void countWord(String sentenceEntered, String badWord) {
int number = sentenceEntered.indexOf(badWord, 0);
while (number >= 0) {
System.out.println(number);
number = sentenceEntered.indexOf(badWord, number + badWord.length());
}
}// end of countWord

You should uppercase your badword too.
int number = sentenceEntered.toUpperCase().indexOf(badWord.toUpperCase(), 0);

Your problem is that you need to set the new starting index to number + badWord.length() in the while loop so that you can start the next indexOf at the end of the last badWord.
number = sentenceEntered.indexOf(badWord, number + badWord.length());

Apart from what is causing your current issue, if you just want to count the number of word occurence in a sentence, there's a simple way without using the loops
int length = sentenceEntered.length();
int badwordLength = badword.length();
if (sentenceEntered.contains(badword)) {
System.out.println("badword count: " + (length - sentenceEntered.replace(badword,"").length() / badwordLength));
}

Related

Check String is in a range of numbers Java

Trying to design a simple lottery program. Everything works except checking if the numbers entered are between 1 to 59.
Exercise says the numbers must be stored in a String variable.
so
if(num<0 || num>59) //wont work for me
Tried making another variable
int numConverted = Integer.parseInt(num)
We haven't covered converting String to int in class though so I don't think this is what expected. Got confused trying that way anyway so probably this is wrong.
Here is the code I have currently.
{
Scanner scan = new Scanner(System.in);
String num=""; //num variable is empty untill user inputs numbers
for(int i =0; i<6; i++)
{
System.out.println("Enter your number between 1-59");
num = num +" "+ scan.nextLine();
}
System.out.println("Ticket printed £2. Your numbers are " + num);
}
In your posted code it's obvious that you want the User to supply 6 specific numerical values. These values are appended to the String variable named num (space delimited). You need to obviously do a few things here:
1) Make sure the value supplied by the user is indeed a numerical value;
2) Make sure the numerical values supplied fall within the minimum and maximum scope of the lottery itself (which you have stated is: 1 to 59);
3) Make sure the number entered by the User hasn't been supplied already.
You've been tasked to store the entered values into a String data type variable and that is all fine but at some point you want to carry out value comparisons to make sure that all the entered values actually play within the limits of the lottery.
When the User completes his/her entries, you end up with a space delimited string held in the num string variable. You now need to make sure that these values entered are indeed....numbers from 1 to 59 and none contain alpha characters.
In my opinion (and this is only because you need to store entered values into a String variable), it's best to use your String variable to gather User input, then test the input to make sure it is indeed a string representation of an actual integer number. Once this is established then we test to make sure if falls within the value min/max limits (1-59). Now we need to test to make sure the number entered hasn't already been entered before for this ticket.
Of course with each test described above, if one fails then the User should be prompted to re-enter a proper value. You can do this by utilizing a while loop. Plenty examples of this in StackOverflow but here's a quick example:
Scanner scan = new Scanner(System.in);
String ticketNumbers = "";
for(int i = 0; i < 6; i++) {
Boolean isOK = false;
while (!isOK) {
System.out.println("\nPlease enter your desired 6 ticket numbers:\n"
+ "(from 1 to 59 only)");
String num = scan.nextLine();
//Is the string entered an actual integer number?
//We use the String.matches() method for this with
//a regular expression.
if(!num.matches("\\d+")) {
System.out.println("You must supply a numerical value! "
+ "Try Again...");
continue;
}
if (ticketNumbers.contains(num + " ")) {
System.out.println("The number you supplied has already been chosen!"
+ " Try Again...");
continue;
}
if (Integer.parseInt(num) >= 1 && Integer.parseInt(num) <= 59) {
ticketNumbers+= num + " ";
isOK = true;
}
else {
System.out.println("The number you supply must be from "
+ "1 to 59! Try Again...");
}
}
}
System.out.println("Ticket printed £2. Your numbers are " + ticketNumbers);
How about -
if(Integer.parseInt(num) < 0 || Integer.parseInt(num) > 59)
This should work, place it after the input.
If it works, please mark this as correct, I need the rep!!!
Easy way would be add available numbers (suppose it wont grow more than 60. You can use a loop to add to this as well)
String numbers[] = {"1","2","3", "..."};
Then inside the loop
Arrays.asList(numbers).contains(num);
You can remove prefixing zero in order avoid conflicts with values like '02'
Here everything is String related.
If you don't want to explicitly convert to int, you could use a regular expression.
if (num.matches("[1-5]?[0-9]")) {
...
This checks whether the String consists of (1) maybe a digit from 1 to 5, followed by (2) definitely a digit from 0 to 9. That'll match any number in the range 0-59.
If you've got a whole series of numbers separated by spaces, you could expand this to cover a whole series like this.
if (num.matches("([1-5]?[0-9]\\s+)*[1-5]?[0-9]")) {
This matches any number of repetitions (including zero) of "a number followed by spaces", followed by a single repetition without a space. The "\\s" means "any whitespace character", the "+" after it means "one or more of what precedes", and the "*" means "zero more of what precedes" - which in this case is the term in parentheses.
Oh I see what you are trying to do
This is what you want
Scanner scan = new Scanner(System.in);
String allNums = "";
for(int i =0; i<6; i++)
{
System.out.println("Enter your number between 1-59");
int num = scan.nextInt();//Take the number in as an int
if(num >0 && num < 59)//Check if it is in range
{
allNums += num + " ";//if it is add it to a string
}
else
{
System.out.println("Number not in range");
i--;//go back one iteration if its not in range
}
}
System.out.println("Ticket printed £2. Your numbers are " + allNums);

How to create a number sequence such as "minutes:seconds"

Write a program that asks the user to enter the time as a sequence of digits and then displays the time as "minutes:seconds". Assume that if two or fewer digits are entered that the digits represent seconds. In this case, display a zero before the colon. If three or more digits are entered, the last two represent seconds.
Output needs to be:
Enter cook time-> 123
Your time-> 1:23
According to the instructions how does one create this sequence? I know how to scan in the user's time. I just need help differentiating between minutes and seconds.
There's probably no end of ways you might achieve this, you could use modular maths, convert the input to a String and convert that to a char array or you could just use String#substring
For example...
int value = 1;
String text = Integer.toString(value);
if (text.length() < 2) {
if (text.length() == 1) {
text = "0:0" + text;
} else {
text = "0:" + text;
}
} else {
text = text.substring(0, text.length() - 2) + ":" + text.substring(text.length() - 2);
}
And yes, you could just have a 3 way if-else instead of the compound if, but that's up to you
Take a closer look at String#substring(int) and String#substring(int, int) for more details
You should also take a look at the Strings trail

Java - divide a text into n equal lines

Let's say I have this String
String myText="I think that stackoverflow is a very great website";
If i want to divide it in 2 lines i would have something like
I think that stackoverflow
is a very great website.
So the String will be now ("I think that stackoverflow\nis a very great website"
If I want it to divide in 3 lines it will be like
I think that
stackoverflow is a
very great website
What I've tried was just dividing the text, every line would have total number of words / n (n is the number of lines that i want to divide my text).
But this is a bad thing, i would have a result like
String myText="I me him is veryverylong wordvery longestwordever thisisevenlonger"
And the result would be (if i want to divide it in 2 lines) something like
"i you me is\nveryverylong wordvery longestwordever thisisevenlonger"
What do you guys suggest for me to try?
I've tried the common apache algorithm
http://pastebin.com/68zycavf
But my output text will be every word separated by \n ..if i use wrap(text,2)..
As Eran noted in his answer, you want to split at approximately the line length divided by the desired number of lines, but have to adjust for that being in the middle of a word.
I think his solution won't quite always give the best solution though, as it might sometimes be best to split before the word instead of after as he's doing.
A divide-and-conquer approach would be a recursive algorithm roughly as follows:
Let N be the desired number of lines and LENGTH be the number of characters in the input string (normalizing to single-spaces first).
If the character at LENGTH/N is a space, make the first cut there, and recursively call to split the remainder into N-1 lines, otherwise find the spaces at each end of the word containing this character and make trial cuts at both points with recursive calls again tom complete both cuts. Score the results somehow and choose the better.
I have implemented this as follows. For the scoring function, I chose to minimize the maximum length of lines in the split. A more complex scoring function might possibly improve the results, but this seems to work for all your cases.
public class WordWrapper {
public String wrapWords(String input, int lines) {
return splitWords(input.replaceAll("\\s+", " "), lines);
}
private String splitWords(String input, int lines) {
if (lines <= 1) {
return input;
}
int splitPointHigh = findSplit(input, lines, 1);
String splitHigh = input.substring(0, splitPointHigh).trim() + "\n" + splitWords(input.substring(splitPointHigh).trim(), lines - 1);
int splitPointLow = findSplit(input, lines, -1);
String splitLow = input.substring(0, splitPointLow).trim() + "\n" + splitWords(input.substring(splitPointLow).trim(), lines - 1);
if (maxLineLength(splitLow) < maxLineLength(splitHigh))
return splitLow;
else return splitHigh;
}
private int maxLineLength(String split) {
return maxLength(split.split("\n"));
}
private int maxLength(String[] lines) {
int maxLength = 0;
for (String line: lines) {
if (line.length() > maxLength)
maxLength = line.length();
}
return maxLength;
}
private int findSplit(String input, int lines, int dir) {
int result = input.length() / lines;
while (input.charAt(result) != ' ')
result+= dir;
return result;
}
}
I didn't actually bother with the special case of the lucky situation of the simple split landing on a space, and adding special handling for that might make it a little quicker. This code will in that case generate two identical "trial splits" and "choose one".
You might want to make all these methods static of course, and the recursion might give you a stack overflow for large inputs and large line counts.
I make no claim that this is the best algorithm, but it seems to work.
You can split based on the number of characters divided by n.
Then, for each line, you should add the end of the last word (which is the beginning of the next line, if the current line doesn't end with a space and the next line doesn't begin with a space), so that no words are split in the middle.
So if you have :
I me him is veryverylong wordvery longestwordever thisisevenlonger
And you wish to split it to two lines, you get :
I me him is veryverylong wordvery
longestwordever thisisevenlonger
In this case the second line already starts with a space, so we know that no word was split in the middle, and we are done.
If you split it to three lines, you first get :
I me him is veryverylo
ng wordvery longestwor
dever thisisevenlonger
Here some words were split, so you move "ng" to the first line, and then move "dever" to the second line.
I me him is veryverylong
wordvery longestwordever
thisisevenlonger
This is my solution using the split() function.
public class Textcut {
public static void main(String arg[]) {
String myText="I think that stackoverflow is a very great website";
int n = 2;
String[] textSplit = myText.split(" ");
int wordNumber = textSplit.length;
int cutIndex = wordNumber/n;
int i = cutIndex;
int j = 0;
while(i <= wordNumber) {
for(; j < i; j++) {
System.out.print(textSplit[j] + " ");
}
System.out.println("\n");
i = i+cutIndex;
}
}
}

How is this program coming along?

Instructions:
Write a program that will read a line of text that ends
with a period, which serves as a sentinel value. Display all the
letters that occur in the text, one per line and in alphabetical
order, along with the number of times each letter occurs in the text.
Use an array of base type int of length 26 so that the element at
index 0 contains the number of as. and index 1 contain number of bs etc.
package alphabetize;
import java.util.*;
public class Alphabetize
{
private static void number(String s)
{
int[] array = new int[26];
s = s.toUpperCase();
System.out.println(s);
for (int i = 0; i < s.length(); ++i)
{
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
{
++array[s.charAt(i) - 'A'];
}
}
for (int i = 0; i < 26; ++i)
{
System.out.println("|" + (char) ('A' + i) + "|" + array[i] + "|");
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String aString = ".";
while (true)
{
System.out.println("Please enter sentence with a period to end");
aString = keyboard.nextLine();
if (".".equals(aString))
{
System.exit(0);
}
number(aString);
}
}
}
Still having problem with the period thing.. it does not seem to work the way i did it.
Considering this is a homework and instructions are very specific, you should read the text character by character instead of using built-in functions
If your text file was something like
abcabca.
The output should be something a appears three times, b appears two times etc etc.
So your algo should be something like
Read next character
If char is period goto 5
If char is space goto 1.
If char is between a <-> z. update the counter in arr[0..25] and goto 1
output arr[0..25] one per line
Was it mandated that this assignment is done in Java? The whole idea of a "sentinal character" rather than just using a line terminator is pretty bizarre.
Anyway, you can achieve the behaviour you want by setting the delimiter of Scanner:
keyboard.useDelimiter("\\.");
As for the looping, a big hint is this:
int[] counts;
counts[chars[0] - 'a'] = counts[chars[0] - 'a'] + 1;
or simply
counts[chars[0] - 'a']++;
I'll leave it up to you to include that in a loop.
Edit
If you are looking for character-at-a-time input, I would suggest you use an InputStreamReader instead of Scanner for your input. Here's a basic skeleton of what that looks like:
Reader reader = new InputStreamReader(System.in);
while (true) {
int nextInput = reader.read();
if (nextInput == -1) {
System.out.println("End of input reached without sentinal character");
break;
}
char nextChar = (char) nextInput;
//deal with next character
}
Still, read() will typically block until either the end of input is reached (CTRL-D or CTRL-Z from most consoles) or a new line is sent. Thus the sentinal character is of limited use since you still have to do something after typing ".".
You have to check whether period is there at the end or not. So the last character should be '.'.
Then take the length of string before last '.'.
For the counting part create an array like u are doing :
int [] name = new int[26]
where each index starting from 0, 25 corresponds to 'a' till 'z'.
Now you put the string characters in a loop and have to check what that character is like :
if its a 'a' : increase the value at index 0 by 1.
if its a 'd' : increase the value at index 3 by 1.
like wise.
later you display the whole array with a, z along with indexes from 0 till 25.
Suggestion: If its not required to use an array, and you can use any other data-structure you can implement the same in a HashMap very easily. by keeping 'a', 'z' as the keys and count as the corresponding values. and then retrieving and showing the values will also be easier.
You need an int array (e.g., int[] counts = new int[26];) After you read the input line, examine it character by character in a loop. If the character is a not period, then increment the appropriate element of the counts array. (If the character is a, then increment counts[0]; if it is b, increment counts[1]; etc. Hint: you can subtract a from the character to get the appropriate index.) When you find a period, exit the loop and print the results (probably using a second loop).

Counting characters in a word

I want to create a program to output the number of occurrences of a letter in a word. Basicaly the output has to look like this:
Please enter a word—howdy
The word howdy has five characters.
What letter would you like to guess? (enter zero to quit) a
There are 0 A’s.
What letter would you like to guess? (enter zero to quit) b
There are 0 B’s.
This is what I got so far:
import java.util.Scanner;
public class LetterOccurence
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String word;
String letter;
int letterNumber;
boolean [] alphabet = new boolean [27];
System.out.println("Please enter a word- ");
word=input.next();
System.out.println("The word "+ word + " has " + word.length() + " letters\n");
String lower = word.toLowerCase();
for(letterNumber=0;letterNumber<lower.length();letterNumber++)
{
alphabet[lower.charAt(letterNumber) - 'a'] = true;
}
//do
//{
System.out.println("What letter would you like to guess? (enter 0 to quit) ");
letter = input.next();
if (alphabet[letter.charAt(0)-'a'] = true)
{
System.out.printf("There are %s's\n",letter.charAt(0));
}
else
{
System.out.printf("There are 0 %s's\n",letter.charAt(0));
}
//}while(Character.isLetter(letter.charAt(0)));
}
}
I am now stuck, any ideas on how to continue?
For a start, booleans only hold a true or false value, you can't count with them. Rather than in your loop setting each element in the array that maps to a certain character to true, you could simply increment the counter.
In this case you're going to require an int as opposed to a boolean. And to print out the amount of occurrences for a specific letter, you obviously just print out the int value at that specific index of the array.
An alternative is also a Map<Character,Integer>, rather than calculating the index for specific chars in your array.
EDIT: As Dave mentioned, your if statement is also broken.
If nothing else, that if statement in your commented-out do/while is using a single = (assignment) instead of == (comparison).
All you really need to do is keep a single count per guess, initialize it to zero, and each time you encounter the guessed letter, increment it.
If you're trying to do all that work "up front" before guessing, then you don't really need a map of booleans, you need a map of ints, one per lower-case letter, initialized to zero, and incremented each time its corresponding letter is guessed.
You're very close.
You don't want a boolean array for alphabet -- that only tells you what letters appear in the word, but not how many of each. Try this instead:
boolean [] alphabet = new boolean [27];
for(letterNumber=0;letterNumber<lower.length();letterNumber++)
{
++alphabet[lower.charAt(letterNumber) - 'a'];
}
Then later:
System.out.printf("There are %d %s's\n",alphabet[letter.charAt(0) - 'a'], letter.charAt(0));

Categories

Resources