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");
}
Related
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
1
333
55555
7777777
999999999
Program to print number pyramid. I want to print this pattern in Java.
My code:
private static void pyramid() {
System.out.println("Please Enter any number less than 10 : ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int temp = num;
for (int row = 0; row <= num; row++) {
for (int column = 0; column < temp; column++) {
System.out.print(" ");
}
temp--;
for (int k = 0; k <= row-1; k++) {
if (row % 2 != 0) {
System.out.print(row);
}
System.out.println();
}
}
}
And I am getting following output:
Please Enter any number less than 10 :
9
1
3
3
3
5
5
5
5
5
7
7
7
7
7
7
7
9
9
9
9
9
9
9
9
9
System.out.println(" 1");
System.out.println(" 333");
System.out.println(" 55555");
System.out.println(" 7777777");
System.out.println("999999999");
Your teacher probably wants you to use loops.
So you should note that the bulk standard for loop which programmers can bang out with their eyes closed:
for (int i = 0; i < someNumber; i++) {
//..
}
Is fairly configurable. For instance the i++ at the end means to increment i, but we could increment by bigger amounts (or decrement, or do some other funky things like stepping through a list of objects and so on and so forth).
e.g.
i += 3;
Will increase i by three.
You can also nest loops inside one another, e.g.
for (int i = 1; i < 10; i+=2) {
String s = "";
for (int j = 0; j < i; j++) {
s += i;
}
System.out.println(s);
}
The padding at the front I leave as an exercise to the reader.
Note that this pattern (one loop inside another, and the inner loop being bounded by the outer loops counter) is actually quite common 'out in the wild', and so is worth investing the time to understand.
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
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());
}
I have to create a 2D jagged array with a random number of rows (5-10) with each row having a random length (5-10). I filled the jagged array with random numbers. It should look something like this:
2 4 1 5 3 8 6 3
2 5 8 9 7 4 3 5 6
6 7 9 3 5
2 6 7 8 4 5 3 6 7
1 4 2 2 1
This is my current createArray method
public static int [][] createArray(){
int row = (int)(Math.random()*5)+5;
int column = (int)(Math.random()*5)+5;
int[][]array = new int[row][];
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
//Fill the matrix with random numbers
array[i][j] = (int)(Math.random()*10);
}}
return array;
}//End createArray method
However, this just randomizes the rows and columns and doesn't create a jagged array. Can anyone help lead me in the right direction? Thanks a lot!
As #DoubleDouble stated, your code throws a NullPointerException.
It looks like you want something like this:
public static int [][] createArray(){
int row = (int)(Math.random()*5)+5;
//int column = (int)(Math.random()*5)+5; //not needed
int[][] array = new int[row][];
for(int i = 0; i < array.length; i++){
int column = (int)(Math.random()*5)+5; //create your random column count on each iteration
array[i] = new int[column]; //Initialize with each random column count
for(int j = 0; j < array[i].length; j++){
//Fill the matrix with random numbers
array[i][j] = (int)(Math.random()*10);
}
}
return array;
}//End createArray method
Of course it will produce different results each time it runs, but here is a sample of what it will output:
1 2 5 4 3 9 2 7 9
4 1 4 2 2 6
9 5 7 8 7 8 4 2
8 3 8 7 9 4 0
0 2 1 4 9 3 7 8
4 0 3 8 3
1 3 8 9 9 8
package JavaPrograms;
import java.util.Random;
public class jaggedarr
{
public static void main(String[] args) {
int a[][] = new int[3][];
Random r = new Random();
a[0] = new int[4];
a[1] = new int[2];
a[2] = new int[3];
for (int[] a1 : a)
{
for (int j = 0; j < a1.length; j++)
{
a1[j] = r.nextInt(20);
}
}
for(int i[] : a)
{
for(int j : i)
{
System.out.print(j + " ");
}
System.out.println("");
}
}
}