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


ADVERTISEMENT

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

Value Assign To A String

Sep 25, 2014

I am doing an assignment where I have to find the price per square inch of a pizza, compare them and display the results. I have everything figured out with the values and stuff. Now when I have to displays the results I have to display which of the two pizzas is more favorable.

I have both values / square inch for both. and I know how to find the minimum value of the two wit the Math.min class.
My question is how can I assign the char, PIZZA A to the value that I had so I can display it in the output statement, without writing PIZZA A. It should display after the difference is calculated.

Here's my code so far.

// This programs finds the price per square inch of a pizza

import java.util.Scanner;
import java.text.DecimalFormat;
public class PizzaSquareInches {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.###");

[Code] .....

View Replies View Related

Set And Assign Doubles - String For Gross Pay?

Dec 15, 2014

I'm working with doubles I'm trying to figure out how to set this up so it works?

I'm just stuck on how to set and Assign a double FedTaxWitholding that gets the Fed Tax Withholding = Gross Pay * Fed
Tax Withholding Rate.

Do I need a string for Gross pay?

View Replies View Related

Assign A Value To Numeric Variable Then Manipulate It And Return A New String

Aug 18, 2014

At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.

Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:

A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.

import java.util.*;
public class Day {
static Scanner readinput = new Scanner(System.in);
String day;
public Day(String day) {

[Code] ....

So right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).

View Replies View Related

Word Cannot Be Resolved To A Variable Error

Jun 27, 2014

I am getting an error stating that 'word cannot be resolved to a variable' pointing to my return value. Why? Should I make a default String value for word before my while loop or something, like

String word = " ";
?

String getItRight(){//make sure the user enters the password correctly
Scanner input = new Scanner(System.in);
boolean repeating = true;
while(repeating){

[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

Cannot Assign Cloned String Array To Generic Type Array

Jun 21, 2014

I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.

This is the incorrect line: ar[r] = maze[r].clone();

My code:

private String[][] maze = {{"*","*","*"," ","*","*","*","*","*","*"},
{"*"," ", "*"," "," "," ","*"," ","*","*"},
{"*"," ","*","*","*"," ","*"," ","*","*"},
{"*"," "," "," "," "," "," "," "," ","*"},
{"*","*","*","*","*"," ","*","*","*","*"},
{"*","*","*","*","*"," ","*","*","*","*"}};
//private String[][] mazeCopy = copyMaze(new String[6][10]);
private <T> T[][] copyMaze(T[][] ar){
for (int r = 0; r < ar.length; r++){
ar[r] = maze[r].clone();
}
return ar;
}

My compiler says: Required: T[]. Found: java.lang.String[]

Since ar[r] is an array and .clone() also returns an array why is this line incorrect.

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

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

Read Line From File And Save First String As Word And Remaining As Meaning

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

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







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