Reputation: 125
So I'm trying to read all the input from one line, using scanner, then take the values and find the second largest. I would use an array BUT I am not allowed. You are supposed to enter 10 integers, hit enter and evaluate them.
Something like this:
10 20 30 40 50 60 70 80 90 100 ENTER
Second highest number is: 90
I can't manage to solve it at all. It should be easy but I have no idea.
public class SecondLargest {
public static void main(String[] args) {
{
int largest = 0;
int secondLargest = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter integers: ");
int numbers = sc.nextInt();
largest = numbers;
while (sc.hasNextInt()) {
if (numbers > largest) {
secondLargest = largest;
largest = numbers;
} else if (numbers > secondLargest) {
secondLargest = numbers;
}
}
System.out.println("Second largest number is: " + secondLargest);
sc.close();
}
}
Upvotes: 2
Views: 40272
Reputation: 79015
Start with assigning the first scanned integer to largest
and secondLargest
variables and then process the remaining nine integers in a loop as follows:
num = sc.nextInt();
if (num > largest) {
secondLargest = largest;
largest = num;
}
Demo:
import java.util.Scanner;
public class SecondLargest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter 10 integers separated by single spaces: ");
// Scan the first integer and assign it to largest and secondLargest
int num = sc.nextInt();
int largest = num;
int secondLargest = largest;
// Input 9 more integers
for (int i = 1; i < 10; i++) {
num = sc.nextInt();
if (num > largest) {
secondLargest = largest;
largest = num;
}
}
System.out.println("The second largest number is: " + secondLargest);
}
}
A sample run:
Enter 10 integers separated by single spaces: 10 23 4 12 80 45 78 90 105 7
The second largest number is: 90
Note: This is just a sample program assuming that the input is in the correct format. I leave it to you to work on how to deal with the wrong input. It will be a good exercise for you.
Upvotes: 1
Reputation: 11
To read single line input, You can do this simply by eating the (\n) which is new line in Java.
Input : 1 2 3
\\Create the object of Scanner Class
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
// this will eat the new line and move forward for other inputs.
sc.nextLine()
To read in a loop or to store at 2D array.
Input : 1 2 3 3 4 5 6 7 8
//... do the above declaration of array
for ( int i = 0; i < n; i++){
for( int j = 0; j < m; j++){
arr[i][j] = sc.nextInt()
}
sc.nextLine(); // you need to eat the \n here.
}
This will work perfectly.
Now you should be able to get the input easily in a single line.
Upvotes: 1
Reputation: 1
import java.util.*;
public class m1{
public static void main(String args[]){
Scannerscan = new Scanner(System.in);
Stringnum = scan.nextLine();
String[] strs = num.split("\\s+");
int[] arrs = new int[strs.length];
for(inti=0;i<strs.length;i++)
{
String stringnum = strs[i];
arrs[i] = Integer.parseInt(stringnum);
if(arrs[i]%2==0){
System.out.print(" ");
}
else{
System.out.print(arrs[i]);
}
}
scan.close();
}
}
Upvotes: 0
Reputation: 19
/*
**In this code I've used BufferedReader class which is faster than Scanner
Class as well as have large buffer of 8KB while Scanner has only 1KB.**
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Second_Highest {
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] s=br.readLine().split(" ");
int a[]=new int[10];
int max=Integer.parseInt(s[0]);
for(int i=0;i<10;i++)
{
a[i]=Integer.parseInt(s[i]);
if(a[i]>max)
{
max=a[i];
}
}
int secondmax=Integer.parseInt(s[0]);
for(int i=0;i<10;i++)
{
if(a[i]>secondmax && a[i]<max)
{
secondmax=a[i];
}
}
System.out.print(secondmax);
}
}
Upvotes: 1
Reputation: 75
Below snippet can be used to take multiple Integer Input on same line.
Scanner sc= new Scanner(System.in); // Declare and Initialize Scanner
while(sc.hasNext()) // While the input has data execute
System.out.println(sc.nextInt()); // nextInt() method is used to take Integer data
For Multiple String input on same line separated by white space or a comma, we can use the following snippet
Scanner sc = new Scanner(System.in).useDelimiter("[,\\s+]");
Upvotes: 0