i m not getting correct output? - java

import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] in = new String[n];
for (int i = 0; i < n; i++) {
in[i] = sc.nextLine();
}
for (int i = 0; i < n; i++) {
System.out.println(in[i]);
}
}
}
Input:
2
Ankit
Output:
Ankit

That's because of the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter" and so the call to Scanner.nextLine returns after reading that newline.
There are two options to resolve this issue,
1. read the input through Scanner.nextLine and convert your input to the proper format you need
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String[] in = new String[n];
for (int i = 0; i < n; i++)
{
in[i] = sc.nextLine();
}
for (int i = 0; i < n; i++)
{
System.out.println(in[i]);
}
2. Add Scanner.nextLine call after each Scanner.nextInt
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] in = new String[n];
for (int i = 0; i < n; i++)
{
in[i] = sc.nextLine();
}
for (int i = 0; i < n; i++)
{
System.out.println(in[i]);
}

the Scanner class skips next line after nextint so you can fix it by editting your code like this-
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String[] in=new String[n];
for(int i=0;i<n;i++){
in[i]=sc.nextLine();
}
for(int i=0;i<n;i++){
System.out.println(in[i]);
}
}
}

Related

Input numeric ranges with java.util.Scanner

I would input numeric ranges (int arrays with two elements) like this:
Enter a number: 3
Enter a range: -3 5
Enter a range: 0 4
Enter a range: 6 10
I use java.util.Scanner, but I do not know how to proceed:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
for(int i=0;i<number;i++)
String str = input.nextLine();
}
}
I think, you just nead a solution for input a numeric array. Check out this: Java Scanner Array
You can read the whole line, then split it into a String array:
public static void main(String[] args) {
try (var scanner = new Scanner(System.in)) {
System.out.print("Enter the count of arrays: ");
int arrayCount = scanner.nextInt();
scanner.nextLine();
// System.out.print("Enter the length of an array: ");
int arraySize = 2;
int[][] ranges = new int[arrayCount][arraySize];
for (int i = 0; i < arrayCount; i++) {
System.out.printf("[%d] Enter the elements: ", i);
String line = scanner.nextLine();
String[] numbers = line.split("\\D+", arraySize + 1); // separator: 1+ non-digit
for (int j = 0; j < arraySize; j++) {
ranges[i][j] = Integer.parseInt(numbers[j]);
}
}
// Arrays.stream(ranges).map(Arrays::toString).forEach(System.out::println);
}
}
Or simply scan ints one-by-one:
public static void main(String[] args) {
try (var scanner = new Scanner(System.in)) {
System.out.print("Enter the count of arrays: ");
int arrayCount = scanner.nextInt();
// System.out.print("Enter the length of an array: ");
int arraySize = 2;
int[][] ranges = new int[arrayCount][arraySize];
for (int i = 0; i < arrayCount; i++) {
System.out.printf("[%d] Enter the elements: ", i);
for (int j = 0; j < arraySize; j++) {
ranges[i][j] = scanner.nextInt();
}
}
// Arrays.stream(ranges).map(Arrays::toString).forEach(System.out::println);
}
}

I have to print the array , but for each loop is not working

import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k-1];
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);
arr[i] =sc1.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
I am giving output using Scanner class. But is not printing the array.
You don't need to declare the Scanner again inside the loop. Another thing that you should do to be sure of your code, is to have this condition on the loop if i < arr.length. Lastly, I moved the "Print array" message outside the last for.
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr = new int[k];
System.out.print("Enter array :");
for(int i=0; i<arr.length; i++) {
arr[i] =sc1.nextInt();
}
System.out.println("Print array");
for(int element :arr){
System.out.println(element);
}
}
}
There are two problems with this code,
1. You don't need to create a new Scanner object for each user input.
2. You are declaring an array size of k-1 and then asking user input k times.
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k]; // To store k elements, you need k sized array
System.out.print("Enter array :");
for (int i = 0; i <= (k - 1); i++) {
// Scanner sc1 = new Scanner(System.in); / / Not required here
arr[i] = sc.nextInt();
}
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
}
There is only one problem in your code you are declaring the size of array as k-1 instead declare it for k elements.You just need to give each inputs in new line. For more refer this :Scanner class.
And your for each loop is correct and working.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k];//it was giving java.lang.ArrayIndexOutOfBoundsException
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);//not required
arr[i] =sc1.nextInt();//use arr[i] =sc.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
}
Above code will work. But you don't need new scanner objects for taking input, creating only single object will work.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k];
System.out.print("Enter array :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
sc.close();
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; i < arr.length; j++) {
arr[i] = arr[j];
count++;
}
System.out.println(arr[i] + " " + count);
}
}
}

