Asking for multiple user inputs then printing them below separetely [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to make a script that asks for multiple inputs then prints them out below.
For example:
How many inputs do you want? -> 3
What is the 1. input? -> 5
What is the 2. input? -> 3
What is the 3. input? -> 4
You gave these inputs:
1) 5
2) 3
3) 4
Below you can see how far I got with it. Asks for all the inputs nicely but I can't figure out how to print them out below the input.
import java.util.Scanner;
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
for (int i=1;i<=inp;i++){
System.out.print("What was " + i + ". input? ");
int print=read.nextInt();
System.out.println(print);
}
}
}

public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
int[] answers = new int[inp];
for (int i=1;i<=inp;i++){
System.out.print("What was " + i + ". input? ");
int print=read.nextInt();
System.out.println(print);
answers[i] = print;
}
int index = 0;
for(int a : answers){
System.out.println( index + ")" + " " + a);
index ++;
}
}
}
You can code clean it yourself as a practice =)

You can use Collections (i used List) to protect your inputs;
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
List<Integer> numbers = new ArrayList<Integer>();
for (int i=1;i<=inp;i++){
System.out.print("What was " + i + ". input? ");
int print=read.nextInt();
numbers.add(print);
System.out.println(print);
}
System.out.println("Inputs are :");
for (Integer integer : numbers) {
System.out.println(integer);
}
}
}
And the output is;
How many inputs?
3
What was 1. input? 5
5
What was 2. input? 3
3
What was 3. input? 4
4
Inputs are :
5
3
4

You need to create an array of integers for later use.
int array = new int[inp];

You need to store the inputs and print them later
import java.util.Scanner;
public class Inputs {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp=read.nextInt();
int[] keepInputsHere=new int[inp];
for (int i=0;i<inp;i++){
System.out.print("What was " + i + ". input? ");
int readInt=read.nextInt();
keepInputsHere[i]=readInt;
}
System.out.print("Print results: ");
for (int i=0;i<inp;i++){
System.out.print( keepInputsHere[i]);
}
}
}

Dont forget to close the Scanner and separate the login into different functions:
public class Inputs {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("How many inputs?");
int inp = read.nextInt();
Print(ReadInput(inp,read));
}
private static int[] ReadInput(int questionsNo, Scanner sc) {
int[] inputs = new int[questionsNo];
for (int i = 0; i < questionsNo; i++) {
System.out.print("What was " + i + ". input? ");
int readInt = sc.nextInt();
inputs[i] = readInt;
}
sc.close();
return inputs;
}
private static void Print(int[] inputs) {
System.out.println("Print results: ");
for (int i = 0; i < inputs.length; i++) {
System.out.println( i + ")" + " " + inputs[i]);
}
}
}

Related

Write a program that prints out every line of input exactly as they were entered

Non-CS major here taking my first programming class for fun. As the title states, my first assignment is "Write a program that prints out every line of input exactly as they were entered." My program right now will accept one input, but not any others. How can I fix this? Thank you so much :)
import java.util.Scanner;
public class echohw {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ans;
ans = in.nextLine();
System.out.print(ans);
Like Scary Wombat said, use a while loop:
import java.util.Scanner;
public class echohw {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ans;
// Continue printing user input unless "stop" is entered
while (!(ans = in.nextLine()).equals("stop"))
System.out.println(ans);
}
}
You can use array function to store multiple user inputs. In this example, I try to make it simple so you can understand it well. If you have any other doubt, you can ask me here.
import java.util.Scanner;
public class echohw {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
//Decide number of inputs
System.out.println("How many inputs you want to enter: ");
int numInput = Integer.parseInt(scan.nextLine());
//Store inputs
String aryInput[] = new String[numInput];
for (int i = 0; i < aryInput.length; i++) {
System.out.println("Enter the input " + (i+1) + " : ");
aryInput[i] = scan.nextLine();
}
//Print inputs
for (int i = 0; i < aryInput.length; i++) {
System.out.println("Input " + (i+1) + " : ");
System.out.println(aryInput[i] + "\n");
}
}
}

Create a program that will continuously accept number when user does not input same number as the previous inputted number [duplicate]

This question already has answers here:
Need To Take Input To Array Until User Enters 0 JAVA
(3 answers)
Closed 5 years ago.
This is the only program that i can't create. Please help me with this.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j=0;
do {
System.out.print("Enter number: ");
i = input.nextInt();
i=j;
}
while (i == j);
System.out.println("You have inputted the same number on the previous.");
}
Cheers m8, good luck at your test :D
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = 0, j = 999999, input = 0;
do {
i = j;
System.out.print("Enter number: ");
input = input.nextInt();
j=input;
} while (i != j);
System.out.println("You have inputted the same number on the previous.");
}

Java User Input array is only capturing 3 integers instead of five

