I want to make the program print for any integer that I input an asterisk in such way that every new line there is an increasing number of two more asterisks, always starting from one asterisk.
This code will print for any integer the same number of lines that I entered, with one asterisk in it, but how do I increase the number of the asterisks in each line?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter a number:");
int num = sc.nextInt();
int j=0;
int star=1;
int space= num;
System.out.println ("* ");
if (num>0) {
for (j=1; j<num; j=j+1) {
System.out.println ("* " );
}
for( j=0; j<star; j++) {
}
}
}
public static void main(String[] args) {
String line = "* "; // let's use a variable for the next line we want to print
Scanner sc = new Scanner(System.in);
System.out.println("enter a number:");
int num = sc.nextInt();
if (num > 0) {
System.out.println(line);
for (int j = 1; j < num; j++) { // j++ does the same as j = j + 1
line = "*" + line; // add a * at the start of the line
System.out.println(line);
}
}
}
System.out is a PrintStream. PrintStream has a second method, print that prints out what you put but without printing out the end of line character after it.
So, you probably want to use that instead.
Now, having said that, you'll still need to print out the end of line character when you've finished with the other characters. You can do that by calling the no argument version of println (which looks like System.out.println();)
Assuming you're looking for something like this:
INPUT: 4
*
**
***
****
Then you can do something like this:
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 0; i < num; i++) {
for (int j = 0; j < i; j++) {
System.out.Print("*");
}
System.out.Println();
}
Note that this prints out only one star at a time, and will be fairly slow. Fun to watch though, if toss a sleep statement in there.
You should use two for loops for that, one for lines and another for stars.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.Print("*");
}
System.out.Println();
}
Output:
=======
*
**
***
****
*****
Related
I am doing an exercise where I have to print 'x' (is an input) rows of numbers incrementing from 0 to 10.
If I input 3, the output should look like this
012
345
678
012
345
678
012
345
678
but instead, I get 3 rows of a 0 to 10 count.
I know it might be easy to code, but I am stuck in that!
I think I am not undestanding well the nested loops :(
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
}
}
}
You don't need two loops for this. All you need is to print a newline after every 3rd letter and an extra newline after every 3rd line. Your code can be like:
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int lines = 0;
int letters = 0;
while (lines < q) {
System.out.print(i);
if (letters && letters % q == 0) {
System.out.println();
lines++;
}
if (lines && lines % q == 0) {
System.out.println();
letters = 0;
continue;
}
letters++;
}
}
PS: I haven't tried this code myself. But concept would be the same.
The answer above should solve your problem so I will try to explain what your code does.
Let's start with code inside first for loop:
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
First we have a loop iterating through numbers from 0 to 10 and the output is:
012345678910
and a new line after that.
That means that output of your program will print above mentioned output q times.
012345678910
012345678910
012345678910
You can try with below code
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i < 9; i++) {
if(i%3 == 0)
System.out.println();
System.out.print(i);
}
System.out.println();
}
}
}
Print X quandrants of X rows and X columns each
Scanner in = new Scanner(System.in);
int q = in.nextInt();
// q quadrants
for (int iQuadrat = 0; iQuadrat < q; iQuadrat++) {
// count will keep track of the last number you print
int count = 0;
// q rows
for (int iRow = 0; iRow < q; iRow++) {
// q cols
for (int iCol = 0; iCol < q; iCol++) {
System.out.print(count);
// increment the count and take its modulo 10 so it stays between 0 and 9
count = (count+1)%10;
}
// line return at the end of the row
System.out.println();
}
// line return between quadrants
System.out.println();
}
For an input of 12, it will print 12 times this quadrant
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
I'm currently working on an assignment for school and the objective is to have the user input n amount of lines and then print them in reverse.
For example:
"Please enter number of lines: "
3
"Please enter the lines: "
Hi
Hey
Howdy
Desired Output:
Howdy
Hey
Hi
My output:
H
o
w
d
y
H
e
y
H
i
I'm not sure what's wrong and I'd really like some help, here is my code:
import java.util.Scanner;
public class ReverseOrder {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Please enter the number of lines: ");
int numberOfLines = kb.nextInt() + 1;
String inputLines[] = new String[numberOfLines];
System.out.println("Please enter the lines: ");
for (int i = 0; i < numberOfLines; i++) {
inputLines[i] = kb.nextLine();
}
System.out.println("Lines in reverse: ");
for (int i = numberOfLines - 1; i >= 0; i--) {
for (int j = 0; j <= inputLines[i].length() - 1; j++) {
System.out.println(inputLines[i].charAt(j));
}
}
kb.close();
}
You are printing each character with an end of line character by calling println() with your current two for loops. This is one step too many.
Since you already have the entire string, you can simply print the strings in reverse order like so using the println() function
for(int i = numberOfLines - 1 ; i>=0; i--){
System.out.println(inputLines[i]);
}
#Kody
I'm not sure what you have to do in your code but:
If you need just print each line in a reverse order, you can do this:
System.out.println("Lines in reverse: ");
for (int i = numberOfLines - 1; i >= 0; i--) {
System.out.println(inputLines[i]);
}
The method println will create a new line for you.
I want to get the desired outputs like here Tests 1-4 and still prompt the user to input the tests using the Scanner scan = new Scanner(System.in). My program says out of range. How should I fix this?
public static void main(String[] args){
String word="";
System.out.println("Enter a Word:");
Scanner scan = new Scanner(System.in);
word= scan.next();
for (int j=word.length(); j>=0; j--) {
System.out.println(word.substring(j-1, j));
}
}
new StringBuilder(scan.next()).reverse().toString();
Try this:
for (int j=word.length(); j >=1; j--)
{
System.out.println(word.substring(j-1, j));
}
Explanation: In your for loop j should only decrement till j>=1. When
j = 1 Because you do substring(j-1, j) = substring(0, 1)
In your case, when j becomes 0, substring(j-1, j) = substring(-1, 0)
Hence the exception, as string does not have -1 as an index.
The error is due to last iteration of loop when j=0 in this case you are doing word.substring(j-1, j) ie word.substring(-1, 0) Giving you that error.
Instead change the loop to j>=1
String word = "";
System.out.println("Enter a Word:");
Scanner scan = new Scanner(System.in);
word = scan.next();
System.out.println();
for (int j = word.length(); j >= 1; j--) {
System.out.print(word.substring(j - 1, j));
}
DEMO
I don't see the point of making a substring each time. Simple charAt(index) would do.
Scanner scanner = new Scanner(System.in);
String word = scanner.next();
for (int i = word.length() - 1; i >= 0; i--) {
System.out.print(word.charAt(i));
}
Basically I'm trying to make a program to accept an integer between 1 and 10 and also an alphabetic character. It then outputs an appropriate pattern based on this value as a maximum width
For example a user enters an integer of 5 and a letter X the program prints out:
x
xx
xxx
xxxx
xxxxx
I can't seem to get it to print out anything, below is what I've got so far.. any tips are extremely appreciated!
import java.util.*;
public class pattern {
public static void main(String[] args) {
int New1 = 1, Linecounter = 1;
Scanner sc = new Scanner(System.in);
int Number = sc.nextInt();
if (Number >= 1 && Number <= 10) {
Number = New1;
}
else{
System.out.println("Error: Enter a number between 1 and 10");
}
Scanner keyboard = new Scanner(System.in);
char letter = keyboard.next().charAt(0);
for (New1 = 1; New1 <= 10; New1++) {
for (letter = (char) Linecounter; letter <= 10; letter++) {
System.out.print("" +letter+ "");
}
System.out.println();
}}}
First off:
if (Number >= 1 && Number <= 10) {
Number = New1;
}
Sets Number = 1. After this code executes, both Number and New1 are equal to 1. You want something where Number is set to the input. Now the loops need work. You should have something like this:
for (int i = 1; i <= Number; i++) { //1 through Number
for (int j = 1; j <= i; j++) {
System.out.print(letter); //Print letter i times
}
System.out.print("\n"); //New line
}
Is it something like this, you're searching for?
int x = 5;
char letter = 'x';
for (int i = 0; i <= x; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(letter);
}
System.out.println();
}
The above outputs
x
xx
xxx
xxxx
xxxxx
This seems like homework, so I'll just bring you pseudo code. The idea is to do something like this:
for i in 1..x do
for j in 1..i do
print ('*')
end
println (''
end
(in fact, if you change the println for puts this becomes a valid Ruby script).
Anyway, the key here is: you need as many asterisk as lines entered by the user. As long as you print an asterisk in the inner loop without printing a newline, and call println in your outer loop, you are ok to go.
It is also important that you iterate up to the wanted value in the outer loop, but only up to the New1 variable in the inner loop.
Try this:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Integer: ");
int userInt = scan.nextInt();
System.out.print("Letter: ");
String userLetter = scan.next();
String letter = "";
for (int i = 0; i <= userInt; i++) {
System.out.println(letter);
letter += userLetter;
}
}
This doesn't limit the input to be between 1-10, you can add a little if statement for that.
How can I print a pyramid in Java like this
1
23
456
78910
My current code looks like this:
public class T {
public static void main(String[] args) {
int i, j, num = 1;
int n = Integer.parseInt(args[0]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.println(num);
num++;
}
System.out.println(" ");
}
}
}
If I try this removing declared i & j then it shows an array out of bounds exception
However 'i' & 'j' are creating the problem. What should my code look like.
int val=1;
for(int i=0;i<6;i++){
for(int j=1;j<i;j++){
System.out.print(val);
val++;
}
System.out.print("\n");
}
initially val is equal to 1 . Inside the first for loop i=0 and j with increase from 1, but when i=0 second for loop doesn't run. then you get the first value as 1. Then it will point to new line.
When i=1,j still 1 so second for loop runs 1 time and print 2, because val has increment(val++). when j=2 in inside for loop it is not running only print the new value (3) of val there.
so on this will work
public static void main(String[] args) {
int num = 1;
//i is how many numbers per row
for(int i = 1; i < 5; i++){
//prints i numbers because j increases from 0 to i, incrementing num each time
for(int j = 0; j < i; j++){
System.out.print(num++);
}
System.out.println();
}
}
This code will work for your purposes.
Now, please read on if you would like to understand Java better and see why the compiler was throwing errors in your code. You shouldn't use stackoverflow to copy in paste someone else's code without understanding it. In your code, you were declaringi and j twice. In Java, you cannot declare a variable twice. You did it first in int i,j, num = 1; and then again in each for loop for (int i = 1; i <= lines; i++). You could correct this by saying for(i = 1; i <= lines; i++). Notice how the int is left out in the second version of the for loop. You can simply assign a value to a variable in a for loop rather than creating a new variable as you do when declare the type int i = 1
The syntax of a for loop is:
for(initialization; Boolean_expression; update)
{
//Statements
}
The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
As for the array out of bounds error that you receive, you are trying to read in a command line argument in the statement int n = Integer.parseInt(args[0]); Notice how the main method has a parameter String[] args. These are called command line arguments and can be passed in if you manually run the program from the command line. You were trying to read in args[0] which is outside of the bounds of args[].
In other words, if you run
java MyProgram one two
Then args contains:
[ "one", "two" ]
public static void main(String [] args) {
String one = args[0]; //=="one"
String two = args[1]; //=="two"
}
I suppose you give the number of lines as your only argument, so the code would be
public static void main(String[] args)
{
int lines = Integer.parseInt(args[0]);
int num = 1;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num);
num++;
}
System.out.println("");
}
}
int l=1;
for (int i=0; i<5; i++)
{
for (int k=0; k<5-i; k++)
{
System.out.print(" ");
}
for (int j=0; j<(i*2)+1; j++)
{
if(j%2!=0){
System.out.print(l++);
}else {
System.out.print(" ");
}
}
System.out.println("");
}
public static void pyramid(int max) {
int num = 1;
max = 4;
for (int row = 0; row < max; row++) {
for (int column = 0; column < max; column++)
System.out.print(column <= row ? num++ : " ");
System.out.println();
}
}
import java.util.Scanner;
/**
*
* #author shelc
*/
public class PrintNumberPyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the count : ");
int number = scanner.nextInt();
//enter the number of rows you want to print
pyramid(number);
}
public static void pyramid(int rows) {
int count = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(j <= i ? count++ : " ");
}
System.out.println();
}
}
}