Java Recursion Word Pyramid?

Apr 26, 2015

I was asked to create a word pyramid program using recursion. I understand the concept of recursion (i.e. a method calling itself with the problem it is solving getting simpler and simpler each time) but I am having issues writing the program. Here is my code:

public static void main(String[] args) {
//This program will create a word pyramid by using a recursive algorithm.
//For example, the output of "DOGGY" should be:
//DOGGY
//OGG
//G
//This program will also use a recursive algorithm to accomplish this.
String userWord = JOptionPane.showInputDialog(null, "Hello and welcome to the Word Pyramid program."

[Code] .....

So, if I input HORSE the output should be:

HORSE
ORS
R

View Replies


ADVERTISEMENT

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

Recursion Pattern In Java

Jul 16, 2014

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25. For example, if the value was 4, the pattern would look like this
 
*
* *
* * *
* * * *
* * *
* *
*
 
The values are stored in a file entitled prog3.dat which looks like this
 
4
3
15
26
0
 
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem. Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
 
import java.util.Scanner; 
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);

[Code] ....
 
Output:
 
Please enter an integer
*
* *
* * *
* * * *
Please enter an integer
*
* *
* * *
Please enter an integer
 
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
Please enter an integer
Please enter an integer
 
As you can see, I don't know how to make it print the pattern like in the example and am honestly not even sure if this is recursion since I've never actually worked with recursion before.

View Replies View Related

Java Recursion In Minesweeper Game

Nov 16, 2014

Recursion in a MineSweeper game. So far, when a user clicks on a square and it has a bomb on it, I reveal the bomb to the user - see clicked(). I also have the ability to show the user if the squares surrounding them have any adjacent bombs - see countAdjacentSquares().

However, I am having trouble with my fillZeros() recursive method. When a user clicks on a square that has zero bombs in its surrounding squares, it not only reveals that square, but also any adjacent squares (horizontally, vertically or diagonally adjacent) that also have zero bombs in its surrounding squares.

I don't have any recursion, if I click on a square with no bombs all it does it set the square to 0, but doesn't continue to check for other empty squares.

Java Code:

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Square extends GameSquare
{
private boolean revealed = false;
private boolean squareHasBomb = false;
public static final int MINE_PROBABILITY = 10;
public int mineCount = 0;

[Code] ....

View Replies View Related

Java Code Using Recursion - Reversing Sentence

Mar 15, 2015

So I have to design a program that takes a word and reverses the order of it. The exact directions are:Write a recursive method void reverse() that reverses a sentence. For example:

[code = Java]
Sentence greeting = new Sentence("Hello!");
greeting.reverse();
System.out.println(greeting.getText());
[/code]
prints the string "!olleH".

Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two. So basically my biggest issue is my method to actually reverse the word.

public class Sentance
{
private String Word;
public Sentance (String aWord)
{
Word = aWord;

[code]....

View Replies View Related

Java Recursion Method Is Not Working - Summing Elements In Array

Feb 2, 2015

I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.

This is what I have so far:

public static void main(String[args]){
int size = kbd.nextInt();
int [] myArray = new int [size]
//user inputs the elements of array

[Code]....

View Replies View Related

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

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

Trying To Create Pyramid Using For Loop

Jan 30, 2014

pyramid.jpg

Write a for loop for creating a pyramid starting on the platform with the bottom line consisting of 10 boxes then 8 then 6 and so on. I have attempted making the first row but when i run the program the boxes all split up and fall over.

View Replies View Related

Building A Pyramid From Blocks?

Apr 15, 2014

I am trying to make a pyramid from blocks . I know that for example to make a bigger block from small blocks you write something like :

for(int i = 10 ;i <=1 ; i--){
for (int j= 10 ;j <= 1 ; j--) {
< statement to create block where you use both i and j to define the location of each block>
}
}

but i cant seem go grasp how to make a pyramid , so far my best effort is :

int i =10 ;
while (i >=1 ) {
for( ; i>=1 ; i-- ){
< statement to create block > }
}

At the moment visually i don't get a pyramid but it might be because of other issues and not because of the while and then a for inside it approach .

My approach is correct and if now what would be the correct one to create a pyramid from smaller blocks ?

View Replies View Related

Hexagon And Pyramid Formula

Mar 30, 2014

What is accurate formula area and perimeter for pyramid and hexagon? I got stuck of my assignment.

View Replies View Related

Simple Pyramid Program

Jun 28, 2014

I was learning Java and there was this exersize to construct a simple pyramid of prespecified height and width. The program i wrote is turining up with the wrong result.

package asgn2;
import acm.graphics.*;
import acm.program.*;
public class pyramid extends GraphicsProgram {
private static final int BRICK_WIDTH = 30;
private static final int BRICK_HEIGHT = 12;
private static final int BASE_BRICKS =14;

[code]...

View Replies View Related

Stuck On Pyramid Program

Feb 13, 2014

I have spent some time thinking about the program, and realize I need two loops-- 1 that keeps track of bricks in a row and 1 that keeps track of the number of rows as the Pyramid rises in height. The Pyramid is supposed to be centered on the bottom of the screen. Three CONSTANTS have been required:

BRICK_WIDTH=30, BRICK_HEIGHT=12, BRICKS_IN_BASE=14.

I tried breaking the scope of things down by trying step-wise refinement. But, I got bogged down. When I tried a method where both loops were included, I got limited success. For some reason, my equation to center along the x axis seems to be doubling the number of bricks. But, the correct number of total rows of bricks was a success. Also, the Pyramid was centered on the bottom of the screen. Another frustrating failure was that the number of bricks would not decrease as the new layers were created. My thought process was instead of using a loop that counts up (i++) I would use a loop that counts down (j--) which I thought would reduce the number of brick in each row as the "y" coordinate was reduced by each count of the BRICK_HEIGHT.

This is the code that "works". Whenever I add the (j--) the results are NOT what I expect.

/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the sample run in the assignment,
* but you should make sure that changing these values causes the generated display to change accordingly.
*
[code].....

View Replies View Related

Print Pyramid String Error

Nov 18, 2014

I'm having a small error with my code, it is getting rid of the first letter of the string after the first line printed.

import java.util.*;
public class PrintPyramidName {
public static void main(String[] args) {
System.out.println("What is your name?");
Scanner input = new Scanner(System.in);

[Code] ....

View Replies View Related

Write Nested Loop For Pyramid Pattern

Sep 26, 2014

Write a nested loop for a pyramid pattern. Well here's an ugly description of what it should look like:

1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 128 64 32 16 8 4 2 1

I managed to make the pyramid using addition. However, how to turn it into multiplication x 2.

import java.util.*;

public class NumbersPyramid
{
public static void main(String[] args) {
int odd = 1;
int numSpaces = 7;

[code]....

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

Prompt User To Enter Integer From 1 To 15 And Displays A Pyramid

Jun 1, 2014

The exercise sounds like this : Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid , as shown in the following sample run (my code displays correctly the first 9 lines):

1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7

The problem is when i input a number greater then 9 as it requires 2 spaces . I m pritty sure i solved it incorrectly or at lost not optimal as i m using a string that decreases on each line to create the pyramid effect.

import java.util.*;
public class C5_17 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of lines: ");

[Code] ....

View Replies View Related

Java Cannot Resolve Word Into Type?

Nov 3, 2014

I am creating a user friendly program that allows the user to adjust the dimensions of an oval (painted onto a window) via a 'slider' Everything is fine for the most part, except in the main code- when I tried coding for a window object for some reason java didn't recognize the type (that is, I used the keyword TheWindow and java didn't recognize it) the following is the full code set across 2 classes, but I think the problem is centered around the main class.

package javaIntermediate;
import java.awt.*;
import javax.swing.*;
//NOTE: this is 1 out of 3

[Code]......

View Replies View Related

Word Document Dynamically Using Java Code

Jul 31, 2014

I need to generate the word document dynamically using java code, included the necessary jar files, No compliation issue, but During run time am getting this error: Could not initialize class org.openxmlformats.schemas.wordprocessingml.x2006. main.CTDocume .using all these jar files: POI-3.6.jar, POI_3.9.jar, Poi-ooxml-3.5,Poi-ooxml-3.6,Poi-ooxml-3.7,Poi-ooxml-3.9,Poi-ooxml-schemas-3.6,Poi-ooxml-schemas-3.9.jar. using the XWPFdocument class. when my cursor get into that line XWPFDocument doc = new XWPFDocument(); getting the above error.

View Replies View Related

EJB / EE :: How To Use Java Codes To Send Email With Word Attachments

Aug 9, 2014

I have a request to create a java scheduled job to send email with attachment of word document every week. any example codes I can use. This is my first time to code this request, I do not what is the standard way to do it.

View Replies View Related

Java Program For Counting Specific Character In Word

Jun 27, 2014

I am trying to make a java program which should count the occurrences of a specific character in a string. I have 1 error - "cannot find method charAt(int)". Here is what I have.

import java.util.Scanner;
public class ch4q5 {
public static void main(String[] args) {
String input ;
String t ;
int c = 0;

[Code] ....

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

Copying Netbeans Java Code To Word With Format?

Jun 15, 2014

I need to copy my neatbean code to word, (school stuff) now I know this is possible, cause I see it on internet often.

I tryed copying it to notepad++ and then to word... wont work the code will still all black.

View Replies View Related

Scanning And Displaying Every Word From A Website Source Code Java

Feb 19, 2014

I have been given a task to scan the contents of a websites source code, and use delimiters to extract all hyperlinks from the site and display them. We havent been told anything about how to do this so after some looking around online this is what I have so far:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

[Code] ....

So my program can extract each line from the source code of a website and display it, but realistically I want it to extract each WORD as such from the source code rather than every line. I've looked around online but I don't really know how it's done because I keep getting errors when I use input.read();

How to make it extract each word from the source code?

View Replies View Related

Java Program To Translate English Into Pirate - Word Pair Class

Apr 5, 2015

I am making a java program that translates English into Pirate. I need working with the Word Pair class. I'm not sure what needs to be passed to the default constructor. And how to do the method that will take the words and pair them. here is my code and the dictionary file

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner; 
public class PirateTranslator {
public static void main(String[] args)
 
[Code] .....

View Replies View Related







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