1

So I have a method that displays the information from my arrays. I want to have another method where I can add information to those arrays. You can see in the code how I am trying to do this. I am trying to use the addTV() method to create a TV and put it in the array which is in displayTVs().

package harveynorman;

import java.util.Scanner;

/**
 * Created by tiern on 11/06/2017.
 */
public class HarveyNorman
{
    public static void main(String args[])
    {
        displayTVs();
    }

    public static void addTV()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the product code: ");
        String productCode = scanner.nextLine();

       if (productCode.startsWith("UE")) {
            modelNo.add(productCode);
       }

    }

    public static void displayTVs()
    {
        String[] modelNo = new String[]{"UE43MU5500", "UE49MU6200", "UE50MU6100", "UE55MU6100", "UE55MU6200", "UE55MU6500", "UE55MU7000", "UE55MU9000", "UE65MU6100", "UE65MU6100"};
        int[] quantity = new int[] {1, 2, 1, 1, 1, 2, 1, 2, 2, 2};
        int[] price = new int[] {729, 949, 929, 1079, 1199, 1299, 1739, 2394, 2399, 2759};
        String[] location = new String[] {"Opposite Samsung Stand", "Samsung Stand", "6th from Left, Bottom Row", "Top Row, 2nd from Right", "Top Row, Second from Left", "N/A", "Samsung Stand", "Samsung Stand", "N/A", "N/A"};


        System.out.print("SAMSUNG TVS");
        System.out.println("--------------------");

        for (int i = 0; i<modelNo.length; i++)
        {
            System.out.println(i+1 + ":\t" + "Model No:\t" + modelNo[i] + ".\tIn stock: " + quantity[i] + ".\t€ " + price[i] + "\tLocation:\t" + location[i] + ".");
        }
    }
}
13
  • Your simplest option is to pass the same mutable List object to the two methods. One option is to define the List of televisions in the main() method. If you're not allowed to use a List, pass a fixed-size array to the two methods, and handle overflow. Also, you'll have to call addTV() somewhere. Commented Jun 12, 2017 at 20:12
  • Well, I'd start by creating a Television class an have a Television[] rather than four separate arrays. I'd then probably return that from displayTVs. I'd then consider using a list instead of an array. Commented Jun 12, 2017 at 20:12
  • @JonSkeet I appreciate the help but I am fairly new to Java and not really experienced in using classes. Commented Jun 12, 2017 at 20:14
  • If your teacher has not yet introduced Java classes, you can pass four parallel fixed-size arrays or flexible-sized Lists down to each method. [Side-comment - although I know of at least one university that is leaving object-oriented concepts until a second course, this seems like a mistake to me.] Commented Jun 12, 2017 at 20:16
  • 1
    Right, so that should be in the question, along with what goes wrong when you try to use it. Please read codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question and bear in mind that the aim of Stack Overflow is not to be an interactive debugging service - it's to create a repository of high quality questions and answers. Commented Jun 12, 2017 at 20:25

2 Answers 2

4

Arrays in Java have a fixed size. Without copying them over in another, larger array its not that easy to just append another item into the array.

You can use Java List's to solve this.

a ArrayList<String> stores as many Strings as you wish (depending of the available memory - but that should not bother you)


The easiest way to solve your problem would be to your variables (modelNo, location, quantity and price) static class variables¹ of your class.

and change their types from Array to List types (String[] -> ArrayList<String>)

This will result in not-so-clean code.


A nicer solution would be to remove the static keyword from everyone of your methods except the main method.

in your main, create a instance of your class and call the methods on this instance. like:

HarveyNorman myClassInstance = new HarveyNorman();
myClassInstance.displayTVs();

then move your variables out of the method as member variables¹.


Another approach, to make your code more organized and better readable:

Create a new class for your TV-stock's with member variables for modelNo, quantity, price, location. (Or for TV's in general, depends a bit on you)

and then use a List of this class instead 4 separate Arrays for your data.


¹ :

class MyClass{
    static int a;       // class variable
    int b;              // member variable

    static void foo(){} // class method        
    void foo(){}        // member method
}
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot add anything to those arrays from a different method for 2 reasons.

  1. They are local to the method displaysTv (means they can only be accessed through there)
  2. Arrays cannot be resized. If you want to add dynamically to an array you need to use ArrayList or at least set a size for the array.

To explain my second point further the problem with your arrays is that you have already set their size and the values to go inside them when you placed the values in them on creation. If you want to add more you simply can't.

To answer your question use ArrayList, this way you can add as many records as you want. However do not declare those ArrayList inside of the method, declare them outside to have them as instance variables which are accessible by all methods within the class.

I have re-wrote your code so it would do what you want if you are stuck on anything let me know.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by tiern on 11/06/2017.
 */
public class HarveyNorman {

    private static ArrayList<String> modelNo = new ArrayList<>();
    private static ArrayList<Integer> quantity = new ArrayList<>();
    private static ArrayList<Integer> price = new ArrayList<>();
    private static ArrayList<String> location = new ArrayList<>();

    public static void main(String args[])
    {
        displayTVs();
    }

    public static void addTV()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the product code: ");
        String productCode = scanner.nextLine();

       if (productCode.startsWith("UE")) {
            modelNo.add(productCode);
       }

    }

    public static void displayTVs() {


        String[] modelNoRecords = new String[]{"UE43MU5500", "UE49MU6200", "UE50MU6100", "UE55MU6100", "UE55MU6200", "UE55MU6500", "UE55MU7000", "UE55MU9000", "UE65MU6100", "UE65MU6100"};
        modelNo.addAll(Arrays.asList(modelNoRecords));
        Integer[] quantityRecords = new Integer[] {1, 2, 1, 1, 1, 2, 1, 2, 2, 2};
        quantity.addAll(Arrays.asList(quantityRecords));
        Integer[] priceRecords   = new Integer[] {729, 949, 929, 1079, 1199, 1299, 1739, 2394, 2399, 2759};
        price.addAll(Arrays.asList(priceRecords));
        String[] locationRecords = new String[] {"Opposite Samsung Stand", "Samsung Stand", "6th from Left, Bottom Row", "Top Row, 2nd from Right", "Top Row, Second from Left", "N/A", "Samsung Stand", "Samsung Stand", "N/A", "N/A"};
        location.addAll(Arrays.asList(locationRecords));

        System.out.print("SAMSUNG TVS");
        System.out.println("--------------------");

        for (int i = 0; i<modelNo.size(); i++) {
            System.out.println(i+1 + ":\t" + "Model No:\t" + modelNo.get(i) + ".\tIn stock: " + quantity.get(i) + ".\t€ " + price.get(i) + "\tLocation:\t" + location.get(i) + ".");
        }
    }
}

3 Comments

@AndyThomas Edited
The edit provides an answer. That said, it can be harmful to do the student's work for him or her. It invites plagiarism, which may hinder the learning and endanger enrollment. There's a middle ground which helps the student overcome an obstacle, but still requires the student to write the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.