How to store user input into an existing Array?
I java arrays are immutable, meaning you can't change the value once set. I have built a function that can acieve what you want:
public static int[] appendToArray(int[] array, int value) {
int[] result = Arrays.copyOf(array, array.length + 1);
result[result.length - 1] = value;
return result;
}
//--------------------------------------------
Int newId = kbd.nextInt();
id[] = appendToArray(id, newId);
JAVA - how do I save user inputs into array with a method that has no parameters?
You can declare your array in the class, and make it static
;
static int[] guessesArray;
Then, in you main method, initialize it.
And from you makeGuess()
method, I can guess that you need to store it in an array x
. Just change it to array guessesArray
.
storing user info to an array for use in a switch statment
As others have stated you've given your array too narrow (small) of a scope.
Imagine that you live with 4 people. Each of you have your own room and you all share a common space -- the kitchen. Inside the kitchen is a refrigerator that contains food. None of you are allowed to go into each other's rooms. Roommate #1, decides that the refrigerator will go in his room. Now no one else has access to the food in the refrigerator, because the other roommates aren't allowed to enter his room. In essence, that's what has occurred when you placed the array declaration inside case #1. For each roommate to have access the refrigerator needs to be in a common place -- the kitchen. When you use "{" and "}" with statements such as "if", "for", "switch-case", etc..., you are defining a scope (ie: a room). It's good to only give as much access as is needed, but at the same time, ensure that everything that needs access to a variable (such as an array) has access to it.
Have you been introduced/taught about writing pseudo-code? Write down the steps that you need to do. Use both normal words and C# when appropriate -- using proper C# syntax isn't necessary when writing pseudo-code, but can sometimes be helpful. Initially, start high-level. Then revise it until you've broken the steps down far enough that you are able to start programming. You decide what the detail is, and how much detail is necessary.
Here's a pseudo-code example:
Revision #1:
Inform user what the program does (Console.WriteLine).
Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
1. Add New T-shirt Details
2. Edit Exisiting T-shirt detials
3. Display All T-shirts in store
4. Delete T-shirt information
5. Exit
Get user response (Console.ReadLine)
if userResponse = 1 then add new t-shirt details
else if userResponse = 2 then edit existing t-shirt details
else if userResponse = 3 then display all t-shirts in store
else if userResponse = 4 then delete t-shirt information
else if userResponse = 5 then exit the program
else user selected an option that doesn't exist
Now that we've got an overall idea what needs to happen, we can add more detail to the parts that aren't quite clear.
Revision #2:
Inform user what the program does (Console.WriteLine).
Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
1. Add New T-shirt Details
2. Edit Exisiting T-shirt details
3. Display All T-shirts in store
4. Delete T-shirt information
5. Exit
Get user response (Console.ReadLine)
Save user response in integer variable (name: option)
After looking at the 5 options I notice that I'm going to ask the user for more information (t-shirt details). How am I going to store the t-shirt details? I'll store them in a string variable. I need to store details for multiple t-shirts. What data type(s) allow me to store multiple values? I'll use an array -- a string array. How many of the 5 options need access to the t-shirt details? More than one, so I'll make sure they all have access to this array. How many t-shirts will the user enter details for? I'm not sure--the user can enter as many as he/she wants to. I'll start with 1 and resize the array as necessary.
string[] tshirtDetails = new string[1];
Let's continue adding more detail. We'll start by adding more detail for option #1.
I need to get t-shirt details. How can I let the user know that?
if userResponse = 1 then add new t-shirt details
{
prompt user for t-shirt details (Console.WriteLine)
get t-shirt details from user (Console.ReadLine)
store t-shirt details in array (tshirtDetails)
}
Let's continue for option #2. I need to let the user edit existing t-shirt details. How will he/she know what t-shirts exist? Should I display the t-shirt details when this option is selected or should the user display the existing t-shirts prior to choosing this option? The user can use option #3 to display the existing t-shirt info if they need to.
else if userResponse = 2 then edit existing t-shirt details
{
prompt user for which t-shirt details to modify (Console.WriteLine)
get t-shirt index from user (Console.ReadLine)
update t-shirt details in array (tshirtDetails)
}
Next, we'll add more detail for option #3. Since we're using an array we'll have to get multiple values from the variable. We'll use a loop. "for" (or "foreach") should work.
else if userResponse = 3 then display all t-shirts in store
{
for each detail in tshirtDetails Console.WriteLine
}
Moving on to option #4. Which t-shirt detail do I need to delete? I'll need to ask the user.
else if userResponse = 4 then delete t-shirt information
{
prompt user for which t-shirt details to modify (Console.WriteLine)
get t-shirt index from user (Console.ReadLine)
remove t-shirt details from array (tshirtDetails)
}
Option #5. This one is self-explanatory, so I don't need to add any more detail here.
else if userResponse = 5 then exit the program
What if user enters an invalid option? I'll inform them and then re-prompt for a valid option.
else
inform user of invalid option (Console.WriteLine)
Revision #3:
Inform user what the program does (Console.WriteLine).
Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
1. Add New T-shirt Details
2. Edit Exisiting T-shirt details
3. Display All T-shirts in store
4. Delete T-shirt information
5. Exit
Get user response (Console.ReadLine)
Save user response in integer variable (name: option)
string[] tshirtDetails = new string[1];
if userResponse = 1 then add new t-shirt details
{
prompt user for t-shirt details (Console.WriteLine)
get t-shirt details from user (Console.ReadLine)
store t-shirt details in array (tshirtDetails)
}
"Store t-shirt details in array". My array size is 1, what happens if the user enters details for three t-shirts? I better resize the array, before I add each t-shirt detail to the array. Let's add some pseudo-code for option #1 before "store t-shirt details in array (tshirtDetails)"
if userResponse = 1 then add new t-shirt details
{
prompt user for t-shirt details (Console.WriteLine)
get t-shirt details from user (Console.ReadLine)
resize array (tshirtDetails)
store t-shirt details in array (tshirtDetails)
}
We continue looking through the pseudo-code:
else if userResponse = 2 then edit existing t-shirt details
{
prompt user for which t-shirt details to modify (Console.WriteLine)
get t-shirt index from user (Console.ReadLine)
update t-shirt details in array (tshirtDetails)
}
else if userResponse = 3 then display all t-shirts in store
{
for each detail in tshirtDetails Console.WriteLine
}
else if userResponse = 4 then delete t-shirt information
{
prompt user for which t-shirt details to modify (Console.WriteLine)
get t-shirt index from user (Console.ReadLine)
remove t-shirt details from array (tshirtDetails)
}
else if userResponse = 5 then exit the program
else
inform user of invalid option (Console.WriteLine)
I think that I'd like to use a "switch-case" statement instead of "if-else if-else" statements.
Revision #4:
Inform user what the program does (Console.WriteLine).
Give user 5 options. If user enters an invalid option, re-prompt user. Display these options until user selects option #5 (loop / Console.WriteLine)
1. Add New T-shirt Details
2. Edit Exisiting T-shirt details
3. Display All T-shirts in store
4. Delete T-shirt information
5. Exit
Get user response (Console.ReadLine)
Save user response in integer variable (name: option)
string[] tshirtDetails = new string[1];
switch(option)
{
case 1:
//add new t-shirt details
prompt user for t-shirt details (Console.WriteLine)
get t-shirt details from user (Console.ReadLine)
resize array (tshirtDetails)
store t-shirt details in array (tshirtDetails)
case 2:
//edit existing t-shirt details
prompt user for which t-shirt details to modify (Console.WriteLine)
get t-shirt index from user (Console.ReadLine)
update t-shirt details in array (tshirtDetails)
case 3:
//display all t-shirts in store
for each detail in tshirtDetails
{
Console.WriteLine t-shirt details
}
case 4:
//delete t-shirt information
prompt user for which t-shirt details to modify (Console.WriteLine)
get t-shirt index from user (Console.ReadLine)
remove t-shirt details from array (tshirtDetails)
case 5:
//exit
exit the program
default:
//user selected an option that doesn't exist
inform user of invalid option (Console.WriteLine)
}
I'm comfortable that I have enough detail to start programming.
namespace A4pf
{
class Program
{
static void Main(string[] args)
{
int option = 0;
string optionStr = string.Empty;
string[] tshirtDetails = new string[1];
Console.WriteLine("Welcome to the T-shirt Inventory Management program.\n");
do
{
//re-initialize
option = 0;
Console.WriteLine("1. Add New T-shirt Details");
Console.WriteLine("2. Edit Exisiting T-shirt detials");
Console.WriteLine("3. Display All T-shirts in store");
Console.WriteLine("4. Delete T-shirt information");
Console.WriteLine("5. Exit");
Console.Write("\nSelect an option: ");
optionStr = Console.ReadLine();
//convert to int
Int32.TryParse(optionStr, out option);
switch (option)
{
case 1:
//add new t-shirt details
Console.Write("Please enter the T-Shirts Details. ");
Console.Write("Brand Name and size(eg. Thrasher-M): ");
//ToDo: add code
Console.WriteLine("\nT-shirt details saved.\n");
break;
case 2:
//edit existing t-shirt details
//ToDo: add code
Console.WriteLine("\nT-shirt details updated.\n");
break;
case 3:
//display all t-shirts in store
//ToDo: add code
break;
case 4:
//delete t-shirt information
//ToDo: add code
Console.WriteLine("\nT-shirt details deleted.\n");
break;
//case 5:
//exit
// break;
default:
//user selected an option that doesn't exist
Console.WriteLine("\nError: Invalid option entered (" + optionStr + "). Please try again.\n");
break;
}
} while (option != 5);
}
}
}
You may notice that "Case 5" isn't needed because we evaluate if option = 5 in the "while" statement. I've commented it out. Since "case 5", and the code it contains isn't necessary, it can be deleted (removed).
The comment //ToDo: add code
means that you may need to add additional C# code.
Firestore:Javascript: How to convert user input into an array and storing it in Firestore?
Related Topics
Using Streams to Convert a List of Objects into a String Obtained from the Tostring Method
How to Format a String Number to Have Commas and Round
Java Classnotfoundexception With Maven Dependency
Spring Data JPA - Consider Defining a Bean Named 'Entitymanagerfactory' in Your Configuration
Duplicate Entry Exception: Spring Hibernate/Jpa Cascade Save Many to One
How to Exclude Property from Lombok Builder
Calculating the Sum of Number and Its Reverse in Java
Alternative to Switch Case in Java
Connection Java - Mysql:Public Key Retrieval Is Not Allowed
Convert Multi If-Else to Simple Lambda Expression Code in Java
A Method to Check If a Collection or Map Is Empty or Null
How to Convert a One-Dimensional Array to Two-Dimensional Array in Java
How to Update Only the Incoming Fields in a Model Using Spring Data Jpa
Spring Rest Post Json Requestbody Content Type Not Supported
Getting 400 for Spring Resttemplate Post
Java.Sql.Sqlexception: Access Denied for User 'Root'@'Localhost' (Using Password: Yes)