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);
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
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); } }
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;
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.
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 ?
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;
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:
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].....
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();
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."
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
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(" "); } } }
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;
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):
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: ");
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)
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..
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:
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;
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];
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); } }
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
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