Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 搜索数组中的元素_Java_Arrays - Fatal编程技术网

Java 搜索数组中的元素

Java 搜索数组中的元素,java,arrays,Java,Arrays,我必须写一个有3个并行数组的程序,一个包含4位学生ID,第二个包含学生姓名,最后一个包含GPA,数组大小必须为10 此外,该程序还可以进行学生ID搜索,如果不存在,则显示错误消息 第一部分很好,但当涉及到搜索时,它不起作用 import java.util.ArrayList; import javax.swing.JOptionPane; import java.util.Scanner; public class StudentIDArray { public static

我必须写一个有3个并行数组的程序,一个包含4位学生ID,第二个包含学生姓名,最后一个包含GPA,数组大小必须为10 此外,该程序还可以进行学生ID搜索,如果不存在,则显示错误消息

第一部分很好,但当涉及到搜索时,它不起作用

  import java.util.ArrayList;

 import javax.swing.JOptionPane;

 import java.util.Scanner;

public class StudentIDArray
 {

public static void main(String[] args)
{
    int option;
    String inputString;
    inputString = JOptionPane.showInputDialog("Welcome"
            +" Choose the option you will like"
            + " \n1. Enter Student  Information "
            + "\n2. Search for Student");
    option = Integer.parseInt(inputString);

    if(option ==1)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the name of the Student");
        String[] studentname = new String[10];

        for(int i =0; i<studentname.length; i++)
        {

            studentname[i] = in.nextLine();
        }

        System.out.println("Enter the 4 digit Student ID");
        int[] studentID = new int[10];
        for(int x=0; x<studentID.length; x++)
        {
            studentID[x] = in.nextInt();
        }



        System.out.println("Enter the Student's Grade Point Average");
        int[] gpa = new int[10];
        for(int y=0; y<studentID.length; y++)
        {
            gpa[y] = in.nextInt();
        }
    }
    else
    {
        searching();
    }




}

public static void searching()
{

    int idnumber,
        results;
    int[]studentID = null;

    String inputString;
    inputString = JOptionPane.showInputDialog("Please Enter the ID number");
    idnumber = Integer.parseInt(inputString);

    results = sequentialSearch(studentID, idnumber);
    if (results == -1)
    {
        System.out.println("no information");
    }
    else
    {
        System.out.println("yeii congrats");
    }

}


public static int sequentialSearch(int[] studentID, int value)
{
      int index;
      int element;
      boolean found;

      index =0;

      element = -1;
      found = false;

      while(!found && index < studentID.length)
      {
        if (studentID[index] == value)
        {
            found = true;
            element = index;
        }
        index++;
      }
      return element;

    }
}
import java.util.ArrayList;
导入javax.swing.JOptionPane;
导入java.util.Scanner;
公立班学生
{
公共静态void main(字符串[]args)
{
int选项;
字符串输入字符串;
inputString=JOptionPane.showInputDialog(“欢迎”
+“选择您喜欢的选项”
+“\n1.输入学生信息”
+“\n2.搜索学生”);
option=Integer.parseInt(inputString);
如果(选项==1)
{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入学生姓名”);
String[]studentname=新字符串[10];

对于(int i=0;i而言,主要问题是您在本地向方法声明
studentID
数组

改为类成员(最好是实例成员,但所有方法都是静态的),以便方法在同一个数组上工作,而不是在不同的数组上工作:

public class StudentIDArray
{
    private static int[] studentID = new int[10];
    ...

注意,这限制了条目的数量到数组的大小,如果超出这些限制,将得到异常。请考虑使用<代码>矢量< /代码>或反之。

将studentID数组传递给搜索方法,而不是再次声明它

public static void searching(int[] studentID)
{

    int idnumber,
        results;
    //int[]studentID = null;

    String inputString;
    inputString = JOptionPane.showInputDialog("Please Enter the ID number");
    idnumber = Integer.parseInt(inputString);

    results = sequentialSearch(studentID, idnumber);
    if (results == -1)
    {
        System.out.println("no information");
    }
    else
    {
        System.out.println("yeii congrats");
    }

}


public static int sequentialSearch(int[] studentID, int value)
{
      int index;
      int element;
      boolean found;

      index =0;

      element = -1;
      found = false;

      while(!found && index < studentID.length)
      {
        if (studentID[index] == value)
        {
            found = true;
            element = index;
        }
        index++;
      }
      return element;
    }
公共静态无效搜索(int[]studentID)
{
int idnumber,
结果;
//int[]studentID=null;
字符串输入字符串;
inputString=JOptionPane.showInputDialog(“请输入ID号”);
idnumber=Integer.parseInt(inputString);
结果=顺序搜索(studentID、idnumber);
如果(结果==-1)
{
System.out.println(“无信息”);
}
其他的
{
System.out.println(“yeii恭喜”);
}
}
公共静态int序列搜索(int[]studentID,int值)
{
整数指数;
int元素;
布尔发现;
指数=0;
元素=-1;
发现=错误;
而(!found&&index

更好的方法是将其作为@Andreas指出的静态/类变量。

这些是程序的运行步骤。
第一步=>输入数组的大小。
第二步=>输入数组成员。
第三步=>输入数组的搜索整数

        import java.util.Scanner;

        public class Searching 
        {
            public static void main(String[] args)
            {
                 int temp =-1;
                Scanner user_input=new Scanner(System.in);
                System.out.println("Enter Size Of Array ");

                int Size=user_input.nextInt();
                int[] a=new int[Size];
                //Scan input Array.
                System.out.println("Enter element Of an Array...");
                for(int j=0;j<Size;j++)
                {
                    a[j]=user_input.nextInt();
                }

               System.out.println("The contents of the Array are :");
               //Print input Arrray
               for(int i=0;i<a.length;i++)
               {
                  System.out.println("array[" + i + "] = " + a[i]);
               }

                System.out.println("Enter Integer For Searching..");
                int Search=user_input.nextInt();
               //Searching in an Array.
                for(int index=0;index<a.length; index++)
                {
                    if(a[index]==Search)
                    {
                        temp=index;
                        break;
                    }
                }
                if(temp!=-1)
                {
                    System.out.println(" The search element is : " + Search);
                    System.out.println(" It is found in the array at position : " + temp);      
                }
                else
                  System.out.println("\n Element not Found..");
            }
         }
import java.util.Scanner;
公共类搜索
{
公共静态void main(字符串[]args)
{
内部温度=-1;
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入数组大小”);
int Size=user_input.nextInt();
int[]a=新的int[Size];
//扫描输入阵列。
System.out.println(“输入数组的元素…”);

对于(int j=0;jIs现在更容易理解了吗?你能澄清一些要点吗?当你想让用户键入学生姓名或ID时,用户是否必须一个字符一个字符地键入?你提到过它们必须存储在数组中,数组必须最多包含10个元素。但是你想从用户那里得到4位数的ID,但你仍然需要10个用户输入。我想你得到了“10元素数组”错误。它可以是“输入大小不能超过10个字符”吗?你想搜索你以前保存的学生数据,但是你没有把这些数据保存到一个对象中,当你想搜索的时候,你不会从任何地方搜索。考虑改变这些致命错误。
        import java.util.Scanner;

        public class Searching 
        {
            public static void main(String[] args)
            {
                 int temp =-1;
                Scanner user_input=new Scanner(System.in);
                System.out.println("Enter Size Of Array ");

                int Size=user_input.nextInt();
                int[] a=new int[Size];
                //Scan input Array.
                System.out.println("Enter element Of an Array...");
                for(int j=0;j<Size;j++)
                {
                    a[j]=user_input.nextInt();
                }

               System.out.println("The contents of the Array are :");
               //Print input Arrray
               for(int i=0;i<a.length;i++)
               {
                  System.out.println("array[" + i + "] = " + a[i]);
               }

                System.out.println("Enter Integer For Searching..");
                int Search=user_input.nextInt();
               //Searching in an Array.
                for(int index=0;index<a.length; index++)
                {
                    if(a[index]==Search)
                    {
                        temp=index;
                        break;
                    }
                }
                if(temp!=-1)
                {
                    System.out.println(" The search element is : " + Search);
                    System.out.println(" It is found in the array at position : " + temp);      
                }
                else
                  System.out.println("\n Element not Found..");
            }
         }