Array is not getting printed in Java - java

I want to print an array of size n, where each element is pow(i,i),i ranging from 1 to n. i.e. if I input n = 4, it should return me an array A = {1, 4, 27, 256}. This is because power(1,1) = 1, power(2,2) = 4, power(3,3) = 27 and power(4,4) = 256.
But, when I try to run the below code, it is not giving any output.
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main
{
public static void main(String[] args)
{
Main s = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double[] A = new double[n];
int j ;
for(j = 0; j <= n; j++)
{
A[j] = Math.pow(j+1, j+1);
//System.out.println(A[j]); --> 1
}
System.out.print(A);
System.out.println(A); //-->2
for (int i=0; i<A.length; i++)
{
System.out.print(A[i]+" "); // --> 3
}
}
}
When I try to remove commented quotes for equation 1, it is printing me the values. But neither of equation 2 or 3 is helping me to print the array.

Try This :-
public static void main(String[] args) {
Main s = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double[] A = new double[n];
int j ;
for(j = 0; j <= n-1; j++)
{
A[j] = Math.pow(j+1, j+1);
//System.out.println(A[j]); --> 1
}
System.out.print(A);
System.out.println(A); //-->2
for (int i=0; i<A.length-1; i++)
{
System.out.print(A[i]+" "); // --> 3
}
}

try this:
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main
{
public static void main(String[] args)
{
Main s = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double[] A = new double[n];
int j ;
for(j = 1; j <= n; j++)
{
A[j-1] = Math.pow(j, j);
//System.out.println(A[j]); //--> 1
}
System.out.print(A);
System.out.println(A); //-->2
for (int i=0; i<A.length; i++)
{
System.out.print(A[i]+" "); // --> 3
}
}
}

Instead of j <= n;, the loop condition should be j < n because the index start from 0 and ends at n - 1. If you try to access A[n], it will throw array index out of exception.
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Main s = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double[] A = new double[n];
int j;
for (j = 0; j < n; j++) {
A[j] = Math.pow(j + 1, j + 1);
//System.out.println(A[j]); --> 1
}
System.out.print(A);
System.out.println(A); //-->2
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " "); // --> 3
}
}
}

Related

Error <identifier> expected for System.out.println(); ? How can I fix this?

Why do I get an error that says >error: identifier expected
System.out.println(); . I need a new line after the last element in the array, that is why I added the println. How can I fix this?
import java.util.Scanner;
public class LabProgram {
public static void main(String args[]) {
int n, left,right;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++)
a[i] = s.nextInt();
left=s.nextInt();
right=s.nextInt();
for(int i = 0; i < n; i++)
//check if the current element is within the range
if(a[i]>=left && a[i]<=right)
System.out.print(a[i] + ",");
}
System.out.println();
}
SOP written outside the main method. It must be inside the main method.
import java.util.Scanner;
public class LabProgram {
public static void main(String args[]) {
int n, left,right;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++)
a[i] = s.nextInt();
left=s.nextInt();
right=s.nextInt();
for(int i = 0; i < n; i++) {
//check if the current element is within the range
if(a[i]>=left && a[i]<=right)
System.out.print(a[i] + ",");
}
System.out.println();
}
}

Java Array reversing task compiling error - IndexOutOfBoundsException: 2

I'm trying to reverse the array; i input length of array than it's all values. After that i'm trying to reverse it doing like that: a[i] = a[al - i] in the "for construction" - for(int i = o; i < al; i++);
What am i doing wrong there? The full code:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
int al = s1.nextInt();
int a[] = new int[al];
for (int i = 0; i < al; i++) {
a[i] = s1.nextInt();
}
for(int i = 0; i < al; i++) {
a[i] = a[al - i];
}
for(int i = 0; i < al; i++) {
System.out.println(a[i]);
}
}
}
Problem Description of Your program:
for(int i = 0; i < al; i++) {
//let al=5, so index of a will be 0..4
//so for first iteration i=0
//so a[5-0] i.e: a[5] where 5 is index out of bound
a[i] = a[al - i];
}
Solution of the problem, but this logic will not reverse your array data:
for(int i = 0,j=al-1; i < al; i++,j--) {
a[i] = a[j];
}
For reversing your array data, you have to be used like this:
for (int i = 0, j = al - 1; i < j; i++, j--) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
So, Whole solution,
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
int al = s1.nextInt();
int a[] = new int[al];
for (int i = 0; i < al; i++) {
a[i] = s1.nextInt();
}
for (int i = 0, j = al - 1; i < j; i++, j--) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
for (int i = 0; i < al; i++) {
System.out.println(a[i]);
}
}
}

