Trying to add using a multiple in a for statement - java

I'm trying to change the x into a pyramid of multiples. Ex. if the multiple was 2 it would add by 2s going down. I kept getting multiples that would mess up and swap around when I tried and was wondering if anyone would be kind enough to help!
This is what I have so far:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int rows=0;
int multiple=0;
int x=0;
Scanner scan = new Scanner(System.in);
System.out.println("Input Rows to Generate: ");
rows = scan.nextInt();
System.out.println("Input Multiple to Count by: ");
multiple = scan.nextInt();
System.out.println();
for (int i = 1; i<=rows; i++)
{
for (int j = 1; j<=i; j++)
{
System.out.print("x");
}
System.out.println();
}
}
}
ex.
Enter the number of rows:
5
Enter the multiple to count by:
1
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int rows=0;
int multiple=0;
int x=0;
Scanner scan = new Scanner(System.in);
System.out.println("Input Rows to Generate: ");
rows = scan.nextInt();
System.out.println("Input Multiple to Count by: ");
multiple = scan.nextInt();
System.out.println();
int n = 0;
for (int i = 1; i <= rows; i++){
for (int j = 1; j <= i; j++){
System.out.print(multiple * n + " ");
n++;
}
System.out.println();
}
}
}
For rows = 5 and multiple = 1 the output will be:
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
Also, for rows = 5 and multiple = 2 the output will be:
0
2 4
6 8 10
12 14 16 18
20 22 24 26 28

Related

Program for this number pattern using for loop in java 1 22 3 4444 5 666666 7 88888888. Not getting the desired pattern

public static void main(String[] args) {
int noOfRows = 8;
printPattern(noOfRows);
}
private static void printPattern(int num){
for(int i = 1; i <= num; i++){
for(int j = 1; j<=i;j++){
System.out.print(i);
}
System.out.println();
}
Program for this number pattern using for loop in java 1 22 3 4444 5 666666 7 88888888. Not getting the desired pattern
You only need to print odd numbers once, so add a break
for(int i = 1; i <= 8; i++){
for(int j = 1; j<=i;j++){
System.out.print(i);
if (i % 2 == 1)
break;
}
System.out.println();
}
output
1
22
3
4444
5
666666
7
88888888

Storing a matrix from a text file into a 2D array

I am having trouble storing a matrix from a text file into a 2D array. Every time I run the following code, it executes with no errors but nothing is printed out in the console. Any help would be greatly appreciated. My code is shown below:
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input1.txt"));
String [][] array = new String [9][9];
try{
for(int i = 0; i > array.length; i++) {
for(int j = 0; j < array[0].length; j++) {
array[i][j] = sc.next();
System.out.print(array[i][j] + " ");
}
}
}catch(Exception e){
System.out.print("error");
}
Also this is the content of the text file I am reading in:
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
3 4 5 2 8 6 1 7 9
The problem is with your first loop, also it's better to use nextInt() when the content of "input1.txt" is int
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input1.txt"));
int [][] array = new int [9][9];
try{
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[0].length; j++) {
array[i][j] = sc.nextInt();
System.out.print(array[i][j] + " ");
}
}
}catch(Exception e){
System.out.print("error");
}
you have error in first for loop
Scanner sc = new Scanner(new File("input1.txt"));
String [][] array = new String [9][9];
try{
for(int i = 0; i < array.length; i++) { // your error was here. you wrote int i = 0 i > array.length; i++
for(int j = 0; j < array[0].length; j++) {
array[i][j] = sc.next();
System.out.print(array[i][j] + " ");
}
}
}catch(Exception e){
System.out.print("error");
}

How to search for intervals in Java

