Accepting string inputs into an array and checking the availability of the string in the array

how to take string array input in java
how to take string array input in java using scanner
how to input string in array in c
array of strings
how to store user input into an array in java
can we store a string and integer together in an array in c
how to store string in array in java
array of strings in c

So I am completely new to java, and I want to create a code to accept string inputs from a user, and store it into an array. After this in the next statement, I will type a value into the terminal, and I want the code to compare my string input to one of the strings in the array and print available on the terminal when the string is available and vice versa. The first part of my code was right (hopefully) but I had a problem in comparing the strings. I feel it doesn't check the strings with my input in the code. Here is my code, Could anyone please help me with this? Thank you so much.

import java.util.Scanner;
class Course {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String a[] = new String[20] //assuming max 20 strings
  System.out.println("Enter no. of courses");
  int no_of_courses = sc.nextInt(); // number of strings
  if (no_of_courses <= 0)
   System.out.println("Invalid Range");
  else {
   System.out.println("Enter course names:");
   for (int i = 0; i < no_of_courses; i++) {
    a[i] = sc.next(); //accepting string inputs
   }
   System.out.println("Enter the course to be searched:");
   String search = sc.next() //entering a string to search
   for (int i = 0; i < no_of_courses; i++) {
    if (a[i].equals(search)) //I feel the problem is here
     System.out.println(search + "course is available");
     break;
     else
      System.out.println(search + "course is not available");
    }
   }
  }
 }

I expect the output to be

<string> course is available

when my string matches a string in the array and

<string> course is not available

when my entered string doesn't match a string in the array

But there is no output :(

I have modified your code and commented on line where it need to be explained. check it carefully.

import java.util.Scanner;

class Course {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter no. of courses");
        int no_of_courses = sc.nextInt(); // number of strings
        String a[] = new String[no_of_courses]; // do not assume when you have proper data.
        if (no_of_courses <= 0)
            System.out.println("Invalid Range");
        else {
            System.out.println("Enter course names:");
            for (int i = 0; i < no_of_courses; i++) {
                a[i] = sc.next(); // accepting string inputs
            }
            System.out.println("Enter the course to be searched:");
            String search = sc.next(); // entering a string to search
            boolean flag = false;
            for (int i = 0; i < no_of_courses; i++) {
                if (a[i].equals(search)) // I feel the problem is here
                {
                    flag = true;//do not print here. just decide whether course is available or not
                    break;
                }
            }
            //at the end of for loop check your flag and print accordingly.
            if(flag) {
                System.out.println(search + "course is available");
            }else {
                 System.out.println(search + "course is not available");
            }
        }

    }
}

Day 5 -- Arrays, Conditionals, and Loops, are usually ASCII characters terminated with a NULL (0) character. Step 2: Create a character array of the same length as of string. Step 3: Traverse over the string to copy character at the i’th index of string to i’th index in the array. Step 4: Return or perform the operation on the character array.

class Course {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String a[] = new String[20] ;                //assuming max 20 strings
                System.out.println("Enter no. of courses");
        int no_of_courses = sc.nextInt();           // number of strings
        if(no_of_courses <= 0)
            System.out.println("Invalid Range");
            else
            {
                System.out.println("Enter course names:");
                for(int i=0 ; i < no_of_courses ; i++)
                {
                    a[i] = sc.next();                            //accepting string inputs
                }
                System.out.println("Enter the course to be searched:");
                String search = sc.next()   ;             //entering a string to search
                boolean found = false;
                        for(int i = 0 ; i < no_of_courses ; i++)
                        {
                            if(a[i].equalsIgnoreCase(search))                   //I feel the problem is here
                            {
                                **found = true;**
                                break;
                            }   

                            }
                        if(found) {
                            System.out.println(search+ "course is available"); 
                        }else {
                            System.out.println(search+ "course is not available");
                        }
                        }
            }
    }

JavaScript String split() Method, Let us create a program to accept a string as input from the user and check if it is a Compute the reverse of array a and store the reverse string in array b. If a C string is a one dimensional character array then what's an array of C string looks like? It's a two dimensional character array! Here is how an array of C string can be initialized:

I noticed a few things - Does your program run to the end? When i copy/pasted into my ide i noticed a few missing semi-colons, and like Yhlas said, your last if/else statement syntax is incorrect.