Creating a loop for Scanner for arrays

I want to create a loop that asks for the Scanner to input each number one after the another for a certain amount in an array (I'm thinking of 10). Any suggestions?
import java.util.Scanner;
public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for (int i = 0; i < 9; i++)
{
array1[i] = a;
}
}
}
change to
for (int i = 0; i < 9; i++)
{
int a = sc.nextInt();
array1[i] = a;
}
or even
for (int i = 0; i < 9; i++)
{
array1[i] = sc.nextInt();
}
It's simple, you can just assign the value of the scanner object's input to the indices of the array:
import java.util.Scanner;
public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
// Where you had the original input
// int a = sc.nextInt();
for (int i = 0; i < 9; i++)
{
// Instead of array1[i] = a; you have
array1[i] = sc.nextInt();
}
}
}
Hope this helped!

Runtime Error on CodeChef KOL1506

When i compiled and ran this code on Eclipse, this runs fine but on Codechef it shows a runtime error. Can anyone help me find why it does not run on the website. The website link is: https://www.codechef.com/viewsolution/10987533
and the problem link is : https://www.codechef.com/problems/KOL1506/
import java.util.Scanner;
class SamosaBhai {
public static void main(String[] args){
int n =0;
Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.println("Enter number of test Cases:");
n = keyboard.nextInt();
int[] ans = new int[n];
for(int i = 0; i< n ;i++){
//no. of houses and power 'd' is stored here
String get = input.nextLine();
String[] numarray = get.split(" "); // splitting string by spaces
int num = Integer.parseInt(numarray[0]); // number of houses
int d = Integer.parseInt(numarray[1]); // power to be raised
// positions of the houses is stored here
String entry = input.nextLine();
Scanner scanner = new Scanner(entry);
int[] pos = new int[num];
for (int j= 0;j<num;j++) {
pos[j] = scanner.nextInt();
}
scanner.close();
ans[i] = postalCharge(pos, d, n);
}
keyboard.close();
input.close();
for(int p =0; p<n; p++)
System.out.println(ans[p]);
}
// function to calculate the postal charges between all the houses
public static int postalCharge(int[] location, int d, int n){
int total =0;
for (int i = 0; i<n; i++){
for(int j =0; j< n; j++){
int n1 = location[i];
int n2 = location[j];
total += Math.pow(Math.abs(n1-n2),d);
}
}
return total;
}
}

Java Compile Error, cannot find symbol

Getting the error "Cannot Fin Symbol", but I don't know what I am doing wrong.
import java.util.Scanner;
public class Exercise6_1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberStudents = input.nextInt();
int[] studentScores = new int[numberStudents];
System.out.print("Enter " + numberStudents + " Scores: ");
for (int i = 0; i < numberStudents; i++);{
studentScores[i] = input.nextInt();
}
}
}
You have semicolon after the "for" cycle.
Should look like this:
for (int i = 0; i < numberStudents; i++) {
studentScores[i] = input.nextInt();
}
you have an ; after the for loop.
Correct impl :-
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberStudents = input.nextInt();
int[] studentScores = new int[numberStudents];
System.out.print("Enter " + numberStudents + " Scores: ");
for (int i = 0; i < numberStudents; i++)
{
studentScores[i] = input.nextInt();
}
}
}
The last semicolon in the line
for (int i = 0; i < numberStudents; i++);{
should be removed:
for (int i = 0; i < numberStudents; i++) {
for (int i = 0; i < numberStudents; i++);{
studentScores[i] = input.nextInt();
}
Here You have ended the for loop with a semi-colon , which results in the termination of the loop at that point . That's why it shows it can't find symbol i , As it is out of the scope of the for loop.

Categories

Resources