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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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 toString方法并在列表中搜索字符串相等性_Java_Arrays_Oop_Tostring - Fatal编程技术网

Java toString方法并在列表中搜索字符串相等性

Java toString方法并在列表中搜索字符串相等性,java,arrays,oop,tostring,Java,Arrays,Oop,Tostring,这是一个面向对象的程序,有两个类:目录和产品,读取包含产品名称、数量和价格的文件,将它们添加到列表数组中,并计算项目的总价。我的程序除了有一个toString方法外,其他一切都可以正常工作。当我运行程序时,它会打印出类似于@445ea7e的内容,我认为这是因为Catalog类上的addProducts和getProductsString名称与toString有关,我不知道如何将它们连接到工作中 import java.io.File; import java.io.FileNotFou

这是一个面向对象的程序,有两个类:目录和产品,读取包含产品名称、数量和价格的文件,将它们添加到列表数组中,并计算项目的总价。我的程序除了有一个toString方法外,其他一切都可以正常工作。当我运行程序时,它会打印出类似于@445ea7e的内容,我认为这是因为Catalog类上的addProducts和getProductsString名称与toString有关,我不知道如何将它们连接到工作中

 import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Catalog{



    private static int MAX_ITEMS = 10;

    /**
     * List of Products objects.
     */
    private Products[] list;
    private int nextItem;


    /**
     * Default constructor 
     */
    public Catalog(){
        list = new Products[MAX_ITEMS];
        nextItem = 0;
    }

    /**
     * Adds an item to the list
     */
    public void addProducts (String name, int quantity, double price){
            **//I tried this: list[nextItem] = new Products(name, quantity, price);
                //but I'm not sure if that's ok.**

     }

    /**
     * Reads items from an input file.
     */
    public void loadList(String fileName)
        throws FileNotFoundException { 
             if ( (fileName != null) && (!fileName.equals("")) ) { 
             Scanner input = new Scanner(new File(fileName)); 
             String newLine = null; 
             String name = null;
             int quantity = 0;
             double price = 0.0;


            while (input.hasNextLine() && nextItem < MAX_ITEMS) { 
             if (input.hasNext()) { 
             name = input.next(); 
             } else { 
             System.err.println("ERROR Not a String"); 
             System.exit(2); 
             }
             if (input.hasNextInt()) { 
             quantity = input.nextInt(); 
             } else { 
             System.err.println("ERROR Not an integer"); 
             System.exit(2); 
             } 
             if (input.hasNextDouble()) { 
             price = input.nextDouble(); 
             } else { 
             System.err.println("ERROR Not a double"); 
             System.exit(2); 
             }
             list[nextItem] = new Products(name, quantity, price); 
             newLine = input.nextLine(); 
             nextItem += 1; 
             } 
             } 
             return; 
             } 

    /**
     * Calculate the total cost of products.
     */
    public double getTotalPrice(){
        double total = 0.0;
        for(int i=0; i < nextItem; i++){
            Products products = list[i];
            total+=products.getTotalPrice();
        }
        return total;
    }

    /**
     * Search Catalog items with a product name
     */
    public Products getProducts(String name){
        **//search a list for string equality using the name of the product and returns it to the caller**

    }

         public static void main(String[] args) 
             throws FileNotFoundException { 
             Catalog catalog= new Catalog(); 
             catalog.loadList(args[0]);
             System.out.println();
             //System.out.println(toString());                  **I don't know how to call toString**
             System.out.println();
             System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
            }
}
} 这是包含产品信息的文件

足球215.50

章13.87

第4册10.00


在products类中的toString方法中,更改

String s = null;

此外,为了调用Catalog类,您必须为Catalog类创建一个toString方法

catalog类的toString方法示例如下:

public String toString(){
 String toReturn = "";
 for(Products current: list){
  toReturn+=current.toString()+"\n";
 }
 return toReturn;
}
然后,如果main只是调用System.out.printlncatalog.toString

将toString作为产品类添加到Catalog类中:

您可以调用catalog.toString:

在Product类的toString方法中。您应该将初始化变量从s=null更改为;到s=;。以下是示例:
希望有此帮助:

尝试向toString方法添加覆盖这可能是基于当前输出的解决方案

@Override
public String toString(){
    String s=null;
    s+=getName();
    s+=s + " ";
    s+=getQuantity();
    s+=s + " ";
    s+=getPrice();
    return s;    
}

非常感谢它的帮助:所以您应该将其标记为已接受答案,以便StackOverFlow上的其他人将此作为参考:
public String toString(){
 String toReturn = "";
 for(Products current: list){
  toReturn+=current.toString()+"\n";
 }
 return toReturn;
}
public class Catalog {
private static int MAX_ITEMS = 10;

/**
 * List of Products objects.
 */
private Products[] list;
private int nextItem;

// your old code here

  // new code
  @Override
  public String toString() {
     return "this is a catalog";
  }        
}
public static void main(String[] args) 
     throws FileNotFoundException { 
     Catalog catalog= new Catalog(); 
     catalog.loadList(args[0]);
     System.out.println();
     //System.out.println(toString());                  **I don't know how to call toString**
     // use this line
     System.out.println(catalog.toString());  
     System.out.println();
     System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
    }
public String toString(){
    String s="";
    s+=getName();
    s+=s + " ";
    s+=getQuantity();
    s+=s + " ";
    s+=getPrice();
    return s;    
}
@Override
public String toString(){
    String s=null;
    s+=getName();
    s+=s + " ";
    s+=getQuantity();
    s+=s + " ";
    s+=getPrice();
    return s;    
}