I need to create a functioning To-Do list in java that allows a user to:
Add an item
Delete an item
Show the list
Delete all tasks
Exit the program
I am currently having trouble adding an item to my list. Each time I enter an item to add this is the output I receive:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Test.menu(Test.java:50)
at Test.main(Test.java:11)
Here is what I have tried so far, any help would be greatly appreciated:
import java.util.Scanner;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
int menuItem = -1;
while(menuItem !=0) {
menuItem = menu();
switch(menuItem) {
case 1:
showList();
break;
case 2:
addItem();
break;
case 3:
removeItem();
break;
case 0:
break;
default:
System.out.println("Enter a valid option");
}
}
}
public static int menu() {
int choice;
Scanner keyboard = new Scanner(System.in);
System.out.println("Main Menu");
System.out.println();
System.out.println("0. Exit the program");
System.out.println("1. Display to-do list");
System.out.println("2. Add item to list");
System.out.println("3. Remove item from list");
System.out.println();
System.out.print("Enter choice: ");
choice = keyboard.nextInt();
return choice;
}
public static void showList() {
System.out.println("To-Do List");
Scanner input = new Scanner(System.in);
String line;
int number = 1;
while (input.hasNextLine()){
line = input.nextLine();
System.out.println(number + " ");
System.out.println(line);
++number;
}
System.out.println();
}
public static void addItem() {
System.out.println("Add Item");
Scanner input = new Scanner(System.in);
System.out.println("Enter an item: ");
String item = input.nextLine();
System.out.println(item);
}
public static void removeItem() {
int choice;
showList();
Scanner input = new Scanner(System.in);
System.out.println("What do you want to remove?");
choice = input.nextInt();
ArrayList<String> items = new ArrayList<String>();
int number = 1;
Scanner input2 = new Scanner(System.in);
String item;
while (input2.hasNextLine()) {
item = input2.nextLine();
if (number != choice)
items.add(item);
++number;
}
for(int i = 0; i < items.size(); i++)
System.out.println(items.get(i));
}
}
You are missing the actual operation of adding removing from the list. #DrMe go through these program you can find where you'r doing wrong. I hope this can help you
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestToDoList {
private static List<String> currentList = new ArrayList<String>();
public static void main(String[] args) {
int menuItem = -1;
while (menuItem != 0) {
menuItem = menu();
switch (menuItem) {
case 1:
showList();
break;
case 2:
addItem();
break;
case 3:
removeItem();
break;
case 0:
break;
default:
System.out.println("Enter a valid option");
}
}
}
public static int menu() {
System.out.println();
System.out.println("----------------------");
System.out.println("Main Menu");
System.out.println("----------------------");
System.out.println("0. Exit the program");
System.out.println("1. Display to-do list");
System.out.println("2. Add item to list");
System.out.println("3. Remove item from list");
System.out.println();
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
return choice;
}
public static void showList() {
System.out.println();
System.out.println("----------------------");
System.out.println("To-Do List");
System.out.println("----------------------");
int number = 0;
for (String item : currentList) {
System.out.println(++number + " " + item);
}
System.out.println("----------------------");
}
public static void addItem() {
System.out.println("Add Item");
System.out.println("----------------------");
System.out.print("Enter an item: ");
Scanner scanner = new Scanner(System.in);
String item = scanner.nextLine();
currentList.add(item);
showList();
}
public static void removeItem() {
System.out.println("Remove Item");
System.out.println("----------------------");
Scanner scanner = new Scanner(System.in);
System.out.print("What do you want to remove?");
int index = scanner.nextInt();
if((index-1)<0 || index>currentList.size()) {
System.out.println("Wrong index number! Please enter in range of 1 to "+currentList.size());
}else {
currentList.remove(index-1);
}
System.out.println("----------------------");
showList();
}
}
Further improve using generic and utils methods.
Related
I need to build code that first gets a 2d array and then prints it.
for this, I built a menu with a switch case.
when the user clicks 0, the user types the size of the array (the size is always n*n), and then the user types the values. then I need to create a function that uses this info to build a char array.(the values is hex base 0-F)
when the user clicks 1, the code needs to print the same 2d array.
I have a difficult time understanding how I can move the array from case 0.
import java.util.Scanner;
public class Assignment3 {
static Scanner reader = new Scanner (System.in);
public static void main(String[] args) {
int checker=1;
int user_selction;
do {
user_selction=Menu();
switch(user_selction) {
case 0:
Menu_0(user_selction);
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
checker=GoodBye(checker);
break;
default:
break;
}
}while(checker==1);
}
public static int Menu ()
{
int menu_num;
System.out.println("~ Photo Analyzed ~");
System.out.println("0. Load Photo");
System.out.println("1. Print Photo");
System.out.println("2. Circle Check");
System.out.println("3. Random Check");
System.out.println("4. Exit");
System.out.println("Please select an option>");
menu_num=reader.nextInt();
if(menu_num>4||menu_num<0)
{
System.out.println("Invalid input");
}
return menu_num;
}
public static int GoodBye(int GB)
{
GB=0;
System.out.println("Goodbye!");
return GB;
}
public static int Menu_0 (int a)
{
int Ps;
System.out.println("Please insert the photo size>");
Ps=reader.nextInt();
if(Ps<0||Ps>12)
{
System.out.println("Invalid Photo Input!");
return a;
}
System.out.println("Please insert the photo value>");
String strPhoto;
do {
strPhoto = reader.nextLine();
} while(strPhoto.length() == 0);
if(strPhoto.length()!=Ps*Ps)
{
System.out.println("Invalid Photo Input!");
return a;
}
for(int i=0;i<Ps*Ps;i++)
{
if(strPhoto.charAt(i)<'0'||strPhoto.charAt(i)>'F')
{
System.out.println("Invalid Photo Input!");
return a;
}
}
return a;
}
This is an example
import java.util.Scanner;
public class Assignment3 {
static Scanner reader = new Scanner (System.in);
public static void main(String[] args) {
int checker=1;
int user_selction;
char [][]array = null;
do {
user_selction=Menu();
switch(user_selction) {
case 0:
array = Menu_0();
break;
case 1:
print_array(array);
break;
case 2:
break;
case 3:
break;
case 4:
checker=GoodBye(checker);
break;
default:
break;
}
}while(checker==1);
}
public static int Menu ()
{
int menu_num;
System.out.println("~ Photo Analyzed ~");
System.out.println("0. Load Photo");
System.out.println("1. Print Photo");
System.out.println("2. Circle Check");
System.out.println("3. Random Check");
System.out.println("4. Exit");
System.out.println("Please select an option>");
menu_num=reader.nextInt();
if(menu_num>4||menu_num<0)
{
System.out.println("Invalid input");
}
return menu_num;
}
public static int GoodBye(int GB)
{
GB=0;
System.out.println("Goodbye!");
return GB;
}
public static char[][] Menu_0 ()
{
int Ps;
System.out.println("Please insert the photo size>");
Ps=reader.nextInt();
if(Ps<0||Ps>12)
{
System.out.println("Invalid Photo Input!");
return null;
}
System.out.println("Please insert the photo value>");
char [][]array = new char[Ps][Ps];
for(int i=0;i<Ps;i++)
{
for (int j = 0 ; j < Ps ; j++)
{
char c = reader.next().charAt(0);
if(c<'0'||c>'F')
{
System.out.println("Invalid Photo Input!");
return null;
} else {
array[i][j] = c;
}
}
}
return array;
}
public static void print_array(char [][]array){
for (int i = 0 ; i < array[0].length ; i++)
{
for (int j = 0 ; j < array[0].length ; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Use Scanner like this:
Scanner sc = new Scanner(System.in);
System.out.println("Select 1 to input array size or 2 to do sth");
int option = sc.nextInt();
switch(option) {
case 1 :
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: ");
int size = sc.nextInt()
int[] array = new array[size*size];
break;
case 2 :
something;
break;
default:
System.out.println("No such option ");
break;
}
I'm trying to code simple calculator (all in one) using Switch cases in java. I came up with following code so far. However I'm stuck with while loop. I want to keep showing main menu after each case execution until user decides to exit the program.
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Main Menu:");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("Enter your choice: ");
int i=s.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a=s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=s.nextInt();
int result=0;
switch(i)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
case 4:
result=a/b;
break;
default:
System.out.println("Wrong Choice.");
}
System.out.println("Answer is "+result);
}
}
Above code works fine. Program ends itself after execution of user selected choice. I want to put main menu on a repeat.
Add a while loop like this:
public static void main(String[] args) {
// Moved this outside the while loop as davidxxx pointed out +1
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("Main Menu:");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("Enter your choice: ");
int i = s.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a = s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b = s.nextInt();
int result = 0;//'result' will store the result of operation
switch (i) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
default:
System.out.println("Wrong Choice.");
}
System.out.println("Answer is " + result);
System.out.println("Go again?");
String goAgain = s.next();
if (!goAgain.equals("y")) {
break;
}
}
}
Try this:
import java.util.Scanner;
public class Calculator {
private static final String EXIT = "EXIT";
public static void main(String[] args) {
Calculator calc = new Calculator();
Scanner s = new Scanner(System.in);
while (true) {
String res = calc.runCalc(s);
if (res.equals(EXIT)) {
break;
} else {
System.out.println(res);
}
}
}
private String runCalc(Scanner s) {
System.out.println("Main Menu:");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.println("Enter your choice: ");
int i = s.nextInt();
if (i == 5) {
return EXIT;
}
System.out.println("ENTER FIRST NUMBER ");
int a = s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b = s.nextInt();
int result = 0;// 'result' will store the result of operation
switch (i) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
default:
return "Wrong Choice.";
}
return "Answer is " + result;
}
}
There is more than one way to achieve this, you can use
while loop.
do-while loop.
for loop.
I think do-while loop is better for your situation. Because either user wants to continue or not you have to proceed one time(before loop false). And you do not want to use another variable for quit the loop.
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int result=0;
do{
System.out.println("Main Menu:");
System.out.println("-1. complete and calculate");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multipication");
System.out.println("4. Division");
System.out.println("Enter your choice: ");
int i=s.nextInt();
if(i ==-1){
System.out.println("Answer is "+result);
return;
}
System.out.println("ENTER FIRST NUMBER ");
int a=s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=s.nextInt();
switch(i)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
case 4:
result=a/b;
break;
default:
System.out.println("Wrong Choice.");
break;
}
}while(true);
}
import java.io.*;
import java.util.*;
public class Stack1{
static final int MAX=100;
int top=-1;
int[] stack=new int[MAX];
public static void main(String args[])
{
Stack1 s1=new Stack1();
int opt, val;
System.out.println("1. PUSH ");
System.out.println("2. POP ");
System.out.println("3. PEEP ");
System.out.println("4. DISPLAY STACK ");
System.out.println("5. EXIT ");
System.out.println("\n Enter Your Option: ");
Scanner s=new Scanner(System.in);
opt=s.nextInt();
do{
switch(opt)
{
case 1: System.out.println("Enter the value to be added to the stack: ");
val=s.nextInt();
s1.push(val);
break;
case 2: s1.pop();
break;
/*
case 3: s1.peep();
break; */
case 4: s1.display();
break;
}
}while(opt!=5);
}
public void push(int val)
{
if(top==MAX-1)
{
System.out.println("Stack is FULL!");
}
else
{
top++;
stack[top]=val;
System.out.println("Element added to the stack is: "+val);
display();
}
}
public void pop()
{
int x;
if(top==-1)
{
System.out.println("Stack is EMPTY!");
}
else
{
x=stack[top];
System.out.println("The element deleted from the stack is: "+x);
top--;
display();
}
}
public void peep()
{
int n;
n=stack[top];
System.out.println("The value at the top of the stack is: "+n);
}
public void display()
{
int i;
if(top==-1)
System.out.println("STACK IS EMPTY!");
else
{
for(i=0; i<=top; i++)
System.out.println("The elements in the stack are: "+stack[i]);
}
}
}
I wrote this java code to implement stack. But once I select any option, only that method gets executed and the program ends. I want the program to provide me to enter another option once the current method is executed. What should I do?
As #LordWilmore pointed out in his comment, the opt value will be set just once causing program to spin forever in a corresponding case (unless the value is 5). Moving opt = s.nextInt(); inside loop will fix the issue.
do {
System.out.println("Enter Your Option: ");
opt = s.nextInt();
switch(opt) {
//...
}
} while (opt != 5);
You can modify your main method in a manner like this:
public static void main(String args[]){
int option;
Scanner sc = new Scanner(System.in);
MyStack1 s = new MyStack1();
while(true){
System.out.println("Enter the choice You want to perform on the stack: ");
System.out.println(" 1. push \n 2. Pop \n 3. Display \n 4. peep \n 5. Exit");
System.out.println("Enter your option: ");
option = sc.nextInt();
switch(option){
case 1: System.out.println("Enter the element you want to push into the stack: ");
s.push(sc.nextInt());
break;
case 2: s.pop();
break;
case 3: System.out.println("Displaying the stack contents: ");
s.display();
break;
case 4: System.out.println("The top element in the stack is: ");
s.peek();
break;
case 5: System.out.println("You selected Exit!!");
break;
default: System.out.println("Wrong choice!! Please enter a valid option!!");
return;
}
}
}
public class LibraryMenu {
static Scanner input = new Scanner(System.in);
static Holding[]holding = new Holding[15];
static Member[]member = new Member[15];
static int hold = 0;
private static Scanner scanner;
public static void main(String[] args){
int options;
do{
scanner = new Scanner(System.in);
System.out.println();
System.out.println("Library Management System");
System.out.println("1. Add Holding");
System.out.println("2. Print All Holding");
options = scanner.nextInt();
switch(options){
case 1:
addHolding();
break;
case 2:
print();
break;
default:
System.out.println("Please");
}
}while(options != 3);
System.out.println("I'm out");
}
public static void addHolding(){
int options1;
do{
System.out.println();
System.out.println("1. Book");
System.out.println("2. Video");
System.out.println("3. Return");
options1 = input.nextInt();
switch(options1){
case 1:
addBook();
break;
case 2:
addVideo();
break;
default:
System.out.println("Please enter the following options");
}
}while(options1 != 3);
}
public static void addBook(){
System.out.println("Title: " + "\t");
String title = input.next();
System.out.println("ID: " + "\t");
String holdingId = input.next();
Book tempbook = new Book(title, holdingId);
holding[hold] = tempbook;
++hold;
}
public static void addVideo(){
System.out.println("Title: ");
String title = input.next();
System.out.println("ID: ");
String holdingId = input.next();
System.out.println("Loan Fee: ");
int loanFee = input.nextInt();
Video tempvideo = new Video(holdingId, title, loanFee);
holding[hold] = tempvideo;
++hold;
}
public static void print(){
for(int i = 0; i < holding.length; i++){
System.out.println(holding[i]);
}
}
}
When I use the print() method, nothing shows up. Wondering what is wrong with the code and why it is not printing out. Using user input to add books and video etc. Tried to look around the internet for this problem, but cant seem to find it.
I'm having trouble with adding words and using do while in my code:
ArrayList<String> word = new ArrayList<>();
word.add("fish");
word.add("chicken");
word.add("icecream");
int lengthz = word.size();
Scanner sc = new Scanner(System.in);
System.out.println("Hangman");
System.out.println("1. 1 Player");
System.out.println("2. 2 Player");
System.out.println("3. Add word");
System.out.println("4. Quit");
System.out.print("Choice : ");
int opsi = sc.nextInt();
if (opsi == 3) {
boolean show = false;
boolean founded = false;
System.out.println("Input the words to be added : ");
boolean showall = true;
do {
String input = sc.next() + sc.nextLine();
for (int i = 0; i < lengthz; i++) {
if (!input.equals(word.get(i))) {
while (!founded) {
word.add(input);
System.out.println("Succeed!");
founded = true;
}
} else if (input.equals(word.get(i))) {
while (!show) {
System.out.println("Already Added");
show = true;
}
}
}
System.out.println("Want to add more words?");
String answer = sc.next() + sc.nextLine();
if (answer.equals("no")) {
System.out.println("Thanks for adding");
showall = false;
} else if (answer.equals("yes")) {
opsi = 3;
}
} while (showall);
for (int i = 0; i <= lengthz; i++) {
System.out.println(word.get(i));
}
}
My desired output will be like if the user wants to add more word with "yes" then it will repeat the program, and if the user types "no" then it will display all the words together with the addition, and then going back to show the menu 1-4 option. Please help me ... Thanks in advance !
Structure your code, e.g. as follows:
static final Scanner in = new Scanner(System.in);
static List<String> words = getStandardWords();
public static void main(String[] args) {
while (true) {
System.out.println("-- Hangman --");
System.out.println("1. 1 Player");
System.out.println("2. 2 Player");
System.out.println("3. Add word");
System.out.println("4. Quit");
System.out.print("Choice : ");
String choice = in.nextLine();
switch (choice) {
case "1":
//todo
break;
case "2":
//todo
break;
case "3":
addWord();
break;
case "4":
System.exit(0);
break;
default:
System.out.println("Invalid choice: " + choice);
break;
}
}
}
static List<String> getStandardWords() {
List<String> result = new ArrayList<>();
result.add("fish");
result.add("chicken");
result.add("icecream");
return result;
}
static void addWord() {
System.out.println("Input the word to be added: ");
String word = in.nextLine();
// add word
if (words.contains(word)) {
System.out.println("Already Added");
} else {
words.add(word);
System.out.println("Succeed!");
}
// add more words?
while (true) {
System.out.println("Want to add more words?");
String choice = in.nextLine();
switch (choice.toLowerCase(Locale.ROOT)) {
case "n":
case "no":
System.out.println("Thanks for adding");
// print all words
for (int i = 0; i < words.size(); i++) {
System.out.println(words.get(i));
}
return;
case "y":
case "yes":
addWord();
return;
default:
System.out.println("Invalid coice: " + choice);
break;
}
}
}
1) You need to initialize the
founded = false;
inside do - while loop
2) use last printing for loop as below :
for(int i=0; i<word.size();i++)
{
System.out.println(word.get(i));
}