I have the following input in 3 lines:
line 1: consists of positive int m and n separated by white space
line 2: list of m int separated by white space
line 3 and beyond: list of n words separated by white space or newline character
I am unable to read beyond m and n into my code
My Code:
Scanner input = new Scanner(System.in);
m = input.nextInt();
n = input.nextInt();
int[] lines = new int[m];
String[] words = new String[n];
input.nextLine();
for (int i = 0; i < m; i++) {
lines[i] = input.nextInt();
}
for (int i = 0; i < n; i++) {
words[i] = input.next();
}
How to read the line and words into the arrays
after i call the first nextLine(), and try to read the numbers, i get a nullpointerexception
I think below is what you are looking for. Made small changes to your program. You don't really need line input.nextLine(). Hope it helps
Example Input
3 2
9 5 2
Good Luck
PROGRAM
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n= input.nextInt();
int[] lines = new int[m];
for (int i = 0; i < m; i++) {
lines[i] = input.nextInt();
}
String[] words = new String[n];
for (int i = 0; i < n; i++) {
words[i] = input.next();
}
I had the user insert the input one line at at time here. I'm not sure what you were looking for but this is what I have:
Program:
Scanner input = new Scanner(System.in);
System.out.print("Enter line 1: ");
int m = input.nextInt();
int n = input.nextInt();
System.out.println(m + " ints and " + n + " words");
System.out.print("Enter line 2: ");
int[] lines = new int[m];
String[] words = new String[n];
input.nextLine();
for (int i = 0; i < m; i++) {
lines[i] = input.nextInt();
}
System.out.print("Enter line 3: ");
for (int i = 0; i < n; i++) {
words[i] = input.next();
}
System.out.println(Arrays.toString(lines));
System.out.println(Arrays.toString(words));
Console:
Enter line 1: 3 4
3 ints and 4 words
Enter line 2: 1 2 3
Enter line 3: four words right here
[1, 2, 3]
[four, words, right, here]
Related
I am new to Java. I have to show to terminal int coefficients from a 2D array.
I would like to have each value for the same seller in the same line.
There is a line break (due to Scanner ?). I have been looking for delimiter for system.in but I do not understand how to use it and if that is appropriate.
Please, may you help me ?
Thank you in advance
import java.util.Scanner;
public class Ventes {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the number of sellers ");
int nbrSellers = myObj.nextInt();
System.out.print("Enter the number of models ");
int nbrModels = myObj.nextInt();
int[][] sales = new int[nbrSellers][nbrModels];
for(int i = 1; i <= nbrSellers; i++) {
System.out.print("Seller " + i + " ");
for(int j = 0; j < nbrModels; j++) {
sales[i][j] = myObj.nextInt();
}
System.out.println();
}
}
}
Terminal result :
Enter the number of sellers 5
Enter the number of models 4
Seller 1 0
3
2
0
Final result in terminal
If I understand correctly, you wanna receive both inputs in the same line. If so, Daveid's comment is correct. you don't have to press enter so you can go with the following lines:
System.out.print("Enter the number of sellers and models (separated by a space): ");
int nbrSellers = myObj.nextInt();
int nbrModels = myObj.nextInt();
And just enter both numbers on the same line like so:
5 4
or you can use delimiters like this:
Scanner myObj = new Scanner(System.in);
System.out.print("Enter the number of sellers and models (separated by a comma): ");
String input = myObj.nextLine();
String[] splitValue = input.split(",");
int nbrSellers = Integer.parseInt(splitValue[0]);
int nbrModels = Integer.parseInt(splitValue[1]);
int[][] sales = new int[nbrSellers][nbrModels];
for(int i = 1; i <= nbrSellers; i++) {
System.out.print("Seller " + i + " ");
for(int j = 0; j < nbrModels; j++) {
sales[i][j] = myObj.nextInt();
}
System.out.println();
}
or (based on your comment below) if you have to use a for loop, use this:
for(int i = 0; i < nbrSellers; i++) {
System.out.print("Please enter " + nbrModels + " values for Seller " + (i + 1) + " (separated by a space): ");
for(int j = 0; j < nbrModels; j++) {
sales[i][j] = myObj.nextInt();
}
System.out.println();
}
and just input the numbers like so:
3 4 6 2 1
I have this as an input "1 2 3 4 5" and I want to have it like this
int[] numbers = new int[5];
number[0] = 1;
number[1] = 2;
number[2] = 3;
number[3] = 4;
number[4] = 5;
So how can I extract each number from a string and put it in an int array?
ConsoleIO io = new ConsoleIO();
int[] numbers = new int[5];
io.writeOutput("Type in 5 numbers");
String input = io.readInput();
// If input is longer than 1 character for example, "1 2 3 4 5"
if(input.length() > 1) {
System.out.println(input.length());
for(int y = 0; y < io.readInput().length(); y++) {
numbers[y] = Integer.parseInt(io.readInput().substring(y, io.readInput().indexOf(" ")));
}
return;
}
// If input is one number for example, "1"
else {
for(int i = 0; i < numbers.length; i++) {
numbers[i] = Integer.parseInt(io.readInput());
}
}
The else works, so if I enter one number and press enter and then the next one, it's all good. But if I have a sequence of numbers with a space in between ("1 2 3 4 5") the program just breaks.
String input = io.readInput();
int[] arr = new int[5];
if(input.length() >= 5){
String[] c = input.split(" ");
for(int i = 0; i < c.length(); i++){
arr[i] = Integer.parseInt(c[i]);
}
}
Why not use a Scanner and then split and parse?
Scanner in = new Scanner(System.in);
String[] nums = in.nextLine().split(" ");
for(int i = 0; i <=5; i++) {
numbers[i] = Integer.parseInt(nums[i]);
}
Assume your input is always numbers with a space between each other (or just one number), in Java 8 you can work in this way:
String[] splits = input.split(" ");
int[] result = Arrays.stream(splits).mapToInt(Integer::parseInt).toArray();
import java.util.*;
public class Solution {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
String[] nums = in.nextLine().split(" ");
int[] numbers = new int[5];
for(int i = 0; i <5; i++) {
numbers[i] = Integer.parseInt(nums[i]);
System.out.println(numbers[i]);
}
}
}
I would like to make a program where the user can input the number of variables and fill every variable with certain values. For example, the User inputs that he/she wants to make 10 arrays, then the User inputs that the first array should have 5 elements and the User fills that array with values, then the User wants the second array to have 4 elements and does the same and so on.
This is the code I was using, but it doesn't work:
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
for(int j = 0;j < i;j++){
int[] var = new int[j];
System.out.println("Enter the number of values: ");
int p = s.nextInt();
for(int q = 0;q < p;p++){
int n = s.nextInt();
var[q] = n;
}
}
}
And how could I compare these arrays that the user inputs?
The problem is that each time you are creating the array.
try this:
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){
System.out.println("Enter the number of values: ");
int p = s.nextInt();
var[j] = new int[p];
for(int q = 0;q < p;p++){
int n = s.nextInt();
var[j][q] = n;
}
}
Instead of creating a one dimensional array, you create a jagged array. Essentially, a 2d array is an array of arrays. that way the user inputs the number of arrays (i) and then continues to fill the arrays.
To check whether two collections have no commons values, you can use
Collections.disjoint();
For other operations, you can look here
This should work (with bidimensionnal array)
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){
System.out.println("Enter the number of values: ");
int p = s.nextInt();
var[j] = new int[p];
for(int q = 0;q < p;q++){
int n = s.nextInt();
var[j][q] = n;
}
}
}
You have to replace the incrementation in the second loop too ("q++" instead of "p++")
This should work and solve their first point
Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){
System.out.println("Enter the number of values: ");
int p = s.nextInt();
while (p>0)
{
var[j] = new int[p];
for(int q=0;q < p;q++){
System.out.println("Value number : " +(q+1) + " For Array Number "+ (j+1));
int n = s.nextInt();
var[j][q] = n;
}
p-=1;
}
}
I'm doing a Java activity that prints the Numbers the user Input, and here's my codes:
System.out.print("Enter How Many Inputs: ");
int num1 = Integer.parseInt(in.readLine());
for (int x = 1; x<=num1;x++){
for (int i = 0 ; i<num1;){
System.out.print("Enter Value #" + x++ +":");
int ctr1 =Integer.parseInt(in.readLine());
i++;
}
}
How can I print all the input numbers? Here's the result of my code:
Enter How Many Inputs: 5
Enter Value #1:22
Enter Value #2:1
Enter Value #3:3
Enter Value #4:5
Enter Value #5:6
How can I print all this numbers as array. 22,1,3,5,6
Create an int[] array of length num.
On every iteration take the user input and put it in the array at their specified index and break out of the while loop. and at last print the array elements by iterating over it.
Scanner scan = new Scanner(System.in);
System.out.println("enter num of values: ");
int [] arr = new int[scan.nextInt()];
for(int i=0;i<arr.length; i++) {
scan = new Scanner(System.in);
System.out.println("please enter a value: ");
while(scan.hasNextInt()){
int x = scan.nextInt()
;
arr[i]= x;
break;
}
}
for(int i:arr){
System.out.print(i);
}
OUTPUT:
enter num of values:
5
please enter a value:
22
please enter a value:
2
please enter a value:
3
please enter a value:
5
please enter a value:
6
22
2
3
5
6
System.out.print("Enter How Many Inputs: ");
int num1 = Integer.parseInt(in.readLine());
int arr[] = new int[num1];
for (int i = 0; i<num1; i++)
{
System.out.print("Enter Value #" + (i + 1) + ":");
arr[i] =Integer.parseInt(in.readLine());
}
I guess this should do it...
NOT TESTED
PRINTING
for(int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
SORTING
import java.util.Arrays;
Inside the code block
Arrays.sort(arr);
How do I take in input from a user from a scanner, then put that input into a 2D Array. This is what I have but I dont think it is right:
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int [][] a = new int[row][col];
Scanner in = new Scanner(System.in);
System.out.println("Enter a sequence of integers: ");
while (in.hasNextInt())
{
int a[][] = in.nextInt();
a [row][col] = temp;
temp = scan.nextInt();
}
Square.check(temp);
}
What I am trying to do is create a 2D array and create a magic Square. I have the boolean part figured out, I just need help with inputting users sequence of numbers into the array so the boolean methods can test the numbers. All help greatly appreciated
I don't believe your code will work how you want it to. If I'm understanding your question correctly, here's what I would do:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [][] a = new int[row][col];
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print("Enter integer for row " + i + " col " + j + ": ");
a[i][j] = in.nextInt();
}
}
// Create your square here with the array
}
In the loops, i is the current row number and j is the current column number. It will ask the user for every row/column combination.
You can use that in order to enter all number at the same time :
int [][] a = new int[3][3];
Scanner in = new Scanner(System.in);
System.out.println("Enter a sequence of integers: ");
int row=0,col=0;
while (in.hasNextInt())
{
a [row][col++] = in.nextInt();
if(col>=3){
col=0;
row++;
}
if(row>=3)break;
}
Then you can enter :
1 2 3 4 5 6 7 8 9
to fill your array.