Display Longest Word In String
Dec 31, 2014
i want a simple,beginner program to accept a string from the user and display the longest word in the string.i thought of taking the length of the string and choosing the longest string.but if the user enters a big paragraph,how can i store each word in a variable??
View Replies
ADVERTISEMENT
Apr 27, 2015
I have built a binary tree, from a file. In each node, I am storing each word as a string, and an int frequency for each time the word occurs. For the assignment, I need to find how many words occur only once in the file. I wrote the program, but for some reason I am getting a number different from what my professor is expecting.
As far as I know, this file has loaded into the tree correctly, because all of my other answers in the assignment are correct. What am I doing wrong?
public void findUnique() {
System.out.println("There are " + findUniqueWords(root, 0) + " unique words.");
}
private int findUniqueWords(Node subTree, int uniqueCount) {
// Base Case: At the end of the branch
if(subTree == null){
return uniqueCount;
[Code] ....
View Replies
View Related
Dec 2, 2014
I am doing project on glossary in java. Basically what i need to do is, i have a case study in the text field, when i mouse over the certain text that i already store in database. The text will be highlighted and automatically display the tool tip and content the data that i stored in my database.
View Replies
View Related
Feb 16, 2014
So the exercise I'm working on says to have the user enter their name and the program will output their name with the last name in all caps. i made it work BUT the only way i could figure it out was to ask for the first and last names separately creating two strings rather than one.
Of course I want to make it work how it's supposed to (with one string) so that I'm learning. I'm just having trouble conceptualizing how exactly (with varying lengths of names) to tell the program to only capitalize the second word... at first I thought create a substring beginning with the first letter of the last name and ending with the last...but again, therein lies the issue of varying name lengths.
is there a way to create a substring that beginIndex's at the first "space"? then i could just leave the endIndex parameter empty and it would take the whole word into a new string. and from there utilize toUpperCase to the new string?
Here's my code asking for the first and last names separately.
import java.util.Scanner;
class nameEcho {
public static void main ( String [] args ) {
Scanner scan = new Scanner( System.in );
String first;
String last;
[Code] .....
View Replies
View Related
Mar 25, 2015
I'm very new to Java, and I'm writing a code to search a string to see how many times the word "dog" is found in it. I'm not sure if this is error-free or the most efficient, but I'd like to keep it simple.
public void run()
{
String input = new String("The Dogman was no ordinary dog, nor man, but rather a peculiar dog-like man who barked like a dog, and panted like a dog, he even ate like a dog. He owned a dog named Doglips, and interestingly enough, his favorite food was hotdogs.");
println(input);
int index = -1;
int count = 0;
print("Counting dogs:");
inputarray = input.split(" ");
[Code] .....
View Replies
View Related
Oct 1, 2014
What I need to do is ask the user to input some text and then turn it into pig latin. I am confused on how to select each word from the string to determine if it ends in a way or ay. I am to assume that the letters are all lowercase and the text ends with a period.
import java.util.Scanner;
public class Trial
{
public static void main(String [ ] args)
{
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Jan 29, 2014
Below is my first ever code. I bolded where i am getting a syntax error. Why am i getting an error here if i assigned that word to a string?
public class BeerBottleTest {
public static void main(String[] args) {
int beerNum = 99;
String bottle = "bottles";
while (beerNum > 0) {
if (beerNum == 1) {
bottle = "bottle"; }
[Code] ....
View Replies
View Related
Aug 5, 2014
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
View Replies
View Related
Jul 31, 2014
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{
[Code].....
View Replies
View Related
Aug 5, 2014
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 lenghth 0
world
There are 0 words of lenghth 1
how
There are 0 words of lenghth 2
are
There are 3 words of lenghth 3
you
There are 0 words of lenghth 4
I think I need to use string.split instead of stringtokenizer and the while loop is incorrect it needs to loop equal to the number of letters in the longest word. But how to do either of these ?
View Replies
View Related
Mar 26, 2014
If I have a string of words and want to get a letter from each word (let's say the first), how do I code for anything past two?
For example, if I have North Atlantic Treaty Organization and I want to convert it to NATO I would do something like:
charAt(0) for N
charAt(x + 1) for A
But what do I do for anything past the second string? If I do charAt(x ++ 1) I get an error.
View Replies
View Related
Feb 13, 2015
I'm trying to find a word in an array of char.....but I'm stuck. How to formulate the code to step through the array and pick out the word. This is what I have so far...
public static void searchAcross(String string, char[][] puzzle) {
// Gets the number of rows in the matrix
int rowLength = puzzle.length;
//Gets the number of columns in the matrix.
int colLength = puzzle[0].length;
[Code] ....
View Replies
View Related
Jan 25, 2015
I want to make a method that takes a word and then checks if the word can be created from available letters. For example, if a word "johnson" can be created by using letters "jashoqwnon".
Now my goal is to make sure that if available letters contain a letter from the word, that letter is put into a String called result and then erased from the list of given letters. So, "johnson" and "jashoqwn" would produce the result "johns" and leave "aqw" unused.
Now the problem that I am facing is that I can't get Java not to use the same letter twice. So "johnson" and "jashoqwn" still gives "johnson".
I've tried everything in my power but I am missing something. Here is my code.
public static String makeAWord(String word, String letters){
String result = "";
for(int i = 0; i < word.length(); i++){
for(int j = 0; j < letters.length() ; j++){
[Code] ....
View Replies
View Related
Sep 20, 2014
I'm trying to convert the first letter of every word in a String to uppercase. I've managed to isolate the first letter of every word and then make it uppercase but I don't know how to replace it.
public static StringBuffer makeUpperCase(){
String str = String.valueOf(input2);
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == ' '){
char var = str.charAt(i + 1);
var = Character.toUpperCase(var);
System.out.println(var);
}
}
View Replies
View Related
Aug 7, 2014
I am trying to do is extract numbers that are in word format in a long String, i.e. a song, and return each of their numerical values, in order to add them all up. So I'd like to calculate the sum of all of the numbers in the text. This has to work for any piece of text and for all numbers up to a trillion.
So I broke the string down into tokens and stored them in a String []. And I divided up the possible numbers in word format into:
LARGEST: thousand, million, billion, trillion
HUNDRED: hundred
TENS: twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety
UNITS: one, two, three, four, five, six, seven, eight, nine
SPECIALS: ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen
I believe that these are the only words that it will need to recognize. I began reading the tokenized string from right to left and then when I came across a unit, special or tens as the first number I hit, I would then set it's numerical value and check if the word before was also a number and whether to add or multiply etc. i.e. First number hit is a two, if the number before is sixty, then I would just add it to sixty and check the word before that and so on.
However, when implementing it, it seems like an extremely long way around it. How I could implement this in a swifter manner? An example of it working would be:
"Nine Million rockets turned Three times and met Twenty Two Aliens", it would extract, Twenty Two as 2, then 20 = 22, then extract Three as 3, and then Nine Million as 1,000,000 x 9 = 9,000,000
9,000,000 + 22 + 3 = 9,000,025
View Replies
View Related
Feb 5, 2014
Consider in a Document if a String " Hello" is Encoded and stored as "XYZAB"
I want to search the text on document for a word "Hello" and Replace the word with "HelloWorld"
The Program will encrypt the word "Hello" and Search the file then return the encrypted code as "XYZAB" Found
Now i have to replace the word "Hello" with "HelloWorld" in encrypted form so that the Letter "XYZABEFGHI" is replace in the place of Hello where "World" is encoded as "EFGHI"
Now the Problem is If there is more number of occurrence of the word "Helloworld" exist in the file... How can i Replace only one particular occurrence What can be done to select the particular occurrence.
I have attached my java program for Encryption along with this mail for your ease of use.
View Replies
View Related
May 4, 2014
I have included split() to put a string read from a given file into indexed array. Looking for a word position (not char position number in addition to the line number I have already written. Line number works fine, however word position isn't quite right.Below is my code:
import java.io.*;
public class Word implements Comparable, TreeComparable{
String word;
int count;
int wordpos;
ObjectList lines;
private SuperOutput so;
[code]....
View Replies
View Related
Feb 23, 2014
I need to turn some strings from:
'1-the-high-street'
to:
'1 The High Street'
But...
if string is
'the-dog-and-duck'
I want to achieve
'The Dog and Duck'
...whereby the 'and' is NOT converted to title case
My code as follows:
String propertyPageTitle = "1-the-high-street";
propertyPageTitle = propertyPageTitle.replace("-", " ");
propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);
System.out.println(propertyPageTitle);
prints: '1 The High Street'
How could I handle the second scenario so it does not convert 'and' to titlecase?
View Replies
View Related
Jul 28, 2014
I'm having an issue, I have a scanner (Scan.nextLine();) that scans the console for input to fetch the string "word". Then I want to fetch a character using Scan.findInLine(word).charAt(number);. The problem is that the console requires me to write 2 lines in order for the program to move on. I only want the program to scan for a word, and then move on with what it has instead of requiring 2 inputs.
View Replies
View Related
Jul 6, 2014
I have written the code to read the line from file and save first string as word and and remaining string as meaning..
E.g.: innovation a new method, idea, product, etc.
word = innovation
meaning = a new method, idea, product, etc.
In my code there is no error and have a mistake what it is means first assigning word is ok and while saving meaning ..it saves like
{
a a new a new method } like that
Java Code:
import java.io.BufferedReader;
import java.io.FileReader;
public class Dil{
public static void main(String[] arg)throws Exception
{
BufferedReader in;
[Code] ......
View Replies
View Related
Feb 18, 2015
I have some N*M matrix or N*N matrx , and there's a "worm" that can start from any index in the first column, or, any index in the first row. i need to choose the longest worm that satisfying this :
The index that comes after the index before him must be greater then 1. and i must use recursion, also for helper methods. no loops at all. that's it. i'm having a problem to find the longest worm in the matrix.
My idea was to create an helper array and assign to the array indexes a variable that will count the steps of the worm and finally assigns in to the helper array index. then i used findMax method to find the max value in an index. and thats will be the longest worm. i wrote a lot of code so i wont put it here. i will say that i'm close. if the longest worm is 6 i get in my output 7.
View Replies
View Related
Oct 30, 2014
I have a given array of numbers, and I have to find the longest decreasing sub-array, but the thing is that the elements don't have to be next to each other!
For example, the longest decreasing sub-array for this array : 546 -156 165 -156 -56 -13 5
is 3 (check the bold numbers)
Until now, I have nearly finished my code, but I have one problem...
private static int decreasing(int[] a) {
int result=1, temp=0, br=1;
//Checking all the elements one by one:
for(int i=0;i<a.length;i++){
temp=a[i]; //placing the element on a temp value
[Code] ....
The problem with this code is that it's not smart.. let's say I have : 100 -500 90 80 70
Once it hits -500, none of the other if's with pass....
View Replies
View Related
Oct 26, 2014
From a given array of positive and negative numbers, I have to find the longest SUB-Array which represents a Zig-Zag sequence...
A Zig-Zag sequence means that one number is possitive, the next one negative, the next one possitive, and so on and so on...
Like this: -1, 4, -5, 6, -9, 2, -9 etc....
So that means, if the array looks like this:
1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3
the longest sub-array which fulfills the requirement is (the part in bold):
1, 4, -2, -5, 6, -9, 1, -4, 9, -8, 7, 4, -3
and I only need it's length, which in this case is: 8
View Replies
View Related
Oct 24, 2014
I am trying to find the longest descending sequence without arrays. So 65124976231 would output 9762.
import java.util.*;
public class HelloWorld {
public static void main(String[] args){
String num = "";
int longestLen = 0;
int currLen = 0;
String max = "";
[Code]...
I keep getting: The longest descending sequence is: 6512 In an infinite loop.
View Replies
View Related
Jul 13, 2014
My problem is that I can't even run the program, because it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at domashno.ten.longestSortedSequence(ten.java:37)
at domashno.ten.main(ten.java:17)
Code :
public static void main(String[] args) {
int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12};
longestSortedSequence(arr);
System.out.println(longestSortedSequence(arr));
}
public static int longestSortedSequence(int[] arr) {
[Code] ....
View Replies
View Related
Mar 13, 2015
The intentions of this program is to prompt a user to enter a file name, and then reads the file. The program will prompt the user to enter a word that needs to be corrected. So lets say I have a text file containing "My name is OP and I Like goind to the Park!" I want to change "goind" to "going",
Now, my second method "isSimilar" executes a similar word with more than one same letter and same length, but I dont know how to execute that whole thing in my third method "correctThisLine" . How I can call that isSimilar method and read in that text file and change that word into that?
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class WahidMuhammadA3Q2{
String fileName = "AutoCorrectMe.txt";
public static void main (String [] args){
[Code] ....
View Replies
View Related