Just before I begin to annoy you, I am a Java beginner and my main issue up to date is two- ord more-dimensional arrays.
Let me represent my problem:
The task in the study was: you have an sorted array, then you type the number of how many searching operations(let them named by N) the Programm should do.
Due to this you type N x 2 different values a and b, a should be smaller than b.
The Programm should search in the sorted array for intervalls containing your a and b and print these intervalls out.
For example:
The array to search for looks like this:
[1, 3, 5, 7, 9]
then the user types in the number of operations, for example 3. Following to this the a and b couples:
3 6
2 9
1 5
The output of the programm should be according to these a and b couples:
3 7
1 9
1 5
I tried to master this exercise but as mentioned above I have problems with multidimensional arrays. At the following you can see my code and I just want tipps or specific informations about arrays to understand how they're working.
And i improved it:
import java.util.*;
public class Blatt9{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
boolean check1 = false;
int N = s.nextInt();
int[] array = new int[N];
for(int i = 0; i < array.length; i++){
array[i] = s.nextInt();
}
for(int j = 0; j < array.length; j++){
for(int k = j+1; j < array.length -1; j++){
if(array[j] < array[k]){
check1 = true;
}
else{
check1 = false;
}
}
}
if(check1){
System.out.println("nicht aufsteigend sortiert");
}
else{
System.out.println("korrekt");
}
//Beginn der eigentlichen Aufgabe
int abfragen = s.nextInt(); // Anzahl der Abfragen
int[][] arr = new int[abfragen][2]; //um die abfragen zu speichern
int a = 0;
int b = 0;
for(int i = 0; i < abfragen; i++){
a = s.nextInt();
b = s.nextInt();
if(a>b){
System.out.println("Zahl 1 muss kleiner Zahl 2 sein");
break;
}
arr[i][0] = a;
arr[i][1] = b;
}
int intervall1 = 0;
int intervall2 = 0;
for(int i = 0; i < array.length; i++){
for(int j = 0; j < arr.length; j++){
if(array[i] <= a){
intervall1 = a;
}
if(array[i]>= b){
intervall2 = b;
}
}
System.out.print(intervall1 + " " + intervall2);
System.out.println();
}
}
}
The output now is:
input:
5
1 3 5 7 9
korrekt
3
3 6
2 9
1 5
begin of output:
1 0
1 0
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5

How can I fix my code? 2D Array

I want to create a 2D array using the user's input and the creating random numbers in the second line.
E.g:
Output should be:
If the user enters "7" then:
1 2 3 4 5 6 7 (User's input)
0 2 4 8 9 8 5 (Random numbers)
but instead I only get one random number.
My code is working but I can't see to create the array correctly.
My code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of exits: ");
int n = input.nextInt();
int [][] A = new int[2][n];
for(int i=0; i<A.length; i++){
for (int j = 0; j<n; j++) {
A[i][j] = (int) (Math.random()*10);
}
}
System.out.println(A[1][n-1]);
System.out.print("Distance between exit i and exit j is: " + distance());
}
public static int distance(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter exit i: ");
int i = input.nextInt();
System.out.print("Please enter exit j: ");
int j = input.nextInt();
return i + j;
}
}
How can I fix it?
Would this help?
int n = input.nextInt();
Random rand = new Random();
int [][] A = new int[2][n];
for (int i = 0; i<n; i++) {
A[0][i] = i+1;
A[1][i] = rand.nextInt(10);
}
Am not too certain about what you mean by:
1 2 3 4 5 6 7 (User's input)
0 2 4 8 9 8 5 (Random numbers)
Do you want users to manually enter "1 2 3 4 5 6 7"?
Eitherway, here's something to help with the printing aspect:
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter number of exits: ");
int n = input.nextInt();
int [][] A = new int[2][n];
for(int i=0; i<A.length; i++){
for (int j = 0; j<n; j++) {
A[i][j] = (int) (Math.random()*10);
}
}
for(int[] b: A)
{
for(int k: b)
{
System.out.print(k + " ");
}
System.out.println();
}
System.out.print("Distance between exit i and exit j is: " + distance());
}

Jagged array in java

My final output should be like:
How many rows are in the jagged array? 4
Enter a row, separated by spaces: 9 2 14 5 8
Enter a row, separated by spaces: 3
Enter a row, separated by spaces: 15 23
Enter a row, separated by spaces: 9 8 7 6 5 4 3
After the funky operation, the resulting array is:
9 4 42 20 40
6
45 92
36 40 42 42 40 36 30
But I keep getting errors:
Exception in thread "main" java.util.NoSuchElementException: No line found
at ScannerHacked.nextLine(ScannerHacked.java:1525)
at jagged.main(jagged.java:14)
Here is my code:
import java.util.Scanner;
public class jagged {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many rows are in the jagged array? ");
int row = sc.nextInt();
int[][] jaggedArray = new int[row][];
for(int i = 0; i < row; i++)
{ Scanner rows = new Scanner(System.in);
System.out.print("Enter a row, separated by spaces: ");
String arraystring = rows.nextLine();
String []a = arraystring.split(" ");
jaggedArray[i] = new int[a.length];
for(int j = 0; j < jaggedArray[i].length; j++)
{
int y = Integer.parseInt(a[j]);
jaggedArray[i][j] = y;
}
}
System.out.println("After the funky operation, the resulting array is:");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < jaggedArray[i].length; j++)
{
if(jaggedArray[i][j] > 9)
System.out.print(" "+(jaggedArray[i][j]*(i+j+1)) + "");
else
System.out.print(" "+(jaggedArray[i][j]*(i+j+1)) + "");
}
System.out.print("\n");
}
}
}
As i can see, you are using two Scanner with the same source (System.in) in main() method...
Scanner sc = new Scanner(System.in);
Scanner rows = new Scanner(System.in);
it might throw an Exception, So you should close first Scanner i.e. sc.close(); when going to use another Scanner with same source.

Categories