Adding user data to an array for storage and use

need to take user input, store in an array then print a recipt from.

Page 1 of 1

3 Replies - 4573 Views - Last Post: 17 November 2010 - 09:55 PM Rate Topic: -----

#1 kyfirefly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-March 09

Adding user data to an array for storage and use

Posted 17 November 2010 - 05:54 PM

I have a working program with no errors but now as my part 4 and 5 i need to add an array to store user input, Product, farm it was purchased from, case price and case's sold and a sales tax and overall sales. This much I have working but now it needs to be stored in an array and also needs to be displayed in a GUI. But my first problem is getting the user input into and array and avoiding the null exception error for empty array. My book does not show or explain how to get information into an array from user input.thanks in advsance.

This post has been edited by kyfirefly: 17 November 2010 - 05:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding user data to an array for storage and use

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Adding user data to an array for storage and use

Posted 17 November 2010 - 06:05 PM

Your gonna want to use a scanner to read in data then store the data in an instance of a class then put that instance into an array

//store data scanned in variables and p
YourClass yourInstance = new Class(yourVariables)

//some YourClass array[]
class[i] = yourInstance;


Was This Post Helpful? 0
  • +
  • -

#3 kyfirefly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-March 09

Re: Adding user data to an array for storage and use

Posted 17 November 2010 - 08:07 PM

So I would make a new class or method? This might help as well. Tis is my main class and i have another class with the get,set and return. the main class just inputs and outputs the data to the console. sorry i hope i posted this correctly.
package week5checkpoint;


import java.util.Scanner;
import java.text.NumberFormat;
/**
 *
 * @author Kyfirefly
 */
public class FarmerMarket
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
      Scanner input = new Scanner(System.in);
      SalesMarket sm = new SalesMarket();
      NumberFormat nf = NumberFormat.getCurrencyInstance();
      boolean Product = true;

      while (Product){
         // gets the product name
        System.out.println("Please enter the Product name or stop to exit: ");
        sm.setProductName(input.next());

            if(sm.getProductName().equalsIgnoreCase("stop"))
            {
                Product = false;
                System.out.println("Thank You and Have a nice day!");
                break;
            } //ends if
            else
            { //lets the rest of the display to go on
         // gets the product SKU number
        System.out.println("Please enter the SKU number: ");
        sm.setProductNumber(input.nextLong());
        // gets the number of cases sold
        System.out.println("Please enter the number of Cases ordered: ");
        sm.setCasesSold(input.nextInt());
        // sets the case price
        System.out.println("Please enter the Case price: ");
        sm.setCasePrice(input.nextDouble());


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Adding user data to an array for storage and use

Posted 17 November 2010 - 09:55 PM

If you make a break; no need to make and else
obscur for the ones who will read back your code

      while (true){                  // infinite loop
         // gets the product name
        System.out.println("Please enter the Product name or stop to exit: ");
        sm.setProductName(input.next());

        if(sm.getProductName().equalsIgnoreCase("stop"))
        {
           System.out.println("Thank You and Have a nice day!");
           break;
        } //ends if
        //lets the rest of the display to go on
        // gets the product SKU number
        System.out.println("Please enter the SKU number: ");
        sm.setProductNumber(input.nextLong());
        // gets the number of cases sold
        System.out.println("Please enter the number of Cases ordered: ");
        sm.setCasesSold(input.nextInt());
        // sets the case price
        System.out.println("Please enter the Case price: ");
        sm.setCasePrice(input.nextDouble());
      }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1