How To Use Nested Loops To Print Out A Letter Pattern Wraps From Z
Jan 6, 2015
//method class
import static java.lang.System.*;
public class TriangleFive
{
private char letter;
private int amount;
public TriangleFive()
{
[Code]...
View Replies
ADVERTISEMENT
Jan 24, 2015
Write a program that will display the following pattern.
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
I need to write a program that uses nested for loops to display that patter but I have no clue on how to do it....
View Replies
View Related
Feb 12, 2015
Im trying to create a checkerboard pattern with 2 nested for loops . I need to output asterisk characters. Im supposed o use an n int so I dont know if im limited to that 1 int. Im only getting 1 line of asterisk.
* * * * *
* * * * *
* * * * *
* * * * *
package checkerboard;
public class Checkerboard {
public static void main(String[] args) {
for (int row = 1; row < 6; row++){
System.out.print("* ");
for (int col = 6; col < col; col++) {
[code]...
View Replies
View Related
Nov 15, 2014
We were given a class lab that asks us to write a program that create a multidimensional array ( 5 x 5 ), populates the array using nested loops with letter from A until Y, and displays the array to the screen. and the result should look like this:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
How to write this program.. I have tried all my best but the results are not coming like this..
View Replies
View Related
Mar 7, 2014
Write a method to print each of the following patterns using nested loops in a class called PrintPatterns. The signature of the methods are:
public static void printPatternX(int size) //'X' from 'A' to....., size is a positive integer.
#
##
###
####
#####
######
#######
########
View Replies
View Related
Oct 6, 2014
I'm writing a program to print an ASCII image using nested for loops. The output is a rocket ship that looks like this.
/**
//**
///**
////**\
/////**\
+=*=*=*=*=*=*+
|../..../..|
|.//..//.|
|//////|
|//////|
|.//..//.|
|../..../..|
+=*=*=*=*=*=*+
|//////|
|.//..//.|
|../..../..|
|../..../..|
|.//..//.|
|//////|
+=*=*=*=*=*=*+
/**
//**
///**
////**\
/////**\
I'm having problems with the mid section of the rocket, specifically the bottom part of the mid section:
|../..../..|
|.//..//.|
|//////|
I'm having difficulty writing the for loop to correctly print the dots. You're supposed to incorporate a constant which allows you to adjust the size of the overall figure. The loop doesn't work when I adjust the size, only when the value of HEIGHT is 3. The loop, however, for some reason works with the top part of the mid section.
This is the for loop for the top section.
for (int dots = 1; dots <= -1 * line + 1 * HEIGHT; dots++) {
System.out.print(".");
}
This is the for loop for the bottom section.
for (int dots = -1 * line + 1 * HEIGHT; dots <= 1; dots++) {
System.out.print(".");
}
Usually reversing the iteration of the loop just requires flipping the conditions, right? But it didn't work this time for some reason. Why this doesn't work? I can post the code to my entire program for compiling.
View Replies
View Related
Feb 13, 2014
I don't know how to write a program based on for loop like pattern printing.etc
View Replies
View Related
Jan 19, 2015
I built a java code to print the following pattern using nested loop:
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
And here is my code
public static void main(String[] args) {
for(int i=1; i<=8;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(" ");
[Code] .....
which gives the partially true output:
123456781
123456721
123456321
123454321
123454321
123654321
127654321
187654321
I couldn't figure out how to place the spaces.I have made some changes on the code, but it didn't work.
View Replies
View Related
May 15, 2014
how a nested for loop increments the outer loop and then the inner loop and then when it runs the inside code.I've ran the program a few times but I'm just not understanding it, need little narrative to it?
for(int i=0; i < 5; i++){
System.out.println("i - " + i);
for(int j=0; j < i; j++){
System.out.println("j - " + j);
count++;
}
}
View Replies
View Related
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
Feb 9, 2015
i want to use nested loops to print patterns but i cant seem to make a pattern that will look like V and K this should be the outcome.
* *
* *
* *
* *
*
@ @
@ @
@@
@ @
@ @
View Replies
View Related
Oct 10, 2013
Is there any advantage to using Java's operations defined in sets or collections over inspecting in nested for-loops?
For example, suppose I want to keep items from one array that do not appear in another, I could do:
for( PrimitiveType prim_1 : someArray ){
for( PrimitiveType prim_2 : otherArray ){
if( prim_1 != prim_2){
///put in some return list
}
}
}
Or else I could do:
ArrayList<NotPrimitive> excludedElements =
new ArrayList(Arrays.asList(primitiveArrayToExclude));
for( PrimitiveType elem : someArray){
if( ! excludedElements.contains( elem ) {
//put in some return list
}
}
I personally see no improvement in legibility nor any reduction of error potential.....
View Replies
View Related
Sep 21, 2014
I am having a problem understanding line spacing. I need to write a program that has nested FOR LOOPS to produce the following output:
1
2
3
4
5
I can get a for loop that counts down. The problem I have is using for loop for line spacing. How a for loop for a line works.
View Replies
View Related
Apr 21, 2014
I need to write a class,that will give me output like this:
*
***
****
***
*
I have to use for loop,i know that i have to use nested for loops, for rows and columns. I just cant figure it out the thing with spaces,and how to turn it to count back.
View Replies
View Related
Feb 4, 2015
Java Code:
// Demonstrate a two-dimensional array
class TwoDArray {
public static void main(String args[]) {
int twoD[] [] = new int[4] [5];
int i, j, k = 0;
[Code] .....
Output:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
(1) I don't understand how repeating the loop creates the structure of the output. Taking the second loop away and putting the "System.out.print(twoD[i] [j] + " " );" under k++ creates the output to print a number on each line. How do I write the code not having the second loop, assigning k to each value that is moved through the grid then printing it out but having the output the same?
Java Code:
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
System.out.print(twoD[i] [j] + " " );
System.out.println();
} mh_sh_highlight_all('java');
(2) I don't understand why you can't put a new line sandwiched between the first for loop
Java Code:
for(i=0; i<4; i++)
System.out.println();
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++; mh_sh_highlight_all('java');
It compiles, but I get the message:
" Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at TwoDArray.main(TwoDArray.java:11) "
View Replies
View Related
Sep 29, 2014
So I cant figure out why my output for my for loop isn't working properly. So the output for the square comes out right but the for loop isn't working properly for the H. I have tried to figure it out and it should go to the next line but its not.
import java.util.Scanner;
public class Random
{
public static void main(String [ ] args)
{
[Code] ....
Output
Enter a positive odd integer: 5
Square:
*****
* *
* *
* *
*****
H:
* *
View Replies
View Related
Mar 23, 2015
I have to make a pattern that is of a diamond shape such as this:
Java Code:
1
1 2 3
1 2 3 4 5
1 2 3
1
mh_sh_highlight_all('java');
I wrote this (for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):
Java Code:
public class NewFile{
public static void main(String []args){
int k = 0;
for (int i=1 ; i<=5 ; i++) {
{ for (int h=2 ; h >= i ; h--)
[Code] ....
My output is the following:
Java Code:
1
123
12345
1234567
123456789
mh_sh_highlight_all('java');
I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.
View Replies
View Related
Oct 11, 2014
Question: 1. Declare and implement a class named Substrings. The class will contain the following data members and methods:
Data members:
string S
Methods:
setS() take a string as a parameter and store it in S
printSub1(), printSub2(), printSub3(), printSub4() print all substrings of S. If S = "abcd",
printSub1() will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d",
printSub2() will print "abcd", "bcd", "cd", "d", "abc", "bc", "c", "ab", "b", "a",
printSub3() will print "a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd", "abcd", and
printSub4() will print "abcd", "abc", "bcd", "ab", "bc", "cd", "a", "b", "c", "d".
Alright so after doing some googling and watching tutorials I managed to put together the first two but I still don't exactly understand how nested for loops work, however just a single loop I do understand.
Secondly, the 3rd and 4th prints are completely destroying my brain using for loops, which we are supposed to use. Check out my code below, also keep in mind the 3rd is giving errors and the 4th i had to revert back to the same as the first print temporarily because my code because so messed up. How to understand nested for loops better.
[code=Java]
import java.util.Scanner;
class subString {
String S;
void setSubstring(String SS) {
S = SS;
[Code] .....
Compiling: error for myS.printSub3 and myS.printSub4 is the same as 1 because i had to revert it after ruining the code.
View Replies
View Related
Dec 2, 2014
So basically i have to read from a text file and get some information back from that and print to screen. Im quite confused and im not sure what loop i should use first to scan the text and receive certain information back?
View Replies
View Related
Aug 4, 2014
Nested for-loops always throw me in a loop.I found a snippet that uses 2 for-loops to check if there is a duplicate element in the array:
/*
* brute force way of checking if array contains duplicates in Java comparing each elements to all other elements of array complexity on order of O(n^2) not advised in production
*/
public static boolean bruteforce(String[] input) {
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input.length; j++) {
if (input[i].equals(input[j]) && i != j) {
return true;
}
}
}
return false;
}
Let us say we have:
String[] input = new String[] {"one","two","three","one"}
View Replies
View Related
Feb 23, 2014
When input validation for the first months rainfall is non-negative, this results in correct average rainfall.
When input validation is used for the first months rainfall I'm prompted to input a positive number, which is 2.
When asked to input rainfall, in inches, for each month, I begin with input -3, I am again prompted to re-enter a positive, I enter 3. What happens is, whichever positive integer I input after I had entered a negative for the first months rainfall, the average would be off by the positive number inputted.
package averagerainfall;
import java.util.Scanner;
public class AverageRainfall
{
public static void main(String[] args)
{
int maxYears;
int totalMonths;
[Code] ....
View Replies
View Related
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
Oct 1, 2014
I am working on my java program it prints out a table of angles and the sin, cos, and tan. For extra credit he wanted us to use nest loops to create a space after every five lines. I have come real close to getting this to work but have a problem with the very end of the table. The table needs to stop at 180...
public static void printTable(double angle,double sin,double cos,double tan) { // Method to create a table for the values
for (double j = 0; j <= 180;) {
for (double i = 1; i <= 5; i++) {//loop to print five lines of code
angle = Math.toRadians(j);
[Code] .....
Here is the end of the output of the table:
175.00.0872-0.9962-0.0875
176.00.0698-0.9976-0.0699
177.00.0523-0.9986-0.0524
178.00.0349-0.9994-0.0349
179.00.0175-0.9998-0.0175
180.00.0-1.00.0
181.0-0.0175-0.99980.0175
182.0-0.0349-0.99940.0349
183.0-0.0523-0.99860.0524
184.0-0.0698-0.99760.0699
View Replies
View Related
Nov 1, 2014
Alright so I wrote a switch statement that decides what range to print based on the letter grade input.
import java.util.Scanner;
public class SwitchPractice
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
[code]...
It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.
View Replies
View Related
Oct 22, 2014
I have a problem with functions connected to strings.
I have a random String, e.g. "Hello World!" and I have to change every capital Letter into a small letter and vise versa. This alone would be fairly simple, but I have to do it with two other strings.
lowercase= "abcde...z" and
uppercase="ABCDE...Z". Every small letter stands at the very same position as the capital letter, but in the other string.
There should be a letter for letter search in lowercase and uppercase for the letters of "Hello World".
How I could solve the task, however I need a way to search the first string, here "Hello World", according to position. A statement which does: "Give me the letter of position x". This found letter should be able to be stored in a variable or else be able to be used by another statement. And it should be compatible with a for lope.
View Replies
View Related
Apr 15, 2014
Example : I have code and name but my code must start with the first letter of the inputed name if the 2 input is match it will be inserted into database
code = "A"001
name ="Angela"
= success this will inserted into database
else
code ="B"002
name="Angela"
=failed this will not inserted into database
else
code="A"003
name="Andy"
=success still accepts the input cause they have diff code number
What I am thinking on this was compare the code the name? if == it will be inserted but how do i get the 1st letter of the input name?
View Replies
View Related