This code is supposed to capture 5 user integers, print them out, then print them in reverse. It is capturing the first int only, and printing it 3 times, then printing the first integer again 5 more times without reversing. Test ends with "Process finished with exit code 0" which I think is says the program finished without errors -- which of course is not correct. I assume the issue is in how the user input array is stored. I have it assigning as userNum[i] with a limited array of 5, and int i =0 to begin array storage at userNum[0], so I'm not clear on why all the inputs are not captured up to userNum[4].
Thank you for any insight you can provide. I am very new to java and this is prework for my java class.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[i]);
++j;
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[i] + " ");
}
}
}
You need to work more about on for loops and study how to iterate values in for loop, the problem in your i,j variables.
Here I fix your code.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
//for 5 inputs you need loop
for(;i<NUM_VALS;i++){
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
}
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[j]);
//++j; //no need to increment as you already did in for loop
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[j] + " ");// userNum[0] = your last value which you reverse
}
}
}
Here is a solution using the collections framework:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final List<Integer> numbers = new ArrayList<>();
System.out.println("Enter any number of integers. (whitespace delimited. enter a non-integer to quit.): ");
while (scnr.hasNextBigInteger()) {
final int n = scnr.nextInt();
System.out.println("Parsed: " + n);
numbers.add(n);
}
System.out.println("Done reading user input.");
System.out.println("Your input: " + numbers);
Collections.reverse(numbers);
System.out.println("Your input reversed: " + numbers);
}
}
I have provided you with a solution. This is a clean way of doing it.
nextInt() reads the next integer that the user inputs. Notice that this will throw a InputMismatchExceptionif the user does not input a integer as value.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> input = new ArrayList<Integer>();
//Simple loop that will read 5 user inputs
//and add them to the input list
for(int i = 0; i < 5; i++){
input.add(scanner.nextInt());
}
//print the 5 values
for(Integer val : input){
System.out.println(val);
}
//reverse the 5 values
Collections.reverse(input);
//print the 5 values again, but they are now reversed
for(Integer val : input){
System.out.println(val);
}
}

How to limit the user from typing in more than three integers into the console?

I want the user to only be able to type in no more than three number into the console. Is there any way to limit or stop the console from accepting keyboard input?
Thanks
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Type three integers ");
int firstInt = userInput.nextInt();
int secondInt = userInput.nextInt();
int thirdInt = userInput.nextInt();
System.out.println(firstInt + secondInt + thirdInt);
}
}
int count = 3;
Scanner z = new Scanner(System.in);
List<Integer> l = new ArrayList<>();
while(count>0)
{
int n = z.nextInt();
l.add(n);
count--;
}
Runtime.getRuntime().exit(0);
Runtime.getRuntime().exit(0) will allow code to skip next lines in code.
give it a try
Hope this is the solution you are looking for

Why variable outside for loop doesn't work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a for loop which stores results in an int array and from these results I need to be able to search for eg. how many 1 are in the array so I declared an int variable outside the loop but it keeps saying that my array hasn't been initialized. Could you guys help me thanks.
import java.util.Scanner;
import java.util.Arrays;
class TestDie {
public static void main (String [] args)
{
Die firstDie = new Die();
int[] playerOneResults;
firstDie.roll();
System.out.println(firstDie.getFaceValue());
Scanner userInput = new Scanner(System.in);
System.out.println("PLease enter the name of player one");
String playerOneName = userInput.next();
System.out.println("Please enter the name of player two");
String playerTwoName = userInput.next();
System.out.println("Enter the number of dice to be thrown");
int numDice = userInput.nextInt();
System.out.println("First player's name: " + playerOneName);
System.out.println("Second player's name: " + playerTwoName);
System.out.println("Number of dice will be thrown: " + numDice);
for(int counter = 0; counter != numDice; counter++)
{
playerOneResults = new int[numDice];
firstDie.roll();
playerOneResults[counter] = firstDie.getFaceValue();
System.out.println("Player one results: " + playerOneResults[counter]);
}
Arrays.sort(playerOneResults);
int c = Arrays.binarySearch(playerOneResults, 1);
System.out.println(c);
}
}
Try this code
public static void main(String args[]) {
Die firstDie = new Die();
int[] playerOneResults = null;
firstDie.roll();
System.out.println(firstDie.getFaceValue());
Scanner userInput = new Scanner(System.in);
System.out.println("PLease enter the name of player one");
String playerOneName = userInput.next();
System.out.println("Please enter the name of player two");
String playerTwoName = userInput.next();
System.out.println("Enter the number of dice to be thrown");
int numDice = userInput.nextInt();
System.out.println("First player's name: " + playerOneName);
System.out.println("Second player's name: " + playerTwoName);
System.out.println("Number of dice will be thrown: " + numDice);
playerOneResults = new int[numDice];
for (int counter = 0; counter != numDice; counter++) {
firstDie.roll();
playerOneResults[counter] = firstDie.getFaceValue();
System.out.println("Player one results: " + playerOneResults[counter]);
}
Arrays.sort(playerOneResults);
int position=0;
while(position<0){
int c = Arrays.binarySearch(playerOneResults, position,playerOneResults.length-1, 1);
position=c;
System.out.println(c);
}
}

Categories