#Storing user data
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Make the variable declarations above the while (true)
Make use of delayed assignment
can you paste the code in here ?
follow the instructions of the bot
` not '
its next to your 1 key
Detected code, here are some useful tools:
good enough
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String catName = null;
System.out.println("To Do List");
while(true) {
System.out.println("Choose what you want to do");
System.out.println("1, Add category");
System.out.println("2, Delete category");
System.out.println("3, Add task");
System.out.println("4, Delete task");
System.out.println("5, Show categories/tasks");
int choice = input.nextInt();
input.nextLine();
if (choice == 1) {
System.out.println("Type the name of the topic");
catName = input.nextLine();
}
else if (choice == 2 ) {
System.out.println("What topic would you like to delete?");
String delCat = input.nextLine();
}
else if (choice == 3) {
System.out.println("What topic do you want to add a task into?");
String catAddTask = input.nextLine();
}
else if (choice == 4) {
System.out.println("What task do you want to delete?");
String taskDel = input.nextLine();
}
}
}
}
see how you can access catName in the other branches
you need to check that it was assigned a value
if (catName != null) {
}
else {
}
(depending on what you are going to do with that)
no, you only put the type on the initial declaration
everything after that is a reassignment
Book teaching how to write modern and effective Java. It is maintained by the community, anyone can contribute.
yep
the book in the link might be helpful
at least for drilling in some early basics
Book teaching how to write modern and effective Java. It is maintained by the community, anyone can contribute.
i know, and its cool when i can just use my book to answer questions
lift the variable declarations outside of the ifs
and only assign values in the ifs
- handle what happens if they select options out of order
let me trim it down then
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String catName = null;
System.out.println("To Do List");
while(true) {
System.out.println("Choose what you want to do");
System.out.println("1, Give a cat name");
System.out.println("2, Show cat name");
int choice = input.nextInt();
input.nextLine();
if (choice == 1) {
System.out.println("Name a cat");
catName = input.nextLine();
}
else if (choice == 2 ) {
if (catName == null) {
System.out.println("You have not named a cat yet");
}
else {
System.out.println("Cat is named " + catName);
}
}
}
}
}
yep
basically null means "does not have a value yet"
and you can check for what you want to do if a value isn't assigned, reset the variables, etc
Detected code, here are some useful tools:
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String catName = null ;
String delCat = null ;
String catAddTask = null ;
String tasldel = null ;
System.out.println("To Do List");
while (true) {
System.out.println("Choose what you want to do");
System.out.println("1, Add category");
System.out.println("2, Delete category");
System.out.println("3, Add task");
System.out.println("4, Delete task");
System.out.println("5, Show categories/tasks");
int choice = input.nextInt();
input.nextLine();
if (choice == 1) {
System.out.println("Type the name of the topic");
catName = input.nextLine();
}
else if (choice == 2) {
System.out.println("What topic would you like to delete?");
delCat = input.nextLine();
}
else if (choice == 3) {
System.out.println("What topic do you want to add a task into?");
catAddTask = input.nextLine();
}
else if (choice == 4) {
System.out.println("What task do you want to delete?");
taskDel = input.nextLine();
}
}
}
}
well
be careful
here
else if (choice == 4) {
System.out.println("What task do you want to delete?");
taskDel = input.nextLine();
}
you probably aren't going to use taskDel from other branches
so its fine to just keep that variable scoped
its only when you want to re-use the variable that it matters
yep
you'll figure it out
Detected code, here are some useful tools:
I can go in VC in a bit
Detected code, here are some useful tools:
okay @surreal sapphire
i think you should probably ask your brother for help
you should have enough of a start by now
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