Binary Tree - Storing Each Word As String And Int Frequency For Each Time Word Occurs

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


ADVERTISEMENT

Hangman Type Game - How Many Times Each Guessed Letter Occurs Within The Word

Jul 31, 2014

The game runs fine, however I have to include how many times each letter that is guessed occurs within the word. For example, if the guess is e, the program should print out: The character e occurs in 3 positions. How would I go about doing this??

/*
* A program that plays simple word guessing game. In this game the user provides a list of words to the program.
* The program randomly selects one of the words to be guessed from this list.
* The player then guesses letters in an attempt to figure out what the hidden word might be.
* The number of guesses that the user takes are tracked and reported at the end of the game.
*/

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

[Code] .....

View Replies View Related

AutoCorrect / Replace A Word That Is Similar To A Word In File Text

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

I/O / Streams :: How To Use Java To Generate Word Document From A Word Template

Aug 19, 2014

I am looking for java codes to generate a word document based on a word template, basically, I have a word template created and in my local path, the template has a proper format with some fields which will be filled in after java codes ran. The java codes will fetch one record from a table, and open the word template and then fill the fields in the word template, and created a new word document and save it in another folder.

I found this example: [URL] which is similar except it uses xml template instead of word template, how to make it work to change the template from xml to word (docx) template?

View Replies View Related

How To Count Frequency Each Number In Array Occurs

Dec 14, 2014

I'm trying to write a program that counts the number of times each number in an array occurs. In theory I understand how it work, you take the first element in the array, compare it again the rest of the array and any time it occurs in the array you increment a counter by 1. Then move onto the next element A[1] and so on...

This is what I've done so far :

public static void main(String[] args) {
int[] array = {5,6,2,5,2,2,0,1,6};
int count = 0;
for(int i = 0; i <array.length; i++) {
int val = array[i];
for(int j = i+1; j<array.length; j++)

[Code] .....

I think I'm on the right track as it correctly prints out the frequency of the first 3 elements, but I'm definitely going wrong somewhere as it just doesn't do it right after that!

View Replies View Related

Comparing Typed Word To The Set Word In Object

Jul 1, 2014

Here's My Code.

package bin;
import java.util.Scanner;
public class AppletMain{
public static void main(String[] args){
Scanner oneinput = new Scanner(System.in);
String one;

[Code] ....

I am trying to get it to compare the word I type in to the set word in the object 'secretword'. I have tried everything from equal to == to compareTo, I even created a two hundred line program to do this SIMPLE problem.

View Replies View Related

Word Game (how To Check That Word Is In Dictionary)

Apr 21, 2014

import java.io.*;
import java.util.Random;
import java.util.Scanner; 
public class WordGame
{
public static void main(String[] args) throws IOException
{
final int RANDOM_LETTERS_COUNT = 10;
final int TRIALS = 10;
int score = 0;

[Code] ....

View Replies View Related

Capitalize Second Word Of A String

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

String Searching For A Word

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

How To Select Each Word From String

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

Getting Error When Assign Word To A String?

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

Count Length Of Each Word In A String

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

Count The Length Of Each Word In A String

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

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 View Related

Counting Length Of Each Word In String

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

String Of Words - Getting First Letter From Each Word?

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

How To Compare String In Binary Search Tree

Aug 31, 2014

I'm trying to implement a Binary Search Tree that accepts strings. I cannot figure out how to compare the string value in my add method against the node object. While I could make the node class data be the string type I am trying to make the code be as reusable as possible. So my question is this, is there a simple way I can compare the two that I am missing?

public class BTNode<E> {
private E data;
private BTNode<E> left, right;
//constructor
public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight) {
data = initialData;
left = initialLeft;
right = initialRight;

[Code] ....

View Replies View Related

Create Binary Tree From A String Of Letters

Jun 15, 2014

Here is the problem:

Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that this will not be a search tree; there’s no quick way to find a given node. You may end up with something like this:

It also says all Letters must be Leaves

Now I had it almost similar to that picture, but it wasn't right. So ive been working on it but im getting some very strange (and frustrating) output from the following methods.

Ive included the display method just for reference. The book told me to use it so I haven't edited it. I believe my main issue is with my (incomplete) insert() method. The output goes into an infinite loop despite having a return statement break the while loop when a character is inserted.

The way I see to solve the problem is just add a (+) whenever a new subtree needs to be created. Say I add A and B, then it first creates a subtree at the root with a (+) and afterwards lists A and B as its leaves. If I insert a C, it should be able to simply move to the right child of the root and deposit the C there.

package pkg4333_hwk1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class Main {

[Code] ....

View Replies View Related

Finding A String Word In Char Array

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

String Method That Creates A Word Out Of Given Letters?

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

Convert The First Letter Of Every Word In A String To Uppercase?

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

Extracting Numbers In Word Format From A String Of Text

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

String In Java - Search Text On Document For A Word

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

Split String From Given File Find Word Position

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

Converting String To Title Case But Ignoring Specific Word

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

Scanner That Scans Console For Input To Fetch String Word

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







Copyrights 2005-15 www.BigResource.com, All rights reserved