0

I wanna ask if this is possible. So I have this program will enter a for loop to get the user input on number of subjects. Then after he will enter the subjects listed in the array as his guide. My goal is that I want to check his subjects if it is really inside the array that I made. I made a program but I don't know where to put the part that the program will check the contents.

My goal:

Enter the corresponding code for the subjects you have chosen: user will input 8 Enter the number of subjects you wish to enroll: be able to type the whole subject name like (MATH6100) Calculus 1

then the program will check if the subjects entered are part of the elements inside the array

UPDATE: I have made another but the problem is that I don't know where to put the code fragment wherein it will check the contents of the user input for list of subjects he wish to enroll.

Here is the code:

private static void check(String[] arr, String toCheckValue){
boolean test=Arrays.asList(arr).contains(toCheckValue);

System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);

}

public static void main(String[] args){
String arr[]={"(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1", "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1", "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1"};

Scanner input1=new Scanner(System.in);
                        System.out.print("\nEnter the number of subjects you wish to enroll: ");
                        int number_subjects1=input1.nextInt();
                        String []subjects1=new String[number_subjects1];
                        
                        //else statement when user exceeds the number of possible number of subjects
                        
                        if(number_subjects1<=8){
                            for(int counter=0; counter<number_subjects1; counter++){
                                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): " + (counter+1));
                                subjects1[counter]=input1.next();
                                
                                
                                
                            }
                            String toCheckValue=subjects1[0];
                            System.out.println("Array: " +Arrays.toString(arr));
                                check(arr, toCheckValue);
                            
                            System.out.println("\nPlease check if these are your preferred subjects:");
                            for(int counter=0; counter<number_subjects1; counter++){ 
                                System.out.println(subjects1[counter]);
                            }System.out.println("**********************************  \n" + "\tNothing Follows");
                            
                            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
                            Scanner character=new Scanner(System.in);
                            String answer_1subjectserrors=character.nextLine();
                            System.out.println(answer_1subjectserrors + "Based on your answer, you need to refresh thae page and try again.");
                        }
                        
                            

} }

4
  • The code is tl;dr Please post a Minimal Reproducible example. Also, state how the program is not actually reaching your goal, and include some small example of exact input and the exact output it produced and the way the output does not match what is expected. Commented Jul 4, 2021 at 17:05
  • Thanks, i made another set of codes that is shorter and made an example Commented Jul 4, 2021 at 18:06
  • String toCheckValue=subjects1[0]; Is it only the first element you wish to check? If it is each element I would think about doing it as part of the for-loop in which you read in the input. Commented Jul 4, 2021 at 18:13
  • That part is for the all the subjects that the user inputs corresponding to his or her number of subjects he or she wishes to enroll for the upcoming semester. I tried putting counter instead of 0 and inside the for loop for entering the subjects but it gives me errors Commented Jul 4, 2021 at 18:19

2 Answers 2

1

I believe the issue is that you are checking your class course codes against an array which contains both the class code AND the class description.

You ask the user to enter the class code but then you use that code to check for its existence in an array containing both the code & description. The contains in List (collections) is not the same as the contains in String.

I have slightly modified your code so you may get the desired result.

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SOQuestion {
    
    private static void check(String[] arr, String toCheckValue){
        List courses = Arrays.asList(arr);
        
        boolean test=courses.contains(toCheckValue);;

        System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
    }

    public static void main(String[] args) {
        String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
                "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
                "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
        String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
        Scanner input1 = new Scanner(System.in);
        System.out.print("\nEnter the number of subjects you wish to enroll: ");
        int number_subjects1 = input1.nextInt();
        String[] subjects1 = new String[number_subjects1];

        // else statement when user exceeds the number of possible number of subjects

        if (number_subjects1 <= 8) {
            for (int counter = 0; counter < number_subjects1; counter++) {
                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
                        + (counter + 1));
                subjects1[counter] = input1.next();

            }
            String toCheckValue = subjects1[0];
            System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
            check(class_codes, toCheckValue);

            System.out.println("\nPlease check if these are your preferred subjects:");
            for (int counter = 0; counter < number_subjects1; counter++) {
                System.out.println(subjects1[counter]);
            }
            System.out.println("**********************************  \n" + "\tNothing Follows");

            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
            Scanner character = new Scanner(System.in);
            String answer_1subjectserrors = character.nextLine();
            System.out.println(
                    answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
        }

    }

}

When you are debugging always try to break down the statements into steps so you know where the error is. For example instead of boolean test=Arrays.asList(arr).contains(toCheckValue);

break it down to two steps like this :

        List courses = Arrays.asList(arr);
        
        boolean test=courses.contains(toCheckValue);

That way you will have an easier time checking for issues.

Second request is to always look at the API. Skim over the API to look at the method that you are using to understand it better. For example if you are using contains method of List then look up the API here: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/List.html#contains(java.lang.Object)

Of course since this is Oracle's Java the explanation is imprecise & not straightforward but it is usually helpful.

I would recommend using a different data structure than plain arrays. Since you are already using List why not use another collections data structure like HashMap?

Sign up to request clarification or add additional context in comments.

7 Comments