Array index out of bounds exception in java code

I am writing the code to print a matrix on taking user input in the form of n value:
Suppose if,
n= 3
output:
3 3 3
3 0 3
3 1 3
3 2 3
3 3 3
I am getting ArrayIndexOutOfBoundException in the line: a[i][j]=n;
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
//System.out.println("Enter n");
int n = scan.nextInt();
System.out.println(n);
int a[][]= new int[n][n];
int b=0;
int mid = n/2 +1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i+j==mid)
{
a[i][j]=n-b;
b++;
}
else
{
a[i][j]=n;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
If a request for a negative or an index greater than or equal to size of array is made, then the JAVA throws a ArrayIndexOutOfBounds Exception. This is unlike C/C++ where no index of bound check is done. TheArrayIndexOutOfBoundsException is aRuntime Exception thrown only at runtime.
Look at this line:
for(int j=0;i<n;j++)
you increment while "i < n" but you increment "j++".
When your j reaches value 3, the inner loop continues, but exceeds the array-length (of 3, because the hightest array-index is actually 2).
(Also on a minor sidenote, it is usually preferrable to write ++i or ++j within for-loop increments. This is not a rule, just easier to read for most oldscool c-Devs). Also consider leaving spaces to inprove readability:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// System.out.println("Enter n");
int n = scan.nextInt();
System.out.println(n);
int a[][] = new int[n][n];
int b = 0;
int mid = n / 2 + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j == mid) {
a[i][j] = n - b;
b++;
} else {
a[i][j] = n;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
minor correction in a loop
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
//System.out.println("Enter n");
int n = scan.nextInt();
System.out.println(n);
int a[][]= new int[n][n];
int b=0;
int mid = n/2 +1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++) //minor correction here
{
if(i+j==mid)
{
a[i][j]=n-b;
b++;
}
else
{
a[i][j]=n;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}

Java Scanner cannot read nextInt() unless enter is pressed / new line character

My program for the coding challenge produces the correct output, however, it requires me to press enter a second time for it to work. Here is the programming challenge.
It prints out the first line just fine but requires me to press enter again for it to work.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
sc.nextLine();
for(int i = 0; i < testCases; i++){
int size = sc.nextInt();
sc.nextLine();
int[] arr = new int[size];
for(int x = 0; x < size; x++){
arr[x] = sc.nextInt();
}
sc.nextLine();
if(size > 1){
solve(arr);
}
else{
System.out.println("0");
}
}
}
private static void solve(int[] arr) {
for(int i =0 ; i < arr.length; i++){
arr[i] = Math.abs(arr[i]);
}
Arrays.sort(arr);
for(int i = 0; i < arr.length - 1; i++){
int f = i + 1;
if(arr[i] == arr[f]){
System.out.print((arr[i] * -1) + " " + arr[i]);
}
}
System.out.println();
}
}

i have searched for my answer in stackoverflow but i cant find it and i myself tried it about an hour for relevant answer

this is actually a multiplication of matrices....
import java.util.Scanner;
class MatMu {
public static void main(String args[]) {
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int c[][] = new int[3][3];
System.out.println("enter the first matrix:");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
a[i][j] = input.nextInt();
System.out.println("enter the second matrix:");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
b[i][j] = input.nextInt();
System.out.println("matrix mutiplication is as follows:");
Scanner input = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = 0;
for (int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][i];
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println(a[i][j] + "\t");
}
System.out.println("\n");
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println(b[i][j] + "\t");
}
System.out.println("\n");
}
System.out.println("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println(c[i][j] + "\t");
}
System.out.println("\n");
}
}
}
the error is at a[i][j]=input.nextInt() and b[i][j]=input.nextInt(): cannot find symbols.... and this is a java program and I can't find the symbol i.e., a and b
You have not defined input which i guess is a Scanner before its first use. Define this scanner at the start and then use it.
you are accessing scanner object without initializing on top of code :
Scanner input=new Scanner(System.in);
write code like That:
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
/* your code */
}
import java.util.Scanner;
class MatMu {
public static void main(String args[]) {
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int c[][] = new int[3][3];
Scanner input = new Scanner(System.in); // pasted it here
System.out.println("enter the first matrix:");
// your code
// Scanner input = new Scanner(System.in); // moving this line to top
// your next code...
}

Categories

Resources