Input S 5 Jan Feb Mar Apr May Expected output [May, Apr, Mar, Feb, Jan] Your Program Output [Jan, Feb, Mar, Apr, May] import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String ch = sc.nextLine(); if (ch.equalsIgnoreCase("I")) { ArrayList<Integer> intList = new ArrayList<>(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { intList.add(sc.nextInt()); } System.out.print(intList); } else if (ch.equalsIgnoreCase("S")) { ArrayList<String> strList = new ArrayList<>(); int n = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { strList.add(sc.nextLine()); } System.out.print(strList); } else { System.out.println("Invalid Choice"); } sc.close(); } }
Question
Input S 5 Jan Feb Mar Apr May Expected output [May, Apr, Mar, Feb, Jan] Your Program Output [Jan, Feb, Mar, Apr, May]
import java.util.ArrayList; import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ch = sc.nextLine();
if (ch.equalsIgnoreCase("I")) {
ArrayList<Integer> intList = new ArrayList<>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
intList.add(sc.nextInt());
}
System.out.print(intList);
} else if (ch.equalsIgnoreCase("S")) {
ArrayList<String> strList = new ArrayList<>();
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
strList.add(sc.nextLine());
}
System.out.print(strList);
} else {
System.out.println("Invalid Choice");
}
sc.close();
}
}
Solution
The code you provided is written in Java. It reads a character from the input, if it's 'I', it reads an integer 'n' and then 'n' more integers and stores them in an ArrayList. If the character is 'S', it reads an integer 'n' and then 'n' more strings and stores them in an ArrayList. However, it doesn't reverse the list as expected in the output.
To reverse the list, you can use the Collections.reverse() method. Here is the modified code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ch = sc.nextLine();
if (ch.equalsIgnoreCase("I")) {
ArrayList<Integer> intList = new ArrayList<>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
intList.add(sc.nextInt());
}
Collections.reverse(intList);
System.out.print(intList);
} else if (ch.equalsIgnoreCase("S")) {
ArrayList<String> strList = new ArrayList<>();
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
strList.add(sc.nextLine());
}
Collections.reverse(strList);
System.out.print(strList);
} else {
System.out.println("Invalid Choice");
}
sc.close();
}
}
This code will now print the reversed list as expected.
Similar Questions
import java.io.*;import java.util.*;public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int Myint = scanner.nextInt(); double d = scanner.nextDouble(); scanner.nextLine(); String mystring = scanner.nextLine(); System.out.println(mystring); System.out.println(d); System.out.println(Myint); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ }}
import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements(); // invoke the method System.out.println("The sum is "+); } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } } ; //return statement }}
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0) { String[] np = br.readLine().split(" "); int n = Integer.parseInt(np[0]); int p = Integer.parseInt(np[1]); String[] arr = br.readLine().split(" "); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(arr[i]); } Arrays.sort(a); int[] prefix = new int[n]; prefix[0] = a[0]; for(int i = 1; i < n; i++) { prefix[i] = a[i] + prefix[i - 1]; } int ans = a[p - 1] * p - prefix[p - 1]; for(int i = p; i < n; i++) { ans = Math.min(ans, a[i] * p - (prefix[i] - prefix[i - p])); } System.out.println(ans); } } }
Arrange the below code in correct order :Scanner sc=new Scanner(System.in);import java.util.Scanner;int num1 = sc.nextInt();public class Main{}}System.out.println("The value is "+num1);public static void main(String args[]) {
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } Arrays.sort(A); int happyCount = 0; int j = 0; for (int i = 0; i < N; i++) { while (j < N && A[j] - A[i] < K) { j++; } if (j > i + 1) { happyCount++; } } System.out.println(happyCount); } } i need output as 5
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.