1

I write 2 ArrayList type String contains the days and possible times , and I want the user to enter input , then check if the input is not from the array it will show a message that the input is invalid and the user enter again . but the result to my code give me the opposite :( when I enter something outside the array it will accept it what's wrong with my code?? and please show me the right code :(

package javaapplication19;

import java.util.ArrayList;
import java.util.Scanner;

public class JavaApplication19 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<String> dayArray = new ArrayList<>();
        ArrayList<String> timeArray = new ArrayList<>();
        dayArray.add("sunday");
        dayArray.add("monday");
        dayArray.add("tuesday");
        dayArray.add("wednesday");
        dayArray.add("thursday");
        timeArray.add("8am");
        timeArray.add("9am");
        timeArray.add("10am");
        timeArray.add("11am");
        timeArray.add("12pm");
        timeArray.add("1pm");
        timeArray.add("2pm");
        timeArray.add("3pm");
        timeArray.add("4pm");

        System.out.println("please enter day :");
        String a1 = input.nextLine();
        for (int g = 0; g < dayArray.size(); g++){
            if (dayArray.get(g).equals(a1))
                System.out.println("invalid day , please enter another day : ");
        a1 = input.nextLine();
}

        System.out.println("please enter time : ");
        String a2 = input.nextLine();
        for (int s = 0; s < timeArray.size(); s++) {
            if (timeArray.get(s).equals(a2))
                System.out.println("invalid time , please enter another time : ");
            a2 = input.nextLine();

        }
    }
}
6
  • Are you looking for dayArray.contains? Commented Nov 29, 2018 at 8:11
  • Do you know what this dayArray.get(g).equals(a1) means? Commented Nov 29, 2018 at 8:13
  • 1
    I think your use of brackets might be causing some of your issues Commented Nov 29, 2018 at 8:14
  • You check, if the input is equal, but want to check if it is NOT equal. So just add a "!" in your if clause Commented Nov 29, 2018 at 8:19
  • 3
    Possible duplicate of Check if a value exists in ArrayList Commented Nov 29, 2018 at 8:23

5 Answers 5

2

You can use below code and find change in if condition.

System.out.println("please enter time : ");
String a2 = input.nextLine();
for (int s = 0; s < timeArray.size(); s++) {
if (!timeArray.get(s).equals(a2))
System.out.println("invalid time , please enter another time : ");
a2 = input.nextLine();
}
Sign up to request clarification or add additional context in comments.

2 Comments

what? that's not a correct solution, isn't it?! you're looping over the elements of the list and each time it doesn't match the input print out that it's invalid - that's clearly wrong. you should only print it after you looped over all elements. And even more strange, you ask for a new input in each iteration. That's also weird
@ishadenkhaled that answer is wrong and doesn't work as expected
2

You can do it like this :

    String a1 = input.nextLine();
    if (!dayArray.contains(a1))
        System.out.println("invalid day , please enter another day : ");
    else {
        System.out.println("day really nice day");
    }

There is no need to iterate over the array. The method contains() does it for you

1 Comment

Nice to see someone just answering the question without pasting a wall of code
1

your code should be like this.

System.out.println("please enter day :");

boolean validDate = false;

while(!validDate){
    String a1 = input.nextLine();
    if(dayArray.contains(a1)){
        System.out.println("invalid day , please enter another day : ");
    }else{
        validDate=true;
    }
}

boolean validHour = false;
System.out.println("please enter a time : ");
while(!validHour){
    String a2 = input.nextLine();
    if(timeArray.contains(a2)){
        System.out.println("invalid time , please enter another time : ");
    }else{
        validHour=true;
    }
}

1 Comment

Instead of just posting the code, you can also explain what did the OP do wrongly and how did you fix it
0

I did refactor your code and put some comments to easier to follow

Scanner input = new Scanner(System.in);
List<String> dayArray = Arrays.asList("sunday","monday","tuesday","wednesday","thursday");
List<String> timeArray = Arrays.asList("8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm");

String a1 = "";
String a2 = ""; //Store user input

boolean nextInput = false; // flag indicate we continue or we force user re-enter information
//try to read the day input
while (!nextInput) {
    System.out.println("please enter day :");
    a1 = input.nextLine();
    nextInput = dayArray.contains(a1);
}

//try to read the time input
nextInput = false; //<= reset flag
while(!nextInput) {
    System.out.println("please enter time : ");
    a2 = input.nextLine();
    nextInput = timeArray.contains(a2);
}

System.out.println("day :" + a1);
System.out.println("time :" + a2);
System.out.println("program finished");

input.close(); //close scanner

Comments

0

write like this

System.out.println("please enter day :");
    String a1 = input.nextLine();
    while (!dayArray.contains(a1)){
        System.out.println("invalid day , please enter another day : ");
        a1 = input.nextLine();
    }
    System.out.println("please enter time : ");
    String a2 = input.nextLine();
    while (!timeArray.contains(a2)){
        System.out.println("invalid time , please enter another time : ");
        a2 = input.nextLine();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.