How would I read a single "line" of input from the user that seperates the values by space and assign them to a certain variable? Seems quite confusing for me.
For example,
Enter 3 digits = 3 4 5
Total = 12
The input that I put, once there is a space, only prints first value when I put "3 4"
I know I should not use nextInt scanner but I can't figure out a way
int a;
Scanner scan = new Scanner(System.in);
System.out.println("Enter 3 digits:");
a = scan.nextInt();
System.out.println(a);
By doing sum in for loop:
Scanner scan = new Scanner(System.in);
System.out.println("Enter 3 digits:");
int sum=0;
for(int i=0;i<3;i++){
sum = sum + scan.nextInt();
}
System.out.println(sum);
It would be best to have an array of variables, and read integers into the array one at a time like this:
int[] a = new int[3];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 3 digits: ");
for(int i = 0 ; i < a.length ; i++) //loop 3 times
{
a[i] = scan.nextInt(); //get int from input
System.out.println(a[i]);
}
scan.nextLine(); //consume the \n
You just call nextInt() 2 more times.
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
If you are required to read all integers in one read you can use this code. If reading one by one is ok the refer to other answers. For this code you can make a loop and use arrays as well.
String a;
Scanner scan = new Scanner(System.in);
System.out.println("Enter 3 digits:");
a = scan.nextLine();
Scanner stscan = new Scanner(a);
System.out.println(a);
int number1 = stscan.nextInt();
int number2 = stscan.nextInt();
int number3 = stscan.nextInt();
System.out.println(number1+number2+number3);
Related
Here is my code:
ArrayList<String> arr= new ArrayList();
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int temp= sc.nextInt();
System.out.println("Enter the elements in the array");
String num;
for(int i=0; i<temp; i++) {
num= sc.nextLine();
arr.add(num);
//System.out.println("i= "+ i +"temp= "+temp);
}
The problem is that if i want to add 5 elements it is taking the first element as null and only letting me to input rest 4 elements but i want to enter all the 5 numbers. Where is the problem?
ArrayList<String> arr= new ArrayList<>();
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int temp= sc.nextInt();
System.out.println("Enter the elements in the array");
String num;
sc.nextLine(); //add this it should work !
for(int i=0; i<temp; i++) {
num= sc.nextLine();
arr.add(num);
//System.out.println("i= "+ i +"temp= "+temp);
}
Reason --
"You need to move the sc pointer to the next line after taking the first input, your pointer is looking for the first integer on the same line instead of the one below it"
In this programming assignment, I need to use bit operators on two numbers that the user gives. First of I need to get inputs a and b from a single input. Here is what I am using for it:
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a and b numbers between the "
+ "interval [-128,127] (-1 -1 to exit): ");
byte[] userInput = new byte[2];
for(byte i = 0; i < userInput.length; i++) {
userInput[i] = stdin.nextByte();
}
I can't find a way to compare the two numbers that are given. Here is what the output is somewhat suppose to look like:
enter a and b numbers in the interval [-128,127] (-1 -1 to exit): 59 18
How do I assign the two input numbers to a and b so I can later use them for my bit wise operators?
Try this:
Scanner stdin = new Scanner(System.in);
System.out.print("Enter a and b numbers between the interval [-128,127] (-1 -1 to exit): ");
String inputString = stdin.nextLine();
String[] inputArray = inputString.split(" ");
byte a = Byte.parseByte(inputArray[0]);
byte b = Byte.parseByte(inputArray[1]);
As Pavneet Singh mentions
a=userInput[0]
b=userInput[1]
should work. Or to avoid using an array you could do:
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a and b numbers between the "
+ "interval [-128,127] (-1 -1 to exit): ");
byte a = stdin.nextByte();
byte b = stdin.nextByte();
}
I am a beginner in Java programming. I am trying to write a simple program to take size of input followed by list of numbers separated by spaces to compute the sum.
The first input is getting in fine for the second one system shows error as it is trying to parse a blank string into integer. Can you please help with the mistake I am making?
import java.util.Scanner;
public class InputStringforarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(" Enter size of input ");
int num = scan.nextInt();
System.out.println("Enter data separated by spaces: ");
String line = scan.nextLine();
String[] str = line.split(" ");
int[] A = new int[num];
int sum = 0;
for (int i = 0; i < num; i++)
A[i] =Integer.parseInt(str[i]);
for (int i = 0; i < num; i++)
sum = sum + A[i];
System.out.println("Sum is " + sum);
}
}
The reason you get an exception in your code is because int num = scan.nextInt(); does not process the newline character after the number.
So when the statement String line = scan.nextLine(); is used, it processes the newline character and hence you get an empty string ""
You can either fetch the entire line and parse it to Integer, like this:
int num = Integer.parseInt(scan.nextLine());
or you can go with using nextInt() and then use a blank scan.nextLine() to process the new line after the number, like this:
int num = scan.nextInt();
scan.nextLine();
Your Program has only one error that you were making only one scan object of scanner class, you have to make two scanner class object one will help in getting array size while another will help in getting array element.
import java.util.Scanner;
public class InputStringforarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in); // change 1
System.out.print(" Enter size of input ");
int num = scan.nextInt();`enter code here`
System.out.println("Enter data separated by spaces: ");
String line = scan1.nextLine();// change 2
String[] str = line.split(" ");
int[] A = new int[num];
int sum = 0;
for (int i = 0; i < num; i++)
A[i] =Integer.parseInt(str[i]);
for (int i = 0; i < num; i++)
sum = sum + A[i];
System.out.println("Sum is " + sum);
}
}
I don't have any code to show what I am trying to do, because I honestly have no idea how to approach this.
What I am trying to do is have the user input how many numbers will be into an array. So basically, this is what I would like to be the output:
How many students in the class? 3
Enter the marks:
76
68
83
The class average is 75.67 %
I can program everything else, except for the first line. I have no idea how to read in a number into an array, so that the array will be as big as that number.
Thank you.
To start you will need to set up your scanner to read console input:
Scanner scanner = new Scanner(System.in);
You can then use function such as scanner.next() or scanner.nextInt() to get the input from the console. Hopefully this gives you some idea of where to start. If you need to look up more Scanner functions check the Scanner Documentation
As far as arrays go you simply need to save the first int input to the console and use this for size:
int size = Integer.parseInt(scanner.next());
int[] array = new int[size];
Then you should be able to use a loop to save each individual score.
import java.util.Scanner;
public class TestArrayInputViaConsole {
public static void main(String args[]) {
double sum = 0;
double avg = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter how many students are in the class? ");
int ArraySize = sc.nextInt();
int[] marks = new int[ArraySize];
System.out.println("Enter the marks:");
for(int i = 0; i < ArraySize; i++) {
marks[i] = sc.nextInt();
sum = sum + marks[i];
}
avg = (sum / ArraySize);
System.out.println("Average of the class : " + avg);
}
}
does anyone know how to set a user input for an array, I cant find the command anywhere. my array 'grades' have 20 locations. im not so sure about 'grades.length' function but I think it prompts 20 times. BUT I added a while statement to override BUT ITS TOTALLY IGNORING THE FOR STATEMENT. if I could set user input for array I could get rid of the while statement...
program has to accept grade for number of students the user inputs btw..
import java.util.Scanner;
public class gradesaverage {
public static void main(String[] args) {
int [] grades = new int [20];
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
for ( i = 1; i <= grades.length; ++i)
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
while(i <= numStudents );
}
}
Not sure what you mean, but assuming all input is correct,
int [] grades = new int [numStudents ];
Should work if you move this line after declaration and assignment of numStudents. There is no problem in java with variable length arrays.
Also note - your iterator i starts from 1, while in java arrays start from 0.
public static void main(String[] args) {
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
int [] grades = new int [numStudents]; //the size we wanted
for ( i = 0; i < grades.length; ++i) //starting from 0, not 1.
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
//print the array - for checking out everyting is ok
System.out.println(Arrays.toString(grades));
}