And this doesn't have anything to do with whether or not your program will give you the right answer, but your last loop will print over and over again because it will check each element in a, and each time it loops and finds a mismatch it will print something out

Chapter 8: Arrays and Strings -- Valvano, To test your function, create a structure array containing the data for three plots of Be sure to check the number of input arguments in your function. 7.8 Write a function that will accept a structure as an argument and return two cell arrays containing the first a structure array, and the second a field name stored in a string. String Input and Output. Input function scanf () can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf () function, it terminates its input on the first white space it encounters.

This is really a good effort and you almost got it. So just a couple of things

  1. Since you are inputting the number of courses, just use that value to initialise your array (it's just a good practice to get into to try not initialise things before you actually need them).
  2. If you are doing String comparisons and case sensitivity does not matter, rather use .equalsIgnoreCase(String)
  3. To solve your problem, you just needed a boolean variable to indicate whether or not you had found a match. Initially this would be FALSE (no match found) and you would run through your array until a match is found. Once found this would be flagged TRUE and you'd breakout your loop (which you correctly did).
  4. Only once out your loop, you'd print out whether you found a match.

Have a look:

public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter no. of courses");
  int no_of_courses = sc.nextInt();           // number of strings
  if (no_of_courses <= 0) {
    System.out.println("Invalid Range");
  } else {
    String a[] = new String[no_of_courses];
    System.out.println("Enter course names:");
    for (int i = 0; i < a.length; i++) {
      a[i] = sc.next();                            //accepting string inputs
    }
    System.out.println("Enter the course to be searched:");
    String search = sc.next();                //entering a string to search
    boolean courseFound = Boolean.FALSE;
    for(int i = 0; i < a.length; i++) {
      if (a[i].equalsIgnoreCase(search)) {
        courseFound = Boolean.TRUE;
        break;
      }
    }
    if(courseFound) {
      System.out.println(search + "course is available");
    } else {
      System.out.println(search + " course is not available");
    }
  }
}

Oh, just for interest (and when you start working with some more advanced constructs), you could always just use stream, which was introduced in Java 8. It'll trim down 12 lines to 5...

if(Arrays.stream(a).anyMatch(i -> i.equalsIgnoreCase(search))) {
  System.out.println(search + " course is available");
} else {
  System.out.println(search + " course is not available");
}

Computer Programming with C++, and add special logic to the test harness to handle the “emptystring” token special input argl : H"; else arg1 = tokens[2]; bool actual = StringLib. When testing API methods that accept string arguments, you should always test for empty strings. Split into a string array named tokens will store an empty string into tokens [2]  So I am completely new to java, and I want to create a code to accept string inputs from a user, and store it into an array. After this in the next statement, I will type a value into the terminal,

MATLAB Programming for Engineers, We can try the following program which can accept data from users,after that it will store user data into an array. String[] uinput=new String[4];. for(int i=0;i<sc.​length  Write a C program to input elements in an array and print array using pointers. How to input and print array using pointer in C programming. Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.

.NET Test Automation Recipes: A Problem-Solution Approach, At the beginning of the tutorial, you have mentioned taking in an input using : char a[10]; scanf(“%s”, a); I guess the variable needs to be stored in memory as a​  The scanf() function in Line 8 reads a string from standard input and stores it in the firstname array. The %s conversion character directs scanf() to look for a string as input, just as %s is a placeholder for strings in printf()’s output.

How to save user input inside an array in Java, For example, we might want to write a program that inputs and then ranks or sorts a C++ provides a structured data type called an array to facilitate this kind of task. Unlike some languages, C++ does not do range bound error checking, so we would The compiler will then not accept any statements within the function's​  Read the entire input as a string, then use string.split to split it at each space and convert the resulting string with parseInt. May 28, 2015 at 9:03pm mjamesball9 (61) I really don't know what I am doing fml

Comments
  • I think your syntax is wrong for "if/else" part. It should be in the following format: if(condition){ ... code } else{ ..code }
  • Hmm I actually thought there was a problem when comparing my string input to that of what is already present in the array. Because strings are reference types the dot equals method is kinda complicated.
  • Actually since I just joined stackoverflow today, I couldn't upvote, I need min 15 reputation but i accepted the answer :)
  • Yes actually it did run to the end but it threw some exceptions.
  • Woah! That was a really elaborate explanation. I now understood where I went wrong. Thank you soo much 😀
  • No problem, please check the answered checkbox in the left corner.