存储/显示阵列中的数据输入

时间:2014-09-18 13:31:30

标签: java arraylist

我在如何以及在何处放置数组时遇到问题。 我想出了如何以及在何处放置循环以便它将继续收集将存储在阵列中的多个用户数据但我不知道将数组放在何处。将有一个产品编号数组,一个产品名称数组和一个价格数组。

import java.io.*; 
public class DataInsideArrays 
{ 
public static DataInputStream a = new     DataInputStream(System.in) 
public static void main(String      args[])throws Exception 
{ 
int y = 0; 
for(int x=1;x<=100;x++)
{                           
System.out.println("1 - Maintenance");
System.out.println("2 - Transaction");
System.out.println("");
System.out.print("Enter code: ");
int code =
Integer.parseInt(a.readLine()); 
System.out.println("");
if(code==l) 
{ 
System.out.print("") ; 
System.out.print("Product number: ");
System.out.print("Product name: "); 
String prodname = a.readLine(); 
System.out.print("Price: "); 
int price = Integer.parseInt(a.readLine()); 
System.out.println(""); 
} 
else if(code==2) 
{
System.out.println("Display List of           Products-Prices") 
System.out.print("Enter product number:
") ; 
int prodnum
Integer.parseInt(a.readLine()); 
System.out.println("")
} 
if(code==3) 
{ 
System.out.println("Invalid");        
System.out.println("Restart");
System.out.println(""); 
}}}}

5 个答案:

答案 0 :(得分:0)

public class DataInsideArrays { 
    public static DataInputStream a = new DataInputStream(System.in);
   List<String> productname=new ArrayList<String>();

    public static void main(String args[]) throws Exception {
        // rest of your code...
        if(code == 1) { 
            System.out.print("") ; 
            System.out.print("Product number: ");
            System.out.print("Product name: "); 
            String prodname = a.readLine(); 
            System.out.print("Price: "); 
            int price = Integer.parseInt(a.readLine()); 
            System.out.println(""); 

  ////////////////////////to store data place  arraylist here/////////////////////////////
           productname.add(prod);

  /////////////////////  //same way use arr to store price and product no
        } else if(code == 2) {
            System.out.println("Display List of Products-Prices") ;
            System.out.print("Enter product number:") ; 
            int prodnum=Integer.parseInt(a.readLine()); 
            System.out.println("");
        }
        if(code == 3) { 
            System.out.println("Invalid");        
            System.out.println("Restart");
            System.out.println(""); 
        }
    }
}

答案 1 :(得分:0)

你应该使用List代替数组,因为你不知道它们的长度的先验。

List允许您在获得元素后立即以动态方式添加元素,因此您应该&#34;放置数组&#34;在用户输入下。

List<String> names = new ArrayList<String>(); //<-- do the same for price(Integer) and product number (Integer)
public static void main(String args[]) throws Exception) { 
//...
    if(code==1) //<-- This was a l, check again your code.
    { 
        System.out.print("") ; 
        System.out.print("Product number: ");
        System.out.print("Product name: "); 
        String prodname = a.readLine(); 

        names.add(prodname); //<-- add value to ArrayList

        System.out.print("Price: "); 
        int price = Integer.parselnt(a.readLine()); 
        System.out.println(""); 
    }
//...
}

要打印数组内容,只需迭代ArrayList:

for(String prodname: names)
    System.out.println(prodname); //<-- This will print all your product names

答案 2 :(得分:0)

你必须使用数组和“if”语句吗? 通常的做法是使用List作为switch语句:

int[] productArray = new int[100];
List<Integer> productList = new ArrayList<Integer>();
for(int x=1;x<=100;x++) {
    switch (code){
    case 1:
        productArray[x-1] = Integer.parseInt(a.readLine());
        productList.add(new Integer(a.readLine()) );
        break;
    default: 
        System.out.println("Invalid")
        break;
    }
}

答案 3 :(得分:0)

首先,您在l (lowercase L)循环中获得此for值的位置?这不应该是1 (number one)吗?此外,如果它是0 (zero)会更好,所以你可以将它用作数组的索引。

第二件事,int y = 0'?你在程序中的任何地方都没有使用y

int y = 0;
for(int x = l; x <= 100; x++)

将其更改为:

for(int x = 0; x < 100; x++)

由于您希望拥有3个独立的数组,因此您应该有3个单独的索引来跟踪它们。

现在让我们看看如何初始化一个数组来存储数据。您应该将大小声明为类顶部的变量(或main()中的本地变量),以便以后在整个程序中更改它。您可以对阵列执行相同的操作。在你的类中或在main()内部声明它。我将向您展示如何在课堂外宣布这两种方法,但您也应该尝试另一种方式来练习。

现在请记住,在使用数组时,您必须在创建数组后立即知道数组的大小,之后无法更改该数组。这意味着只有很多元素可以放在数组中。如果您想要可变大小的结构,请使用ArrayList s检查一些答案。

