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

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();
}
}

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();
}
}

Having trouble searching array

So I have everything working fine up until the point in which I need to search. I'm really new to this so my code is probably awful I'm sorry in advance. Anyway, its a user input array, and the user should be able to search for a number in an array. Im getting the error for a duplicate variable on line 50 (int i, get 1).
import java.util.Scanner;
class SearchingSorting {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println ("How many numbers would you like to input?");
int num = input.nextInt();
double[] array = new double[num];
for (int i = 0; i < num; i++) {
System.out.println ("Input number " + (1 + i) + ":");
array[i] = input.nextDouble();
}
for (double temp1 : array){
System.out.print (temp1 + "\t");
}
input.close();
int pass;
int i;
int hold;
for(pass = 1; pass < array.length; pass++)
{
for(i = 0; i < array.length - 1; i++)
{
if(array[i] > array[i+1])
{
hold = (int) array[i];
array[i] = array[i+1];
array[i+1] = hold;
}
}
System.out.println("\nSorted number is: ");
for(i = 0; i < array.length; i++)
System.out.print(" " + array[i]);
}
int i, get1;
Scanner keyboard = new Scanner(System.in);
int[] numbers = new int[10];
for(i = 0; i < numbers.length; i++)
{
numbers[i] = i * 10;
}
System.out.print("Enter search number: ");
get1 = keyboard.nextInt();
SearchMethod(numbers, get1);
}
public static void SearchMethod(int[] num, int get2)
{
int i ;
boolean j = false;
for(i = 0; i < num.length; i++)
{
if(num[i] == get2)
{
j = true;
break;
}
}
if(j == true)
System.out.println(get2 + " is found at num[" + i + "]");
else
System.out.println(get2 + " is not found in an array");
}
}
You are trying to declare a new variable with the same name ("i") in the same scope.
Rename your variable i on line 50.

Array is not getting printed in 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
}
}
}

Matching Array Numbers By Column

I have been asked to implement a lottery program. The code is as follows..
Scanner keyIn = new Scanner(System.in);
Random randomGenerator = new Random();
int numDigits = 6, randomNum;
int[] userNum = new int[numDigits];
int[] lotteryNumbers = new int[numDigits];
for (int i = 0; i < 6; i++) {
randomNum = (int) (Math.random() * 45);
for (int x = 0; x < i; x++) {
if (lotteryNumbers[x] == randomNum || lotteryNumbers[x] == 0) {
randomNum = (int) (Math.random() * 45);
x = -1;
}
}
lotteryNumbers[i] = randomNum;
}
for (int i = 0; i < userNum.length; i++) {
System.out.print("Enter your numbers: ");
userNum[i] = keyIn.nextInt();
System.out.println("Your number is: " + userNum[i]);
System.out.println(" ");
}
keyIn.close();
System.out.println("You've entered the following numbers...");
System.out.println(Arrays.toString(userNum));
System.out.println(" ");
System.out.println("The lottery numbers are: ");
for (int i = 0; i < 6; i++) {
Arrays.sort(lotteryNumbers);
System.out.print(+lotteryNumbers[i] + " ");
}
I am trying to implement the matching part of the question but I ran into some issues, it is asking me to look for matches in the columns rather than rows.
I need to know how to check if the numbers match in the column.
My attempt at it.
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++)
{
if(lotteryNumbers[i] == userNum[j])
{
match++;
}
}
}
System.out.println(" ");
System.out.print("Matches: " +match +" found.");
But this looks for matches in the rows, I would like to make it so it looks for matches by column
for(int i=0;i<6;i++){
for(int j=i;j<6;j++){
if(userNum[i]==lotterNumbers[j]){
System.out.println("match found: "+userNum[i]);
}
}
}
this code looks if you have matching numbers in user input and random number and if so prints match found + number
array as row: 1|2|3|4|5|6|7
array as column:
1
2
3
4
5
6
if you look at an array like a row you match by columns otherwise by row
List<Integer> list = IntStream.range(1, 5001).boxed().collect(Collectors.toList());
Collections.shuffle(list);
System.out.println(list.subList(0, 6));
The best thing you can do is to use HashMap.
First insert as <column number, value in that column> for 1st row.
Then use get() to get the value stored in that column. Then simply check 2nd row value with what you have got from get().
Code:
import java.util.HashMap;
import java.util.Map;
public class Main
{
public static void main(String[] args)
{
int matched = 0,num;
Map<Integer,Integer> hm = new HashMap<Integer,Integer>();
int[] numbersDrawn = {1,3,4,17,23,45};
int[] yourNumbers = {1,2,3,19,23,41};
for(int i = 0; i<numbersDrawn.length ; i++)
{
hm.put(i,numbersDrawn[i]);
}
for(int i=0 ; i<yourNumbers.length ; i++)
{
num = hm.get(i);
if(num == yourNumbers[i])
{
matched++;
}
}
System.out.println(matched);
}
}
Do not believe the users. They can key in duplicate entries.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;
public class Test {
public static void main(String args[]) {
Scanner keyIn = new Scanner(System.in);
Random randomGenerator = new Random();
int numDigits = 6, randomNum;
TreeSet<Integer> usernum = new TreeSet<>();
int[] lotteryNumbers = new int[numDigits];
ArrayList<Integer> matches = new ArrayList<>();
for(int i = 0;i < 6; i++){
randomNum = (int)(Math.random() * 45);
for(int x = 0; x < i; x++){
if(lotteryNumbers[x] == randomNum || lotteryNumbers[x] == 0)
{
randomNum = (int)(Math.random() * 45);
x =-1;
}
}
lotteryNumbers[i] = randomNum;
}
Arrays.sort(lotteryNumbers);
System.out.println("Enter your numbers: ");
for(int i = 0; i < numDigits; i++){
boolean wasAdded = usernum.add(keyIn.nextInt());
if(wasAdded == false) {
System.out.println("Duplicate entries are disallowed!");
i--;
}
}
keyIn.close();
System.out.print("You've entered the following numbers : ");
System.out.println(usernum.toString().replaceAll("\\[|\\]|,", ""));
System.out.println();
System.out.print("The lottery numbers are: ");
for(int i = 0; i < 6; i++){
System.out.print(lotteryNumbers[i] +" ");
}
int iteration = 0;
for (int entry : usernum) {
if(entry == lotteryNumbers[iteration]) {
matches.add(entry);
}
iteration++;
}
System.out.println("\n");
System.out.println("Numbers matched : " + matches.size());
}
}

