JAVA Numberline errors - java

I am trying to make the following number line is Java
2,3,5,7,11,13,17 (Prime Numbers)
I tried this code
for(int i =0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < 1; j++) {
if(i % j == 0) {
break;
} else {
System.out.print(i + ",");
}
}
}
But it doesn't work
Anyone help please?

Your code is quite poor, but the minimal amount of changes needed to make it work yields this code:
outerLoop:
for(int i = 0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < i; j++) {
if(i % j == 0) {
continue outerLoop;
}
}
System.out.print(i + ",");
}
But a further improvement would be to start the first loop at 2 right away:
outerLoop:
for(int i = 2; i <= 100; i++) {
for(int j = 2; j < i; j++) {
// and so on...

EDITED
There are a lot of errors in that code, first of all that second loop is a infinite loop and second one that the System.out.println line should not be in second loop it should be at end of first loop! If you place it in second it will print numbers hundreds of time.
This is the correct code :
for(int i = 2; i <= 100; i++)//begin loop from 2 instead of 0
{
boolean flag = true;
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = false;
break;
}
}
if(flag)System.out.print(i + ",");
}
You need to set a flag to check if a factor was found outside the loop.

Related

Asking about the while loop inside nested for loop

Can anyone explain how it actually works? ive been carefully checking every single step with my note and all written down, and it does work fine, but why is the spot 4 all the time?when i init it with 0, it doesnt make sense to me...
here is the output
Random rand = new Random();
int[][] map = new int[row][column];
int spot = 0;
int i=0;
int j=0;
/*ignore this part
for(i = 0; i<map.length; i++){
for(j = 0; j<map[i].length; j++) {
map[i][j] = rand.nextInt(2);
}
System.out.println();
}*/
System.out.println();
for(i =0; i<map.length; i++){
for(j =0; j<map[i].length; j++){
if(map[i][j] == 1){
while(spot<4){
map[i][j] = 5;
spot++;
}
}
System.out.print(map[i][j]+" ");
}
System.out.print(spot);
System.out.println();
}
System.out.println();
}
This line:
System.out.print(spot);
Your array has only 4 members, but in printing the value of "spot" at the end of each line, you're printing 4 at the end of each line.
Edit: responding to your comment about increasing 'spot' only when a 1 is found:
if(map[i][j] == 1){
while(spot<4){ // <<<< you increase spot until it equals 4
map[i][j] = 5;
spot++;
}
}
If your intent is to count the number of occurrences of '1', you'll want to remove that nested while:
if(map[i][j] == 1){
map[i][j] = 5;
spot++;
}
I'm not sure what you are trying to do with spot, but this is you problem. The first time if(map[i][j] == 1) is true you are increasing spot to 4, so while(spot<4) won't be executed again. If you remove it it will work.
if(map[i][j] == 1){
map[i][j] = 5;
}
Edit
Base on the comments, if you want to replace only 4 1 you can extract the replacing to a method and return as soon as you swapped 4 1
public void replaceOnes(int[][] map) {
int spot = 0;
for(int i = 0 ; i < map.length ; i++){
for(int j = 0 ; j < map[i].length ; j++){
if(map[i][j] == 1){
map[i][j] = 5;
spot++;
if (spot == 4) {
return;
}
}
}
}
}
replaceOnes(map);
for(i = 0; i<map.length; i++){
for(j = 0; j < map[i].length; j++){
System.out.print(map[i][j]+" ");
}
System.out.println();
}

Print pyramid of * in Java

I'm wondering if you could help me out. I'm trying to write a nested for loop in java that displays a number pyramid triangle that looks like
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
This is what I have so far:
class Triagle {
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
printTriagle(12);//I want to set the triangle to be height of 12
}
}
My result is not equal to the expected output:
___________*#
__________*_*#
_________*_*_*#
________*_*_*_*#
_______*_*_*_*_*#
______*_*_*_*_*_*#
_____*_*_*_*_*_*_*#
____*_*_*_*_*_*_*_*#
___*_*_*_*_*_*_*_*_*#
__*_*_*_*_*_*_*_*_*_*#
_*_*_*_*_*_*_*_*_*_*_*#
*_*_*_*_*_*_*_*_*_*_*_*#
I have updated your code and added comments so that you can understand. Refer to the code below:
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print("_");
}
String s = "_";
if (i + 1 >= n) // check if it is the last line
s = "*"; // change to print * instead of _
for (int j = 0; j <= i; j++) {
// printing stars
if (j == i)
System.out.print("*#"); // check if the last print of the line
else if (j == 0)
System.out.print("*" + s); // check if the first print of the line
else
System.out.print(s + s);
}
System.out.println();
}
}
Result:
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
Try this
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
if(i == (n-1)){
System.out.print("**");
}
else{
System.out.print((j == 0 || j == i) ? "* " : " ");
}
}
System.out.println();
}
}
Your issue is here:
for (int j=0; j<=i; j++){
// printing stars
System.out.print("* ");
}
Here, it prints a star for each number between 0 and i, but it only should print a star if it is exactly 0 or i.
Try something like this:
for (int j=0; j<=i; j++){
if ( i == n ) {
System.out.print("* ");
} else {
System.out.print(j == 0 || j == i ? "* " : " ");
}
}
EDIT: You may still have to adapt your code to get the bottom line printed correctly, in case that line has to be all stars
This is what you need to do:
public static void printTriagle(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2*n; j++) {
if(i == n-1) {
System.out.print((j != 2*n-1) ? "*" : "#");
}
else {
if(i+j == n-1) {
if(i == 0) {
System.out.print("*#");
break;
}
else {
System.out.print("*");
}
}
else if(j-i == n-1) {
System.out.print("*#"); break;
}
else {
System.out.print("_");
}
}
}
System.out.println();
}

