【问题标题】:Searching for Strings in two dimensional arrays在二维数组中搜索字符串
【发布时间】:2014-05-04 01:58:15
【问题描述】:

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

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");

        }
    }


}

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

【问题讨论】:

    标签: java arrays string search


    【解决方案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 以便它只打印需要的内容。

    【讨论】:

    • @user3373392 :如果它完成了工作,请将其标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2020-07-27
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多