How to make Bubble Sort in Java to output the sorted numbers?

This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted.
I'm not sure what I have to do to make them sort.
Any advice or suggestions would be helpful.
package sortingalgorithm2;
import java.util.Scanner;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
Scanner read = new Scanner (System.in);
int[] num = new int[15];
int size = 15;
System.out.println("Enter 15 numbers: ");
for (int i=0; i <= size-1; i++)
{
num[i] = read.nextInt();
}
for (int i=0; i <= size-1; i++)
{
if (num[i] >=1 && num[i] <= 1000)
{
System.out.println("The numbers you entered are: ");
System.out.println(+num[0]);
System.out.println(+num[1]);
System.out.println(+num[2]);
System.out.println(+num[3]);
System.out.println(+num[4]);
System.out.println(+num[5]);
System.out.println(+num[6]);
System.out.println(+num[7]);
System.out.println(+num[8]);
System.out.println(+num[9]);
System.out.println(+num[10]);
System.out.println(+num[11]);
System.out.println(+num[12]);
System.out.println(+num[13]);
System.out.println(+num[14]);
}
else
{
System.out.println("Data input is invalid. Enter a number between "
+
"1 and 1000.");
break;
}
}
BubbleSort (num);
for (int i=0; i < num.length; i++)
{
System.out.println("The sorted numbers are: ");
System.out.print(num[i]+ " ");
}
}
private static void BubbleSort(int[] num)
{
for (int i=0; i <= num.length; i++)
for (int x=1; x <= num.length; x++)
if (num[x] > num[x+1])
{
int temp = num[x];
num[x] = num[x+1];
num[x+1] = temp;
}
}
}
Try this Bubble sort :
private static void BubbleSort(int[] num) {
for (int i = 0; i < num.length; i++) {
for (int x = 1; x < num.length - i; x++) {
if (num[x - 1] > num[x]) {
int temp = num[x - 1];
num[x - 1] = num[x];
num[x] = temp;
}
}
}
}
You are printing the actual numbers in the order the user entered. Try this instead:
int[] sortedNumbers = new int[15];
sortedNumbers = BubbleSort (num);
for (int i=0; i < sortedNumbers.length; i++)
{
System.out.println("The sorted numbers are: ");
System.out.print(sortedNumbers[i]+ " ");
}
public static int[] BubbleSort(int [] num)
{
int temp;
for (int i=1; i<num.length; i++)
{
for(int j=0; j<num.length-i; j++)
{
if (num[j] > num [j+1])
{
temp = num [j];
num [j] = num [j+1];
num [j+1] = temp;
}
}
}
return num;
}
Try this :
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
num[i] = num[i] + num[j] - (num[j] = num[i]);
}
}
}
you are passing the array variable num (which is not static) to BubbleSort()(which does not returns a value and shadows the global num variable with its own) and trying to use the same num variable to access your sorted array from your main method which is not right.
The genuine fix to this is to declare your variable num as static just before the main method( in the class declaration). So I have made the changes in the program and here is the solution.
import java.util.Scanner;
public class sol {
static int num [] =new int [15]; //declaring num as static in the class definition.
public static void main(String[] args)
{
Scanner read = new Scanner (System.in);
int size = 15;
System.out.println("Enter 15 numbers: ");
for (int i=0; i <= size-1; i++)
{
num[i] = read.nextInt();
}
read.close();
/*for (int i=0; i <= size-1; i++)
{
if (num[i] >=1 && num[i] <= 1000)
{
System.out.println("The numbers you entered are: ");
System.out.println(+num[0]);
System.out.println(+num[1]);
System.out.println(+num[2]);
System.out.println(+num[3]);
System.out.println(+num[4]);
System.out.println(+num[5]);
System.out.println(+num[6]);
System.out.println(+num[7]);
System.out.println(+num[8]);
System.out.println(+num[9]);
System.out.println(+num[10]);
System.out.println(+num[11]);
System.out.println(+num[12]);
System.out.println(+num[13]);
System.out.println(+num[14]);
}
else
{
System.out.println("Data input is invalid. Enter a number between "
+
"1 and 1000.");
break;
}
}*/ //I have disabled this just to check with the sort method.
BubbleSort ();//no need to pass the array as it is static and declared as a //class variable hence can be used to by all the methods of that class
System.out.println("The sorted numbers are: ");
for (int i=0; i < num.length; i++)
{
System.out.print(num[i]+ " ");
}
}
private static void BubbleSort()
{
for (int i=0; i < num.length; i++)// required changes in the looping
for (int x=0; x < num.length-i-1; x++)
if (num[x] > num[x+1])
{
int temp = num[x];
num[x] = num[x+1];
num[x+1] = temp;
}
}
}

Categories

Resources