Hi! This really helps me a lot you gave me hope that I can finish this. I would like to ask is that I tried it but it only checks only one subject that was inputted by the user. How can I do it that will check all the subjects the user inputted?
I haven't heard with the HashMap our prof only taught until for loop
@user16376117 If your professor has not taught you hashmaps then learn it. Most people here have learned Java & its API on their own. I and everyone here is here to help you. It is easy to learn. You already know Lists, you can learn hashmaps too...
@user16376117 the reason why its only checking for one course is because you are checking for only one course (sometimes we write code and then forget what we wrote) . See this line here ` String toCheckValue = subjects1[0];`. You are only checking for one course/subject (as a test).
@user16376117 in the meanwhile please think how you will check for all the courses? I am attaching a cleaner version as an answer...
|
0

The original poster may want to look at a slightly refactored and cleaned up version of the code & try to figure out how to check for all courses since that is his next question. I believe that should become obvious with a more refactored code:

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SOQuestion {

    public static String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
            "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
            "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
    public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int desired_number_of_subjects = input_desired_number_of_subjects(input);
        
        String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);

            String toCheckValue = desired_subjects[0];
            System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
            check(class_codes, toCheckValue);

            pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
            System.out.println("**********************************  \n" + "\tNothing Follows");

            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
            Scanner character = new Scanner(System.in);
            String answer_1subjectserrors = character.nextLine();
            System.out.println(
                    answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
        }

    private static int input_desired_number_of_subjects(Scanner input) {
        System.out.print("\nEnter the number of subjects you wish to enroll: ");
        int take_number_of_subjects = input.nextInt();
        // TODO: else statement when user exceeds the number of possible number of subjects
        return take_number_of_subjects;
    }

    private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
        String[] subjects_totake = new String[desired_subjects_count];
        if (desired_subjects_count <= 8) {
            for (int counter = 0; counter < desired_subjects_count; counter++) {
                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
                        + (counter + 1));
                subjects_totake[counter] = input_desired_subjects.next();

            }
        }
        return subjects_totake;
    }

    private static void check(String[] arr, String toCheckValue){
        List courses = Arrays.asList(arr);
        boolean test=courses.contains(toCheckValue);
        System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
    }

    private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
        System.out.println("\nPlease check if these are your preferred subjects:");
        for (int counter = 0; counter < take_number_of_subjects; counter++) {
            System.out.println(take_subjects[counter]);
        }
    }       
}

I will shortly edit the above but a hint is : you can use a for loop to go over the entered desired_subjects array and do a check on each one of the subjects, perhaps?

The following checks for all the courses (though this is not how I would check the courses)

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class SOQuestion {

    public static String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
            "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
            "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
    public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int desired_number_of_subjects = input_desired_number_of_subjects(input);
        
        String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);

            check_all_desired_subjects(desired_subjects);

            pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
            System.out.println("**********************************  \n" + "\tNothing Follows");

            System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
            Scanner character = new Scanner(System.in);
            String answer_1subjectserrors = character.nextLine();
            System.out.println(
                    answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
        }


    private static int input_desired_number_of_subjects(Scanner input) {
        System.out.print("\nEnter the number of subjects you wish to enroll: ");
        int take_number_of_subjects = input.nextInt();
        // TODO: else statement when user exceeds the number of possible number of subjects
        return take_number_of_subjects;
    }

    private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
        String[] subjects_totake = new String[desired_subjects_count];
        if (desired_subjects_count <= 8) {
            for (int counter = 0; counter < desired_subjects_count; counter++) {
                System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
                        + (counter + 1));
                subjects_totake[counter] = input_desired_subjects.next();

            }
        }
        return subjects_totake;
    }

    private static void check_all_desired_subjects(String[] desired_subjects) {
        System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
        for (String subject_code_to_check:desired_subjects ) {
            check(class_codes, subject_code_to_check);      
        }
    }

    private static void check(String[] arr, String toCheckValue){
        List courses = Arrays.asList(arr);
        boolean test=courses.contains(toCheckValue);
        System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
    }
    private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
        System.out.println("\nPlease check if these are your preferred subjects:");
        for (int counter = 0; counter < take_number_of_subjects; counter++) {
            System.out.println(take_subjects[counter]);
        }
    }
    
    
}

5 Comments

Hi I want to ask about the difference between private and the public static void?
@user16376117 public and private are two of the four access modifiers. They allow various entities (code blocks, methods, classes, packages) to access your variable or methods. I use public modifiers with variables that I know I will use often just to be safe & private for methods or variables I do not think will be accessed too often. Here it is mostly random. Thanks for asking & don't forget to upvote the response & comment if it helped.
Thank you for the info about that. Are the variables inside the parentheses after the public and private modifiers called parameters like a part when using a method? And I have slightly added some else statement in the code can you please check it if I didn't disrupt anything?
How do I put the code here the one that I added?
@user16376117 the identifiers after the access modifiers have different names like method name, method signature. The best way to learn about the details is: buy a good book (like Head First Java by Kathy Sierra & Bert Bates & Java Certification by Asim Mughal). Blogs, forums & tutorials arent as helpful. Where have you added this else statement? I cant see your code. Also I posted large blocks of code & asked you to upvote that answer also (not just these comments) so I am enabled to participate & help & be helped in these forums, please.

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.