简体   繁体   中英

Searching an Array for a combination of letters and numbers

I'm a first year programmer.

I've been trying to search an array which has stored four variables, with an input.

All of the examples I've found make use of int, and searches for a number within a list.

My program must search for a combination of letters and numbers. (Ex. COP 2800)

import java.util.Scanner;

public class courseInfo {
   public static int courseInfo(int[] list, int key) {

      Scanner input = new Scanner(System.in);

      // Input course name
      System.out.print("Enter course name: (Ex. COP 2800) ");
      double courseInput = input.nextDouble();

      for (int i = 0; i < list.length; i++) {
         if (key == list[i])
            return i;

         }
         return -1;
      }

      public static void main(String[] args) {
         int[] list = {COP 2800, PSY 1012, EVR 2001, COP 1000};
         System.out.println(linearSearch(list, courseInput));
   }
}

Please use layman's terms, I've only been in this class for three weeks.

If I remove COP, PSY, EVR, and COP from line 21, I return a different error;

courseInfo.java:22: error: cannot find symbol
         System.out.println(linearSearch(list, courseInput));
                                               ^
  symbol:   variable courseInput
  location: class courseInfo
1 error

You are checking an equals condition on String values with a courseInput which is supposed to be an int.

Also, You are returning -1 for whatever happens in the if condition. Are you supposed to return -1 if the value of key doesn't equal the one in the list?

Also the courseInput variable is local to the method. Make it global by declaring it in the class as a member variable.

You have double courseInput = input.nextDouble() , which will only accept doubles, not characters. You also have int[] list , which is an array of integers, so it cannot hold characters either. You'll have better luck if you put the whole course name (both letters and numbers) into a String. Then you can scan an entire line and store it in an array of Strings.

I suggest you do a bit more work on the basic data types of Java before attempting this. Java is a fairly strictly typed language which means that in general (there are plenty of exceptions) you need to decide what type of value you are storing in a variable ahead of time. You are mixing many types in your code: integers, doubles and strings.

So start by deciding if a course's identifier is a number of a name. If it's a number you can use an int to store it. If it's a name you can use a String . You note that a subject includes letters and numbers. But as long as you don't need to compare the numbers directly then you can store the entire identifier in a String . There's no need to split them up.

Another common trap for new starters is that primitive types (such as int ) behave quite differently than objects (such as String ). An immediate difference is the meaning of == . For primitive types it compares the values while for objects it checks if the left and right side refer to the same object.

I suspect you want your subjects to be identified by name (ie String ). In which case your code might look something like:

String[] subjects = {"COP2800", "PSY1012", "EVR2001"};

and

for (int i = 0; i < subjects.length; i++) {
    if (subjects[i].equals(name))
        return i;
}
return -1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM