This question already has answers here:
Java Minimum and Maximum values in Array
(13 answers)
Closed 6 years ago.
I am making a program that finds the min/max numbers in a java array. I am currently stuck at the last part which is finding the min/max. I have currently setup all other parts of the program. This is my code.
import java.util.Scanner;
public class X {
public static void main (String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter size of array");
int n= input.nextInt();
int[] x= new int[n];
System.out.println("Enter Array nums");
for(int i=0;i<n;i++){
x[i]= input.nextInt();
}}}
Arrays.sort(x);
sorts the array, so after doing that all you need to do is look in the first and last element to find the min and max.
You can use the following methods to directly find the max and min.
List list = Arrays.asList(x);
System.out.println(Collections.min(list));
System.out.println(Collections.max(list));
simple.
So Simple actually
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Enter size of array");
int n= input.nextInt();
int[] x= new int[n];
System.out.println("Enter Array nums");
for(int i=0;i<n;i++){
x[i]= input.nextInt();
}}}
Arrays.sort(x);
System.out.println(String.format("Min= %d Max= %d",x[0],x[x.length -1]));
}
import java.util.Scanner;
public class X {
public static void main (String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter size of array");
int n= input.nextInt();
int[] x= new int[n];
int min_num,max_num;
System.out.println("Enter Array nums");
for(int i=0;i<n;i++){
x[i]= input.nextInt();
if(i==0){
min_num=max_num=x[i];
}else{
if(x[i]>max_num)
max_num=x[i];
if(x[i]<min_num)
min_num=x[i];
}}}
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I am learning about Arrays and had gotten this assignment. Whenever I run the code, the value which is supposed to go into String array goes into Int. It skips over the name[i]=sc.nextLine(); completely.
Any Help will be appreciated.
import java.util.*;
public class Marks_Remarks {
public static void main(String []Args){
Scanner sc= new Scanner (System.in);
System.out.println("Enter how many students");
int n= sc.nextInt();
String name[]= new String[n];
int roll[]= new int[n];
int sub1[]= new int[n];
int sub2[]= new int[n];
int sub3[]= new int[n];
for (int i=0;i<n;i++){
System.out.println("Enter Name");
name[i]= sc.nextLine();
System.out.println("Enter Roll No.");
roll[i]= sc.nextInt();
System.out.println("Enter marks in Three Subjects");
sub1[i]= sc.nextInt();
sub2[i]= sc.nextInt();
sub3[i]= sc.nextInt();
}
int avg; String remark;
for (int k=0;k<n;k++){
avg= (sub1[k]+ sub2[k]+ sub3[k])/3;
if (avg>=85)
remark= "Excellent";
else if(avg>=75 && avg<=84)
remark= "Distinction";
else if(avg>=60 && avg<=74)
remark= "First Class";
else if(avg>=40 && avg<=59)
remark="Pass";
else
remark="Poor";
System.out.println("Name: "+ name[k]+ "\tRoll No.: "+ roll[k]+ " \tAverage: "+ avg+ " \tRemark: " + remark);
avg=0;
}
}
}
Use name[i]=sc.next(); instead of : name[i]=sc.nextLine();
It may help you.
I am trying to make a program to print a multiplication table in which the user enters the number of table he wants (for example 3) , also enters the values of table he wants( 2,6,5 (random/unordered)), also enters the range of multiplier.
my code is printing all the elements present in the array.
please help me. ThankYou.
import java.util.Scanner;
class Addition
{
public static void main(String[] args) {
int j=0,k=0;
Scanner obj=new Scanner(System.in);
int arr[]=new int[32];
System.out.println("how many multiplication table do you want to print ? ");
int n=obj.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println("Enter number whos table you want ");
for(j=1;j<=n;j++)
{
arr[j]=obj.nextInt(); break;
}
}
System.out.println("Enter range of multiplier");
int range=obj.nextInt();
for(int l=0;l<=arr[j];l++)
{
for(k=1;k<=range;k++)
{
System.out.println(" "+l+" * "+k+" = "+l*k);
}
}
}
}
the output I want will look somewhat like this(
how many multiplication table do you want to print ? =3 and on entering numbers 2,5,6 and range=4)
2x1=2
2x2=4
2x3=6
2x4=8
5x1=5
5x2=10
5x3=15
5x4=20
6x1=6
6x2=12
6x3=18
6x4=24
I made a few adjustments to carry out what i think you trying to accomplish. you could use an arraylist seen below:
class Addition{
public static void main(String[] args) {
int j=0,k=0;
Scanner obj=new Scanner(System.in);
ArrayList<Integer> typeMultiplicationTable = new ArrayList<Integer>();
//used Arraylist instead of an array
//int arr[]=new int[32];
System.out.println("how many table? ");
int n=obj.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println("Enter number whos table you want ");
for(j=0;j<=n;j++)
{
typeMultiplicationTable.add(obj.nextInt());
break;
//arr[j]=obj.nextInt(); break;
}
}
System.out.println("Enter range");
int range=obj.nextInt();
for(int l=0;l<=typeMultiplicationTable.size()-1;l++)
{
for(k=0;k<=range;k++)
{
System.out.println(" "+typeMultiplicationTable.get(l)+" * "+k+" = "+typeMultiplicationTable.get(l)*k);
}
}
}}
and if you do end up using an array, you could achieve it as follows - i ended up removing one of the for loops.
class Addition{
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("how many table? ");
int n=obj.nextInt();
int arr[]=new int[n];
for(int j=0;j<=arr.length-1;j++)
{
System.out.println("Enter number whos table you want ");
arr[j]=obj.nextInt();
}
System.out.println("Enter range");
int range=obj.nextInt();
for(int l=0;l<=arr.length-1;l++)
{
for(int k=0;k<=range;k++)
{
System.out.println(" "+arr[l]+" * "+k+" = "+arr[l]*k);
}
}
}}
How can we take n input in java according to value of T
Like if i take t=2,then we take two n input
If value of t=3,then we take three n input
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
int t=s.nextInt();
int n=s.nextInt();
int n=s.nextInt();
int evensum=0;
for (int i=1; i<=n; i++) {
if(i%2==0) {
evensum=evensum+i;
}
System.out.print(evensum);
}
}
As in this code i take t=2 and I take two n inputs.
If I take t=3, then what how could I take that third input?
You can't have multiple variable with the same name, for your purpose use a loop :
Scanner s = new Scanner(System.in);
System.out.println("How many values ?");
int nbInput = s.nextInt();
for (int i = 1; i <= nbInput; i++) {
int input = s.nextInt();
System.out.print(input);
}
I am trying to sort a numeric array in ascending and descending order. I am beginner so using the following link Sort an array in Java . I am trying to get input from user as array's elements.
public class SortingofString {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int Array1[];
// String Array2[];
System.out.println("How many numaric elements: ");
int n = input.nextInt();
int[] array1 = new int[n];
int number=input.nextInt();
for(int i=0; i<n; i++){
System.out.println("Enter number:");
array1[i] = number;
System.out.println("Original numeric array : "+Arrays.toString(Array1));
Arrays.sort(Array1);
System.out.println("Sorted numeric array : "+Arrays.toString(Array1));
}
}
}
The error occurs when i pass my array_name Array1 in first toString function.System.out.println("Original numeric array : "+Arrays.toString(Array1));
Error says Initialize variable Array1 . How can i resolve this error?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//int Array1[];
System.out.println("How many numaric elements: ");
int n = input.nextInt();
int arr[] = new int[n];// solve 1st problem
for (int i = 0; i < n; i++) {
System.out.println("Enter number: " +(i+1));
int number = input.nextInt();
arr[i]=number;//init array by user input data
}
System.out.println("Original numeric array : " + Arrays.toString(arr));
Arrays.sort(arr);
System.out.println("Sorted numeric array : " + Arrays.toString(arr));
}
you also need 2 import
import java.util.Arrays;
import java.util.Scanner;
Move (and rename) Array1 behind reading n:
System.out.println("How many numaric elements: ");
int n = input.nextInt();
int[] array1 = new int[n];
And maybe use the entered number:
int number = input.nextInt();
array1[i] = number;
Im relatively new to programming and i was working with a problem that requires me to read space separated integers and input them into a two dimensional array but i dont seem to understand why I am not able to do so . any hint on possible approaches will be helpful
Eg. Input will look like this
2 //FOR DIMENSION
2 3 //ROW1
4 5 // ROW 2
import java.io.*;
import java.util.*;
import java.lang.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Hello World");
System.out.println("Enter Dimension of Matrix");
int N= sc.nextInt();
int[][] m=new int [N][N];
for (int i=0;i<N;i++)
{ System.out.println("Enter Elements of row "+ i);
for(int j=0;j<N;j++ )
{
m[i]=sc.nextInt();
}
}
System.out.println(m);
}
}
I tested the above code and got the following result on the console
HelloWorld.java:16: error: incompatible types: int cannot be converted to int[]
m[i]=sc.nextInt();
^
First of all, I would like to know is my thinking(approach) right?
Second, what are the possible ways to do this correctly?
here you go:
import java.io.*;
import java.util.*;
import java.lang.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Dimension of Matrix");
int N = sc.nextInt();
int[][] m = new int[N][N];
for (int i = 0; i < N; i++) {
System.out.println("Enter Elements of row " + i);
for (int j = 0; j < N; j++) {
m[i][j] = Integer.valueOf(sc.next());//replaced line with proper code
}
}
sc.close();
System.out.println(Arrays.deepToString(m));//use this api to print arrays
}
}
m is a multidimensional array meaning you must specify m[i][?]. Like so
m[i][j]=sc.nextInt();
Your logic is a little off, you want to ask the user for input at row start i, and for the other columns j. Try this and happy coding :)
for (int i=0;i<N;i++){
for(int j=0;j<N;j++){
System.out.println("Enter Elements of row "+ i);
m[i][j]=sc.nextInt();
}
}
for (int i=0;i<N;i++){
for(int j=0;j<N;j++){
System.out.print(m[i][j]);
}
System.out.println();
}