سؤال

I am trying to write a few methods for my homework assignment but I don't know what to do.

I've tried a bunch of things but none seemed to work.

First Question is how do i take a string I have and return it with no spaces. trim() only eliminates the first and last whitespace of the sentence(getNoSpaceString method).

Also I am trying to count the number of digit words my sentence has aka one is 1 digit word.

My code is below(digitWordCount method).

import java.util.*;
public class StringProcessor {
private String noSpaces;
private String input, noVowels;
private String noDigitWords;
private int numOfWords = 0, uppercaseLetters = 0,
            numOfDigits = 0, digitWords = 0;

public StringProcessor()
{
    input = "";
}

public StringProcessor(String s)
{
    StringTokenizer str = new StringTokenizer(s);
    numOfWords = str.countTokens();

    for (int i = 0; i < s.length(); i++)
    {
        if (Character.isUpperCase(s.charAt(i)))
            uppercaseLetters++;
    }

    for (int i = 0; i < s.length(); i++)
    {
        if (Character.isDigit(s.charAt(i)))
            numOfDigits++;
    }

    if (str.nextToken().equalsIgnoreCase("one"))
        digitWords++;


}

public void setString(String s)
{
    input = s;
}

public String getString()
{
    return input;
}

public int wordCount()
{
    return numOfWords;
}

public int uppercaseCount()
{
    return uppercaseLetters;
}

public int digitCount()
{
    return numOfDigits;
}

public int digitWordCount()
{
    return digitWords;
}

public String getNoSpaceString()
{
        return noSpaces;
}

public String getNoVowelString()
{
    return noVowels;
}

public String getNoDigitWordString()
{
    return noDigitWords;
}

public static void main(String[] args)
{
    String input;
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a line of text: ");
    input = keyboard.nextLine();

    StringProcessor str = new StringProcessor(input);

    System.out.println("\nwords: " + str.wordCount());
    System.out.println("uppercase: " + str.uppercaseCount());
    System.out.println("digits: " + str.digitCount());
    System.out.println("digit words " + str.digitWordCount());
    System.out.println("line with no spaces: ");

}
}
هل كانت مفيدة؟

المحلول 2

First Question is how do i take a string I have and return it with no spaces. trim() only eliminates the first and last whitespace of the sentence(getNoSpaceString method).

String input = "  Hello World  ";

You can achieve this by:

String input2 = input;

input2 = input.replaceAll(" ", "");

Take a look here for more information

Second question:

Also I am trying to count the number of digit words my sentence has aka one is 1 digit word.

If you're trying to count number of words, then:

int words = input.trim().split(" ").length;

System.out.println("number of words: " + words);

Here's how split works Let me know it this isn't what you're looking for for 2nd question

نصائح أخرى

For your first question, try finding out how to replace " " which is a whitespace into "" which is nothing (there are also string functions for this in case you've seen this in your classes already)

For your second question it might help to know that a string is an array of characters, again, in case you have seen string functions already you should probably look into the documentation you've gotten around that (or google it)

I hope you don't mind me not giving you straight up answers, seeing as this is a homeworks assignment you should probably just find it yourself, I just thought i'd get you on the right track.

If you care less about time complexity, you can just use a for loop to iterate the string passed in. make another string to hold what to return, say, call it toReturn. in the for loop, all you need to do is to determine if this character is a " ". Use .charAt(i) to do so. if it's not, just attach it to the toReturn. after the for loop, you will have what you want to return.

For the first one, although there is a way to do it using library methods, given that it's a homework assignment, you should probably be looping through the characters one by one and then adding them to a new "result" string if they meet the criteria (i.e. not a space).

For the second one, there is a library method which you can use to split the string on spaces, which will give you a list of the words. You should have also created a list of each of the 10 "digit" words. Then you can iterate over your list of input words and see if they match any of the digit words.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top