Question

In: Computer Science

JAVA PROGRAMMING Is there a way I can use a method to place a user input...

JAVA PROGRAMMING

Is there a way I can use a method to place a user input variable into an array? Then call the same method to print the array? I'm new to Java programming I'm not sure how to continue. For example:


import java.util.Scanner;

public class PartayScanner {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter pokemon 1:");
String pokemon1 = scan.nextLine();

System.out.println("Enter pokemon 2:");
String pokemon2 = scan.nextLine();

System.out.println("Enter pokemon 3:");
String pokemon3 = scan.nextLine();

System.out.println("Enter pokemon 4:");
String pokemon4 = scan.nextLine();

System.out.println("Enter pokemon 5:");
String pokemon5 = scan.nextLine();

System.out.println("Enter pokemon 6:");
String pokemon6 = scan.nextLine();

System.out.println("Here are your pokemon!");
System.out.println(pokemon1);
System.out.println(pokemon2);
System.out.println(pokemon3);
System.out.println(pokemon4);
System.out.println(pokemon5);
System.out.println(pokemon6);


Solutions

Expert Solution

Hi, you cannot use a single method for once to take value and then to print the values. what you can do is you can do both of these work one after the other in a sequence. Like if you call a method it will ask user to input values and after taking all the values it will print all those values right after input from user. If you want to do both of these separately, you have to create two different methods, one for taking input and one for showing the output. Call them whenever you want.  

import java.util.Scanner;
    public class Test {
            void userIO{
                 int ar[];
                 Scanner scan = new Scanner(System.in);
                 for(int i=0; i<5; i++) {
                     System.out.print("Enter number " + (i+1) + ": ");
                     ar[i] = scan.nextInt();    // Taking user input
                 }
                 for(int i=0; i<5; i++) 
                     System.out.println("Number " + (i+1) + ": " + ar[i]);
            }
            public static void main(String[] args) {
                 userIO();      //Calling the method.
            }
   }

In above example, I have used only one method for both purposes.

Happy Coding.

PLEASE GIVE A THUMBSUP IF IT WAS HELPFULL AND LEAVE A COMMENT FOR ANY QUERY.

THANK YOU..


Related Solutions

Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
In Java, how can I convert a string user input for example a student starts a...
In Java, how can I convert a string user input for example a student starts a uni degree in "Winter/2022", to a date type, and then once I have done that, add 3 years to that value, and changing the season, so for example Student Started: Winter/2022 and Student Finished: Summer/2025. Is there any possible way to do this? Thank you.
JAVA - I am asking for the user to input their firstName and lastName but I...
JAVA - I am asking for the user to input their firstName and lastName but I want the method myMethod() to be able to print it when it is called. Is that possible? I'm new to Java and i'm not too sure what I should do to fix this. Also if I were to create a IF statement would I have to declare int amountDeposited; , int accountBalance; , int newBalance; in the new if statement. import java.util.Scanner; import java.util.Arrays;...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be...
Instructions JAVA PROGRAMMING 1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive). 2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked. 3. Take a year input from the user (should be between 1500 and 2019) 4. If the input year is a leap year, print that year is a leap year; otherwise, print that the...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT