Please give me a hand to tell me why second for loop 'row' could not be found in the printSeating Method.
public void printSeating()
{
for (int row = FIRST_ROW_NUMBER; row <= firstClass.length; row++)
{
if (row < 10)
System.out.print(" ");
System.out.print(row);
System.out.println(":" + firstClass[row - FIRST_ROW_NUMBER]);
}
int firstEconomyRowNumber = FIRST_ROW_NUMBER + firstClassRowCount;
int lastEconomyRowNumber = firstEconomyRowNumber + economyRowCount - 1;
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++);
{
if (row < 10)
System.out.print(" ");
System.out.print(row);
System.out.println(":" + economy[row - firstEconomyRowNumber]);
}
}
When declaring the FOR loop, you put a semicolon after the loop, like this:
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++);{
//..insert code here
}
This declares a FOR loop without a body, so when you access the variable row after this statement, it cannot find the variable because it can only be used in the non-existent body of the FOR loop. To fix this, you would need to delete the semicolon, like so:
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++){
//...insert code here
}
This is your error.
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++);
get rid of that ; after the for loop and it should work.
Related
I want to print the following pattern:
*
*_*
*_*_*
*_*_*_*
*_*_*_*_*
But the issue here is I am unable to eliminate last _ symbol, this is the output that I am getting:
*
*_*_
*_*_*_
*_*_*_*_
*_*_*_*_*_
int row =5;
String star="*";
String undrscr="_";
String s;
for(int i=1;i<=row;i++)
{//System.out.print(undrscr);
for(int j=1;j<=i;j++)
{
if(i==1)
{
System.out.print(star);
}
if(i>1)
{
System.out.print(star+""+undrscr);
if(j==i)
{
System.out.print("");
}
}
}
System.out.println();
}
Change your inner for-loop to:
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= i; j++) {
if (i == 1) {
System.out.print(star);
}
if (i > 1) {
System.out.print(star);
if (i - 1 >= j) { // <-- change made in this if-block only
System.out.print(undrscr);
}
if (j == i) {
System.out.print("");
}
}
}
System.out.println();
}
Before printing underscore check whether i - 1 >= j because as per your requirement the number of underscores is always 1 less than the number of stars.
You can change the block if(i>1){...} to print undrscr only if j!=i like this :
if(i>1)
{
// print only star
System.out.print(star);
// print undrscr if j!=i
if(j!=i)
{
System.out.print(undrscr);
}
}
And you will get the pattern :
*
*_*
*_*_*
*_*_*_*
*_*_*_*_*
Try this..
int row = 5;
String star = "*";
String undrscr = "_";
String s;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(star);
if (i != j)
System.out.print(undrscr);
}
System.out.println();
}
Try this:
//i was the rowNum and the number of stars of that row
for (int rowNum = 0; rowNum<=rows; rowNum++) {
int numStars = 0;
while (numStars<rowNum) {
System.out.print(star+undrscr);
numStars++;
}
System.out.println(star);
}
Output:
*
*_*
*_*_*
*_*_*_*
*_*_*_*_*
*_*_*_*_*_*
EDIT: Better use this approach, you would get 5 rows instead of 6.
for (int rowNum = 1; rowNum<=rows; rowNum++) {
int numStars = 1;
while (numStars<rowNum) {
System.out.print(star+undrscr);
numStars++;
}
System.out.println(star);
}
In this stars are in increasing order of rows (i.e. 1 star in first row, followed by 2 stars in second and so on).
Step by step descriptive logic to print right triangle star with underscore pattern.
1)Input number of rows to print from user. we pass 5 Rows.
2)To iterate through rows run an outer loop from 0 to 5 with loop structure for(i=0; i<=5; i++).
3)To iterate through columns run an inner loop from 0 to i with loop structure for(j=0; j<=i; j++). Inside the inner loop print star.
4)In column loop we check if i==j then print only star otherwise print star and underscore.
5)After printing all columns of a row, move to next line i.e. print new line.
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
{
if(i==j)
{
System.out.print("*");
}
else
{
System.out.print("*");
System.out.print("_");
}
}
System.out.println();
}
I'm reading a book and before I go to next chapter, I want to solve every exercise from current one. I have a problem with creating this output (the number of rows must be between 11 and 20)
I almost have it, even when I think my code is pretty bad and I could get it in less lines.
public class piramide {
public static void main(String args[]){
int max, n;
max = 20;
n=1;
for (int min=11; min<=max; min++){
if (n>9) n-=10;
int x=n-1;
int x2=n-1;
int b=min-1;
for (int j=1; j<min; j++){
while (b<max-1) {
System.out.print(" ");
b++;
}
System.out.print(x);
x--;
if (x<0) x=9;
}
System.out.print("A"+n+"A");
for (int j=1; j<min; j++){
System.out.print(x2);
x2--;
if (x2<0) x2=9;
}
System.out.println();
n+=2;
}
}
}
This is my current code and this is the output:
0987654321A1A0987654321
21098765432A3A21098765432
432109876543A5A432109876543
6543210987654A7A6543210987654
87654321098765A9A87654321098765
098765432109876A1A098765432109876
2109876543210987A3A2109876543210987
43210987654321098A5A43210987654321098
654321098765432109A7A654321098765432109
8765432109876543210A9A8765432109876543210
The problem I'm having is that the left part of the pyramid should be reversed. For example in the first row it should start at 0 (from the A1A) and finish in 1 but it starts in 1 and finish in 0, any idea how can I turn it to the other side?
Thanks to all of you who helped me ^^.
Oh, and the caps A are just so I could find the number easier in the output.
Have you worked the problem out?
The code will be much easier to understand with a couple changes...
max, min, and especially the single letter variables like n should have names that help describe what they are. This may also help you think about the problem when you don't have to keep in mind what all those random letters mean.
n I will rename to rowIndex
max I will rename to totalRows
min I will rename to columnIndex
Starting with that we have
public static void main(String args[])
{
int totalRows = 20;
int rowIndex = 1;
int columnIndex = 1;
//we look ready to start at row 1, column 1!
}
Now, this section of your code:
for (int min=11; min<=max; min++){
if (n>9) n-=10;
int x=n-1;
int x2=n-1;
int b=min-1;
for (int j=1; j<min; j++){
while (b<max-1) {
System.out.print(" ");
b++;
}
You are setting min, or, the columnIndex, to start at 11, because that is the "middle" of the pyramid. Then you print out spaces to catch up to the columnIndex.
x = rowIndex - 1;
x2 = rowIndex - 1;
b = columnIndex - 1;
j and b are now like a second and third column index, which is catching up to the actual columnIndex
Look at this example of how your for loop works:
for (int j=1; j <min; j++) { // j = 1;
while (b<max-1) { // 10 < 19
System.out.print(" "); // print space
b++; // b = 11
// 11 < 19
// print space
// b = ...(*skip to the end*) 19
// j = 2
// b is still 19, doesn't print anything
// j = 3, etc.
}
System.out.print(x);
x--;
if (x<0) x=9;
}
In other words, j and b are unnecessary because we already have a columnIndex we can use. Let's do some more renaming of variables.
x I will rename to printValue
x2 will be unnecessary, we only need one printValue, However, I will be adding a totalColumns to the beginning of our main method.
So now our finished code will look like:
public static void main(String args[])
{
int totalRows = 20;
int totalColumns = (totalRows * 2) - 1; //added totalColumns, notice the number of columns increases by two with each row and we start with 1 column.
int rowIndex = 0;//easier for looping to start with zero
int columnIndex = 0;
int printValue = 0;
while (rowIndex < totalRows) // we will want to spin through every row
{
//notice there is no limit to the length of a variable name!
int numberOfValuesInRow = (rowIndex*2) + 1;
int numberOfSpacesToOffsetOnEachSide = (totalColumns - numberOfValuesInRow) / 2;
//Print Spaces before the numbers in this row
for (int i = 0; i < numberOfSpacesToOffsetOnEachSide; i++) //i is commonly used to stand for index in a single for loop
{
System.out.print(" ");
columnIndex++; //keep track of columnIndex so we know when we are at the middle of the columns
}
//Print numbers in this row
for (int i = 0; i < numberOfValuesInRow; i++)
{
if (columnIndex < (totalColumns/2) + 1) { //depending on columnIndex position, increase or decrease printValue
printValue++;
} else {
printValue--;
}
System.out.print(printValue%10); //Print printValue, the % will return the remainder of (printValue/10)
columnIndex++;
}
System.out.println(); //start next line
columnIndex = 0; //reset columnIndex for the next row
rowIndex++;
}
}
I'm trying to print square by using nested for loop. It is needed square to look like sequence numbers. Actually I didn't receive square. Please see attached file to understand my goal- red border.
public class Day22022014 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
for(int row=1; row<=n; row++){
System.out.printf("%d %n", row);
for(int col=row; col<2*n; col++){
System.out.printf("%d ", col);
}
}}}
You have a few errors there. The first printf is incorrectly outputting a number, you only need to make a new line, all numbers should be outputted inside the inner for.
Secondly, the condition col < 2 * n is wrong, you only want to go up to row + n - 1.
I suggest you go through this and try to understand, how do the indices row and col change in each iteration.
for(int row = 1; row <= n; row++){
for(int col = row; col <= (row + n - 1); col++){
System.out.printf("%d ", col);
}
System.out.println();
}
I am just curious as to how that is done. I am writing a small program to get a better understanding of two dimensional arrays. I want to know how I can go though each row and then each column separately using for loops.
Lets say I have a 2D array that is made out of different letters. I want to go through each row and each column and check if a certain letter is there. Then I want it to print how many occurrences of this letter happened in each row and then each column.
First index is row and second index is column.
Assuming that the something[][] is an something[] of something-rows (that is something[i] gives us a row, not a column - if it'S the way round, just change the examples):
public static void loopExample (String[][] someTwoDimArray) {
// Looping rows
for (var row = 0 ; row < someTwoDimArray.length ; row++) {
for (var col = 0 ; col < someTwoDimArray[0].length ; col++) {
System.out.println(someTwoDimArray[row][col]);
}
}
// looping columns
for (var col = 0 ; col < someTwoDimArray[0].length ; col++) {
for (var row = 0 ; row < someTwoDimArray.length ; row++) {
System.out.println(someTwoDimArray[row][col]);
}
}
}
I don't know if the first or second index is considered rows or columns, but this is a pretty standard nested loop for iterating over every element of a 2d array.
for(int column = 0; column < array.length(); ++column) {
for(int row = 0; row < array[column].length(); ++row) {
// do stuff to array[column][row]
}
}
Given your update, let's look for the letter 'N', in a 2d char array called myLetters.
int counter = 0;
for(int i = 0; i < myLetters.length(); ++i) {
for(int j = 0; j < myLetters[i].length(); ++j) {
if('N' == myLetters[i][j]) {
++counter;
}
}
}
System.out.println("N occurs " + counter + " times.");
if you have a 2D array if you want to access each cell you will have to use a nested for loop.
eg:
for(int i = 0; i < length1; i++)
for(int j = 0; j<length2; j++){
// do something
to format column first do array[i][j] = //do something
to format row first do array[j][i] = // do something
}
"I tried using a for loop however i dont have a good understanding of for loops and i was wondering how not just go through array in its entirety but small bits like rows and columns"
A for loop is a java control flow statement. It lets you initialize a variable (i and j) it gives you a condition (i
int i = 0
while (i < length1){
//do something
i++
}
if working with arrays for loops are almost always required.
The following segment of code is from a system that generates a 5 by 5 grid of JButtons. I need to iterate over an ArrayList of JButtons and pass the row and column of the JButton into the ButtonListener's constructor. The way the code is currently shown below works, but I was wondering if I could clean up the code at all or re-factor in any way. I seem to have a lot of instructions for trying to construct a grid.
int row = 1, col = 1;
for (JButton curButton : view.getButtons()) {
curButton.addActionListener(new ButtonListener(row, col));
row++;
if (row > 5) {
row = 1;
col++;
}
}
Is there any way I can improve the quality or simplify the above code segment?
int iterator = 0;
for (JButton curButton : view.getButtons()) {
curButton.addActionListener(new ButtonListener(iterator%5 + 1, iterator/5 + 1));
iterator++;
}
Note here that I'm using integer division, which always rounds down. iterator/5 + 1 will map {0,1,2,3,4,5,6,...} to {1,1,1,1,1,2,2...}
Can't you have:
col = row = 5;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
curButton.addActionListener(new ButtonListener(i, j));
}
}
Might have to make col and row 6 or make i and j start at a different number.