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


ADVERTISEMENT

Take Replacement String Entered By User And Print Out New String

May 22, 2014

I'm having trouble with the last few lines of the code. It's supposed to take a replacement string entered by the user and print out the new string. For some reason it's now allowing me to enter a replacement string

import java.util.Scanner;
public class Project02 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a long string: ");
String lString = keyboard.nextLine();

[Code] ....

Output:

Enter a long string: the quick brown fox jumped over the lazy dog
Enter a substring: jumped
Length of your string: 44
Length of your substring: 6
Starting position of your substring in string: 20
String before your substring: the quick brown fox
String after your substring: over the lazy dog
Enter a position between 0 and 43: 18
The character at position 18 is x

Enter a replacement string: Your new string is: the quick brown fox over the lazy dog <------ isn't taking user input

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...

This is the question:

Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

printStrings("abc", 5);

will print the following output: abcabcabcabcabc

This is what I attempted:

public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}

View Replies View Related

Failed Conversion Error In Print Line

Jan 10, 2015

I don't get an in code error but when I run this one it crashes at the print line "failed conversion error". I've used this style format in another program and format worked ok, what doesn't it like

public class Commission { 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Calculate Commission and payment due
double subtotal;
double granuity;
double paymentDue;
 
[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 Grades Using Only Methods - Cannot Find Symbol Error

Nov 25, 2014

The program is just a simple one to print grades using only methods. The problem is Im trying to use a returned value in another method but the compiler keeps telling me it cannot find the symbol "mark". the problem areas are marked in blue. I am basically trying to input a value into the keyboard and then use it in another method.

public static void main(String[] args) {
printTitle();
enterMark();
gradeCalculator(mark);
printGrade();

[Code] .....

View Replies View Related

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

Binary Expression Tree Print - Null Pointer Error

Aug 3, 2014

I am trying to build an expression tree program . I try to print it then I have java.nullpointer exception . Is it error in my code or what ?

I have 2 classes

one is BSTreeNode which is the nodes for the tree . It has

public class ExpressionTree {
public BSTreeNode root;
public BSTreeNode curr = root;
ExpressionTree() {
root = new BSTreeNode(null, null, null, null);
curr = root;

[Code] ....

My BSTreeNode

public class BSTreeNode {
BSTreeNode parent;
Object element; // Binary search tree element
BSTreeNode left; // Reference to the left child
BSTreeNode right; // Reference to the right child
// Constructor
BSTreeNode (Object elem)
[Code] ....

The error i am getting is

Exception in thread "main" java.lang.NullPointerException
at binaryexpressiontree.ExpressionTree.insert(ExpressionTree.java:38)
at binaryexpressiontree.test.main(test.java:22)

Java Result: 1

After implementing exception handler what i am getting is

java.lang.NullPointerException
root 1 *
root 2 1 error error error//these error print because of my catch clause

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

How To Print String Only Five Times

Feb 11, 2015

I can't figure out how to print this string only five times. I tried to use the * operator, but that doesn't work with strings apparently, unles i'm not importing correctly.
 
import java.lang.String;
public class Looparray
{
public static void main(String args[] {
for (String myStr= "Hello there!";;) {
System.out.print (myStr);
System.out.print("
");
}
}
}

View Replies View Related

How To Print 5th Letter Of A String

Nov 11, 2014

So if I wanted to tell the user that the fifth letter they entered is "____" how would I do that.

I am prompting the user to enter a word and then displaying the length of the String. Now I want to display to the user what the fifth letter of the String they entered is.

import java.util.Scanner;
public class StringPractice
{
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String word;
int lenght;

[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

Input A Sentence And Print Words That Start With Vowel - Java Error On Execution

Jul 23, 2014

This is a program to input a sentence and print the words that start with a vowel.......There is no syntax error but on executing and typing the sentence and pressing enter,I get a error saying :

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at sentence.main(sentence.java:22)

[Code] ....

View Replies View Related

Why Print Char 0 (zero) plus String Does Not Work

Apr 3, 2015

I have a simple doubt. I was studying and create some code to check the result and I found out a strange situation.
 
Whats wrong with this code? Why it does not print anything?
 
char = 0; //integer value
System.out.println( c +" String ");
 
and why this next works very well?
 
char = 1; //integer value
System.out.println( c +" String ");
 
I know that char is stored as a positive integer and assign with 0 is different of assign with '0'.

View Replies View Related

Print String From Starting Character Given By User

Jul 14, 2014

Now I am doing dictionary app using files..

Here i want to print the String from Starting character which is given by user..Here i given below the image like my concept..here we put .(dot and enter R)eclipse will print the method name which is starting from R..

View Replies View Related

Read Through A String / Count How Many Vowels There Are And Print Out When Detected

Sep 15, 2014

So I need to write a program that reads through a String and counts how many vowels there are and prints them out as it finds them. This is what I have so far:

if (vowel == 'a' || vowel =='e' || vowel =='i' || vowel == 'o' || vowel == 'u'){
numberOfVowels++;
}

The problem is that I can't figure out how to print out the character once it detects it as a vowel.

View Replies View Related

Writing To Text File - Print String On One Line

Oct 19, 2014

I am writing to a text file via user input and it is saving all the user input to the file but it is just printing one word per line. I would like it to print the string on one line and print the next string on the next line upon them hitting enter.

public void textFile() {
Scanner reader = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = reader.next();
File f = new File(fileName);
PrintWriter p = null;

[Code] ....

View Replies View Related

Store String Input In Array And Print In Reverse Order

Apr 5, 2014

I have a college question ask me to write a class StringRevert which does the following:

-prompting user for an interger n
-creating an array of n string
-repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array)
-print the string from array in reverse order, from last enter to first enter.
-assume user always supplie correct input

This is what I've done so far but how to get it working.

import java.util.Scanner;
public class StringRevert {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter value of n: ");
int n1 = in.nextInt();
String[] n = new String[n1];

[Code] ....

View Replies View Related

Search For A String In Large Text File Of 1 GB Size And Print Line When Match Found

Feb 18, 2014

I have a large text file of 1 GB size. I need to print the line when a matching word is found in a particular line. Below is the code I am using. But if there are many lines that has the matching word, it's taking lot of time. Any solution to print the lines much faster.

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("xyz")) {
System.out.println(line);
}
}

View Replies View Related

Error String Index Out Of Range?

Jan 25, 2015

public class op{
  String word = "Hello"; //my variable
  public void reverseword() //My function {
  for(int i =word.length();i>=0 ;i--) {
System.out.println(word.charAt(i));

[code]....

when i call function in main i have this error:

run:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at javacourse.Car.opname(Car.java:35)
at javacourse.JavaCourse.main(JavaCourse.java:24)
Java Result: 1

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

Splitting String In Java Error

Jul 21, 2014

I am currently trying to split the string "EAM est" between the part. I have gotten the code to work if the was a -. But I can't see why the error is occuring

I have tried

String test = "EAM-testing";
String[] parts = test.split("-");
System.out.println("parts[0] = " + parts[0]);
System.out.println("parts[1] = " + parts[1]);
String test1 = "EAM esting";
String[] parts1= test1.split("");

[Code] .....

The error occurs at line: String[] parts1= test1.split("");

View Replies View Related







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