在二维数组中搜索字符串

时间:2014-05-04 01:58:15

标签: java arrays string search

我被困在这个程序中一段时间​​我必须制作一个程序,当用户输入项目和商店编号时,它会给出数量

import java.util.Scanner;


public class Searching {

    public static void main(String[] args) {
        String firstarray[][] = {
                {"", "Store101", "Store102", "Store103", "Store104"},
                {"Tennis Shoes", "102", "54", "20", "78"},
                {"Sweaters", "45", "25", "35", "75"},
                {"Jeans", "12", "35", "45", "65"},
                {"Shorts", "54", "25", "34", "45"},
                {"Jackets", "15", "35", "50", "25"}
        };

        Scanner input = new Scanner(System.in);
        System.out.println("Enter item");
        String userintake = input.next();
        System.out.println("Enter Store Number");
        String store = input.next();

        searchingitem(firstarray, userintake, store);

    }

    private static void searchingitem(String x[][], String item, String place) {
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                if ((item.equals(x[i][j])) || (place.equals(x[i][j]))) {
                    int row = i;
                    int column = j;

                    printArray(x, i, j);

                }

            }
        }
    }


    public static void printArray(String x[][], int product, int place) {
        for (int row = 0; row < 1; row++) {
            for (int column = 0; column < 1; column++)

                System.out.print(x[product][place] + "\t");

        }
    }


}

当我运行程序时,它会输出我键入的单词而不是数量。我使用了搜索方法,它确实提供了正确的数字,但是当它打印出来时,它没有打印正确的答案,我可以做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可能希望在两种方法中改进for循环。 请尝试以下代码,

import java.util.Scanner;


public class Searching 
 {


public static void main(String[] args)
{
 String[][] firstarray =
 {{"", "Store101", "Store102", "Store103", "Store104"},
 {"Tennis Shoes", "102", "54", "20", "78"},
 {"Sweaters", "45", "25", "35", "75"},
 {"Jeans", "12", "35", "45", "65"},
 {"Shorts", "54", "25", "34", "45"},
 {"Jackets", "15", "35", "50", "25"}
 };

 Scanner input = new Scanner(System.in);
 System.out.println("Enter item");
 String userintake = input.next();
 System.out.println("Enter Store Number");
 String store = input.next();

 searchingitem(firstarray, userintake, store);

 }

 private static void searchingitem(String[][] x, String item, String place)
  {
  int row =0 , col = 0 ;
  boolean itemFound = false , storeFound = false ;

  //Check if item is present and its row number.
  for(int i = 0 ; i < x.length ; i++)
  {
      if(x[i][0].equalsIgnoreCase(item))
      {
          row = i ;
          itemFound = true ;
          break;
      }
  }

  //Check if item is present in the array or return form the method by giving an error.
  if(!itemFound)
  {
      System.out.println("Item not found.\n");
      return;
  }

  //Getting store
  for(int j = 0 ; j < x[0].length ; j++)
  {
      if(x[0][j].equalsIgnoreCase(place))
      {
          col = j ;
          storeFound = true ;
          break;
      }
  }

  if(!storeFound)
  {
      System.out.println("Can not find store number.\n");
      return;
  }

  //You do not need this method. you can directly printout from here but since you have made this method, I will use it.
  printArray(x, row, col);
  }



public static void printArray(String x[][], int product, int place) 
{
    //Primting out the no of items available in store.
    System.out.println(" Item:  "+ x[product][0] + "\n Store :  " + x[0][place] + "\n Availalbe units: " + x[product][place]);
}


}

我做了以下更改。

1 - 我没有检查所有值,而是制作了两个for循环,它只检查第一列的商品和第一行的商店号。

2 - 如果找不到商品或商店,则会给出错误并返回。

3 - 我没有在print方法中运行for循环,而是将println放到了只打印所需内容的位置。