Can we use '&&' logic operator inside of the for? - java

Let say we want to see that shape in the output :
1
22
333
4444
666666
7777777
88888888
Code:
public class Main {
public static void main(String[] args) {
for((int i=1;i<=4;) && (int i=6; i<=8;) i++){
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
}
First question is I don't know how to remove 55555 line to my program's output
Can somebody help me make the correct corrections with using continue;
Second question is instead of writing continue statement, can we write like this:
public class Main {
public static void main(String[] args) {
**for((int i=1;i<=4;) && (int i=6; i<=8;) i++){**
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
}
I'm stuck :(

There's a simpler way to do what you are trying to do.
public static void main(String[] args) {
for(int i = 1; i < 9; i++){
if(i != 5) {
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
}
Alternatively, just write two for loops, the first one running from 1 to 4, and the second one running from 6 to 8.
public static void main(String[] args) {
for(int i = 1; i < 5; i++){
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
for(int i = 6; i < 9; i++){
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}

Short answer, no - you can't nest loops with a boolean && operator. However, you can use lambdas to implement this
IntStream.range(1, 8) // 1, 2, 3, 4, 5, 6, 7, 8
.filter(i -> i != 5) // remove 5
.forEachOrdered(i -> System.out.println(IntStream.range(0, i)
.mapToObj(j -> String.valueOf(i)) // repeat i i times
.collect(Collectors.joining())));

Related

Slanting Rhombus pattern using nested loops. Java

**I am sharing the code for a third person perspective to where the problem may be lying.
I am trying to design a slanting rhombus using *. My output is a not returning slanting as the spaces before the * in each line might not be working.
**
The Desired output can be seen at the back. My output is also shown here
public class file{
public static void rhombus(int n){
for (int i=1; i<=n; i++){
//spaces
for(int j=1; j<=(n-1); j++){
System.out.print(" ");
}
//stars
for(int j=1; j<=n; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void main (String args[]){
rhombus(5);
}
}
The problem with the code is that in the first for loop, where spaces are being printed, the loop variable 'j' is only being compared to (n-1) instead of (n-i). This means that the same number of spaces are being printed on every row, resulting in a diamond shape instead of a slanting rhombus. To fix this, change the comparison in the first for loop to (n-i) so that the number of spaces printed decreases as the row number increases.
You can change it this way
public class file{
public static void rhombus(int n){
for (int i=1; i<=n; i++){
//spaces
for(int j=1; j<=(n-i); j++){
System.out.print(" ");
}
//stars
for(int j=1; j<=n; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void main (String args[]){
rhombus(5);
}
}

How to print number and "*" combination in java

My program is as below:
package simplemirror;
public class simple {
public static void main (String arg[]){
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print(i+"*");
}
System.out.println("");
}
}
}
Above program output is as below:
1*
2*2*
3*3*3*
I need output as below:
1
2*2
3*3*3
2*2
1
this code will work try this.............:)
public class simple {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
n=3;
for(int i=1;i<2*n;i++){
if(i<=n){
for(int j=1;j<i;j++){
System.out.print(i+"*");
}
System.out.println(i);
}
else{
for(int j=i+1;j<2*n;j++){
System.out.print(2*n-i+"*");
}
System.out.println(2*n-i);
}
}
}
}
try
public static void main(String[] args) {
int limit=5;
int rows=limit+(limit-1);
int center=(int) Math.ceil(((double)rows/2));
boolean centerReached=false;
for(int i=1;i<=limit&&i>0;){
for(int j=1;j<=i;j++){
System.out.print(i);
if(i==j)
continue;
System.out.print("*");
}
if(center==i)
centerReached=true;
if(centerReached)
i--;
else
i++;
System.out.println();
}
}
ouptut
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
EDIT:
It's been a while, and I have learned a few things.
You can use streams!
private static void print(int num) {
IntStream.concat(
IntStream.iterate(1, (i) -> ++i).limit(num),
IntStream.iterate(num - 1, (i) -> --i).limit(num).limit(num - 1))
.boxed()
.map(i -> Stream.generate(i::toString).limit(i).collect(Collectors.joining("*")))
.forEach(System.out::println);
}
Please don't ask such question here. Ans to your question may be looks like follows but once again don't ask such questions here.
public class simple {
public static void main (String arg[])
{
for( int i = 1; i <= 3; i++ ){
for( int j = 0; j < i; j++ ){
if(j== i-1)
{
System.out.print(i);
}
else
{
System.out.print(i+"*");
}
}
System.out.println("");
}
}
}

printing an array in multiple lines

I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
Write a program to assign the integer values 1 through 25 to a 25
element integer array. Then, print the array as five separate lines
each containing five elements separated by commas. The last element
on each line should be followed by a newline instead of a comma. The
output of your program should appear exactly as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
Hints:
One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is
0, it is the 5th number.
You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same
line.
I have a little bit of code but I don't know where to go from here:
public class Program4
{
public static int[] array;
public static void main(String[] args);
{
int[] numbers = new int [25]
for(int i=0; i<25; i++)
array[i] = i + 1;}
public static void printArray()
{
for(int i=1; i<=25; i++);
{
System.out.print(array[i - 1]);
if (i % 5 == 0)
System.out.printIn();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this,
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;
public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}
public static void printArray()
{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}
public class Foo {
public static int[] nums;
public static void main(String[] args) {
nums = new int[25];
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
printArray(nums);
}
public static void printArray(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.print(String.valueOf(myArray[i]);
if (i % 5 == 0) {
System.out.println();
} else if (i % 5 != 4){
System.out.println(", ");
}
}
}
public class Test {
public static void main(String[] args) {
int[] array = new int [25];
for(int i=0; i<25; i++) {
array[i] = i + 1;
}
for (int i=1; i<=25; i++) {
System.out.print(array[i - 1]);
if (i % 5 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
And try to learn Java syntax first of all.
Here is an enhanced version of your code.
public class Program4
{
public static int[] array = new int[25];//instantiate the array to its default values
public static void main(String[] args)
{
//calling the methods from main
addToArray();
printArray();
}
//add the numbers to the array
public static void addToArray(){
for(int i=0; i<25; i++)
array[i] = i + 1;
}
//print the numbers from the array
public static void printArray()
{
for(int i = 1; i <= 25; i++){
if(i % 5 == 0){
System.out.print(i);
System.out.println();
}
else{
System.out.print(i + ",");
}
}
}
}

Print a pattern sequence of numbers in Java

I need to write a method called printOnLines() that takes two arguments an integer n and an integer inLine and prints the n integer, every inLine on a line.
for n = 10, inLine = 3:
1, 2, 3
4, 5, 6
7, 8, 9
10
My call is (10,3). Here's my code:
public static void printOnLines(int n, int s) {
for(int i=1; i<=s; i++) {
for(int j=1; j<=n-1; j++) {
System.out.print(j + ", ");
}
System.out.println();
}
}
}
I believe that there are two mistakes:
I need to remove the last comma appearing in the output.
I need to place 3 of the numbers of each line until I reach the
number 10.
Use this:
public static void printOnLines(int n, int s){
StringBuilder builder = new StringBuilder();
for(int i = 1; i <= n; i++){
builder.append(i);
if(i % s == 0)builder.append("\n");
else builder.append(", ");
}
System.out.println(builder.toString().substring(0,builder.toString().length() - 2));
}
I assume you've already had to submit your homework, so here's how I'd do it:
class printonlines {
public static void main(String[] args) {
printOnLines(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
}
public static void printOnLines(int n, int s) {
for(int i=1; i<=n; i++) { // do this loop n times
if ( (i != 1) && (i-1)%s == 0 ) { // if i is exactly divisible by s, but not the first time
System.out.println(); // print a new line
}
System.out.print(i);
if (i != n) { // don't print the comma on the last one
System.out.print(", ");
}
}
}
}
Oh, you don't want to use any if statements. All you really learn from doing "tricks" like what you are asked to do is a single trick, not how to write easily comprehensible (even by yourself when you have to debug later) code.
Anyway, here's my less comprehensible but more tricky code that doesn't use if as per the suggestions from the answer from #DavidWallace. I'm sure someone can do it neater but this is the best I could do in 15 minutes...
class printonlineswithnoifs {
public static void main(String[] args) {
printOnLines(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
}
// i 012012012
// j 000333666
// i+j 012345678
public static void printOnLines(int n, int s) {
int i = 0;
int j = 1;
for (; j <= n; j+=s) {
for (; i < (s-1) && (j+i)<n; i++) {
System.out.print((i+j) + ", ");
}
System.out.println(i+j);
i=0;
}
}
}
I'm not going to post a complete solution, since this is clearly homework. But here is what I would do, if I weren't allowed to use if statements or ternary operators.
Make the index of the outer loop start at 0 and increase by s on every iteration, instead of by 1 (So it goes 0, 3, 6, 9 in your example).
Print the sum of the two loop indexes on each iteration.
Print the last number on each line outside of the inner loop, without a comma.
Edit
OK, on #localhost's request, here is my solution.
public static void printOnLines(int n, int s) {
for (int i = 0; i < n; i += s) {
int j;
for (j = 1; j < s && i + j < n; j++) {
System.out.print(i + j + ", ");
}
System.out.println(i + j);
}
}
Here is the code sample you wanted..
public static void printOnLines(int n, int s) {
for (int i = 1; i < n; i++) {
System.out.print(i + ",\t");
if (i % 3 == 0) System.out.println();
}
System.out.println(n);
}

Override previous Console Output

is it possible to override the last System.out.println output, so i can for example visualize changes in a array or create a progessbar?
For example if i have this class:
class Main{
public static void main(String[] args){
for(int i = 0; i < 10; i++){
for(int j = 0; j < i; j++){
System.out.print("#");
}
System.out.println("");
}
}
}
What do i have to do to create this simple progressbar which is shown in a single line and not in 10 seperate lines?
This works on my particular console (Windows) but it's not terribly portable...
public class Test {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 100; i++) {
System.out.print("#");
if (i % 20 == 0) {
System.out.print("\r \r");
}
System.out.flush();
Thread.sleep(100);
}
}
}
There's also the Console class, but that doesn't actually buy you very much as far as I can see...

Categories