public class DataInsideArrays 
{ 
    public static DataInputStream a = new DataInputStream(System.in) 
    public static int size = 100; // now you only change this number and everything works
    public static int[] productNumbers = new int[size];
    public static String[] productNames = new String[size];
    public static int[] productPrices = new int[size];

    public static void main(String args[]) throws Exception 
    { 

        int prodnumIndex = 0; // index for tracking product numbers
        int prodnameIndex = 0; // index for tracking product names
        int prodpriceIndex = 0; // index for tracking product prices
        for(int x = 0; x < size; x++)
        {        
        // ... your code...

        // ...

        int prodNum = Integer.parseInt(a.readLine());
        // check if we didn't reach our maximum size
        if(prodnumIndex < productNumbers.length) {
            productNumbers [prodnumIndex] = prodnum;
            prodnumIndex++; // increment
        } else {
            System.out.println("Cannot add product number. Reached maximum amount.");
        }


        // ...

        String prodName = a.readLine();
        // check if we didn't reach our maximum size
        if(prodnameIndex < productNames.length) {
            productNames [prodnameIndex] = prodName ;
            prodnameIndex++; // increment
        } else {
            System.out.println("Cannot add product name. Reached maximum amount.");
        }

        // ...

        int prodPrice = Integer.parseInt(a.readLine());
        // check if we didn't reach our maximum size
        if(prodpriceIndex < productPrices.length) {
            productPrices [prodpriceIndex] = prodPrice ;
            prodpriceIndex++; // increment
        } else {
            System.out.println("Cannot add product number. Reached maximum amount.");
        }   

        // ... 

        }            

另一种方法是创建一个名为Product的对象,它将包含您需要的属性,然后拥有该对象的数组。

class Product {
    int num;
    String name;
    int price;
    Product(int num, String name, int price) {
        this.num = num;
        this.name = name;
        this.price = price;
    }
}

// ...

// declare your array of `Product` objects...
Product[] products = new Product[size];

// ...

// then you can do something like
System.out.println("Enter product number:");
int prodNum = Integer.parseInt(a.readLine());
System.out.println("Enter product name:");
String prodName = a.readLine();
System.out.println("Enter product price:");
int prodPrice = Integer.parseInt(a.readLine());

products[INDEX] = new Product(prodNum, prodName, prodPrice);

// ...

另外,请考虑使用double float作为产品价格,因为它更符合实际产品。

答案 4 :(得分:0)

这里是我想要的输出的正确代码。也许还有其他方法可以用不同的编码获得相同的输出吗?

import java.io.*;
import java.util.ArrayList;
public class DataArrays 
{ 
    public static DataInputStream a = new DataInputStream(System.in);
    public static void main(String args[])throws Exception
    {
        ArrayList<Integer> prodNum = new ArrayList<Integer>(100);
        ArrayList<String> prodName = new ArrayList<String>(100);
        ArrayList<Integer> prodPrice = new ArrayList<Integer>(100);
        ArrayList<Integer> prodPay = new ArrayList<Integer>(100);

        for(int x=1;x<=100;x++)
        {      
            System.out.println("1 - Maintenance");
            System.out.println("2 - Transaction");
            System.out.println("");

            System.out.print("Enter code: ");
            int code = Integer.parseInt(a.readLine());
            System.out.println("");

            int y = 1;
            if(code==1) 
            {
                System.out.print("") ; 
                System.out.println("Product number: "+ x);
                prodNum.add(x);             //this brings 1st input to array element #0 which is not needed
                prodNum.add(x);
                System.out.print("Product name: "); 
                String prodname = a.readLine(); 
                prodName.add(prodname);     //this brings 1st input to array element #0 which is not needed
                prodName.add(prodname);
                System.out.print("Price: ");
                int prodprice = Integer.parseInt(a.readLine());
                prodPrice.add(prodprice);   //this brings 1st input to array element #0 which is not needed
                prodPrice.add(prodprice);   
                System.out.print("Payment: ");
                int prodpay = Integer.parseInt(a.readLine());
                prodPay.add(prodpay);       //this brings 1st input to array element #0 which is not needed
                prodPay.add(prodpay);
                System.out.println("");
                System.out.println("Start New Transaction:");
                System.out.println("");
            }
            else if(code==2) 
            {
                System.out.println("Display List of Products-Prices"); 
                System.out.print("Enter product number: "); 
                int i = Integer.parseInt(a.readLine());
                i = prodNum.get(i);         //this gets the data stored in the arraylist. Assuming it starts with 1 and above
                prodNum.set(1, i);
                System.out.println("");

                System.out.println("Product name: "+ prodName.get(i));
                System.out.println("Product price: "+ prodPrice.get(i));
                System.out.println("Product payment: "+ prodPay.get(i));
                System.out.println("");
                System.out.println("Start New Transaction:");
            } 
            else if(code>=3) 
            { 
                System.out.println("Invalid");        
                System.out.println("Restart");
                System.out.println("");
            }
            else if(code==0)
            {
                System.out.println("Program will end");
                break;
        }}}}

感谢帮助我们。真的很感激。