Summing cells from table

I seem to not be able to solve this java.lang.ArrayIndexOutOfBoundsException: 5
I understand the error, but the table is 5x5 and I think I have everything right for printing it.
public static int tulosta_matriisi(int[][] matriisi) {
int i=0, j=0;
for(i = 0; i <= 4; i++) {
for(j = 0; j <= 4; j++) {
if(j == 4 && i <= 4)
System.out.println(matriisi[i][j]);
else if(i <= 4 && j <= 4)
System.out.print(matriisi[i][j] +"\t");
}
}
return matriisi[i][j];
}
To avoid all this problem you have to use :
for(i = 0; i < matriisi.length; i++) {
for(j = 0; j < matriisi[i].length; j++) {
...
When you get out your loop, the i and j will be incremented so you should not return matriisi[i][j] this make this error java.lang.ArrayIndexOutOfBoundsException: 5, so instead you should to return matriisi[i-1][j-1] so in the end your program should look like this :
public static int tulosta_matriisi(int[][] matriisi) {
int i = 0, j = 0;
for (i = 0; i < matriisi.length; i++) {
for (j = 0; j < matriisi[i].length; j++) {
if (j == matriisi[i].length - 1 && i <= matriisi.length) {
System.out.println(matriisi[i][j]);
} else if (i <= matriisi.length && j <= matriisi[i].length) {
System.out.print(matriisi[i][j] + "\t");
}
}
}
return matriisi[i - 1][j - 1];
}
Good luck

Asterisk in Nested loops, Java

I am trying to make my code print out the Asterisk in the image, you see below. The Asterisk are align to the right and they have blank spaces under them. I can't figure out, how to make it go to the right. Here is my code:
public class Assn4 {
public static void main(String[] args) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int x = 0; x <= 1; x++) {
System.out.println(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
Matrix problems are really helpful to understand loops..
Understanding of your problem:
1) First, printing star at the end- That means your first loop should be in decreasing order
for(int i =7;i>=0; i+=i-2)
2) Printing star in increasing order- That means your second loop should be in increasing order
for(int j =0;j<=7; j++)
Complete code:
for(int i =7;i>=0; i=i-2){ // i=i-2 because *s are getting incremented by 2
for(int j =0;j<=7; j++){
if(j>=i){ // if j >= i then print * else space(" ")
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();// a new line just after printing *s
}
Starting loops with 1 can sometimes help you visualize better.
int stopAt = 7;
for (int i = 1; i <= stopAt ; i += 2) {
for (int j = 1; j <= stopAt; j++) {
System.out.print(j <= stopAt - i ? " " : "*");
}
System.out.println();
}
Notice, how each row prints an odd number of *s ending at the line with 7. So, you start with i at 1 and go through 3 1+2, 5 3+2, and then stopAt 7 5+2.
The nested for loop has to print 7 characters always to make sure *s appear right aligned. So, the loop runs from 1 to 7.
Here the complete code:
for(int i = 0; i < 8; i++){
if( i%2 != 0){
for(int x = 0; x < i; x++){
System.out.print("*");
}
}else{
System.out.println();
}
}

How to print this using loops only

I just want to print this in simple java program using loops only.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Break the problem in small steps.
Loops for printing correct rectangle without patterns
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
System.out.print("-");
}
System.out.println();
}
This would give following output.
---------
---------
---------
---------
---------
Create pattern using if condition.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if ((j - i) % 2 == 0) { // Condition for alternate stars
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
Restrict pattern inside the triangular shape.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (i + j > 3 && j - i < 5) { // Equations of straight lines in triangle
if ((j - i) % 2 == 0) { // Condition for alternate stars
System.out.print("*");
} else {
System.out.print("-");
}
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Refactor for condensed conditions
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (i + j > 3 && j - i < 5 && (j - i) % 2 == 0) {
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Hope this helps.
Good luck.
try this :D
public static void main(String[] argu) {
int index=8;
for(int i=0;i<5;i++) {
for(int j=index;j>0;j--) {
System.out.print("-");
}
index--;
if(i==4) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
}
public static void main(String[] args){
for(int i=5;i>=1;i--){
int otherSide = 10-i;
int printStar = -1;
for(int j=1;j<=9;j++){
if(j>=i && j<=otherSide){
printStar*=-1;
if(printStar==1) System.out.print("*");
else System.out.print("-");
}
else
System.out.print("-");
}
System.out.println("");
}
}
Just find the start index of the * and the part where it is last printed. Print stars alternatingly inside the range.

Categories

Resources