This problem asks us to return all words in a string(which can contain weird cra
ID: 3650905 • Letter: T
Question
This problem asks us to return all words in a string(which can contain weird crap like numbers and symbols) that are longer than five letters in a new array.Here is what I have so far:
Java Code:
public static String[] getBigWords(String sentence){
//sentence = sentence.replaceAll("\d","");
String [] temp = null;
temp = sentence.split(" ");
int size = 0;
int k =0;
for(int i = 0; i < temp.length; i++) {
if(temp[i].length() > 5)
size++;
}
String[] big = new String[size];
for(int i = 0; i < temp.length; i++) {
if(temp[i].length() > 5){
big[k] = temp[i].toString();
k++;}
}
return big;
}
I am getting the following errors:
Problems Detected:
*****?*****Word '67000120909' incorrrectly returned in result
*****?*****Word '6A7B0C0V0D1E2S0D9D09' incorrrectly returned in result
*****?*****Word 'location' not returned in result
*****?*****Word 'son-in-law' incorrrectly returned in result
Fails When:
*****?*****Long non letter strings
*****?*****Punctuation
*****?*****Punctuation, duplicate words, beginning/middle/end
--> Is there a regex expression that has everything but characters in it so I can place it in my replace function?
Thanks.
Explanation / Answer
Please rate...
Program BigWords.java
===================================================
import java.util.Scanner;
class BigWords
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.print("Enter the sentence: ");
String sentence=s.nextLine();
sentence=sentence+" ";
String ar[]=new String[1000];
ar=getBidWords(sentence);
int i=0;
while(ar[i]!=null)
{
System.out.print("""+ar[i]+"" ");
i++;
}
}
public static String[] getBidWords(String sentence)
{
int i=0,c=0,present=0;
String words[]=new String[1000];
String t="";
while(i<sentence.length())
{
if((sentence.charAt(i)>='a' && sentence.charAt(i)<='z')||
(sentence.charAt(i)>='A' && sentence.charAt(i)<='Z'))
{
t=t.substring(0,present)+sentence.charAt(i);
present++;
}
if(sentence.charAt(i)==' '||sentence.charAt(i)==' ')
{
if(t.length()>5)
{
words[c]=t;
c++;
}
t="";
present=0;
}
i++;
}
return words;
}
}
=======================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.