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();

        }
    }
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
ishadenkhaled
  • 33
  • 1
  • 6

5 Answers5

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

eckad158
  • 385
  • 6
  • 19
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();
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • 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 – UninformedUser Nov 29 '18 at 08:31
  • @ishadenkhaled that answer is wrong and doesn't work as expected – UninformedUser Nov 29 '18 at 08:34
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;
    }
}
  • Instead of just posting the code, you can also explain what did the OP do wrongly and how did you fix it – Andreas Nov 29 '18 at 08:27
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
Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29
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();
    }