Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Personal program

SO one of my end goals is to code a little utility for my job.

Our product has two unique numbers associated with each product.

A SKU number and a stock number.

In production they use the stock number , in billing they use the SKU number.

Anytime production talks to billing , everyone is confused.

I want to write a program that the user inputs one or the other number , returning the other number.

So if one person enters the SKU number it returns the stock number, and vice versa.

I could do this now with if statements, but it would take forever because there are many products , and I know that an easy way exists.

I can't fully wrap my head around how an array would do this.

Can this be solved by some type of database linkage ?

Can Java read from an excel sheet ?

Am I going to have to learn SQL as well to complete my goal ?

Any and all input is appreciated , when I'm not working on chapters here I would like to start developing this program and eventually port it to Android as well.

Thanks !

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Erin;

If you don't want to build a full blown database, a HashMap may be useful.

It would allow you to do something like:

import java.util.HashMap;

public class skuConverterHashMap {

    public static void main(String []){
        HashMap<String, String> skuConverter = new HashMap<String, String>();
        //add key-value pair to hashmap
        skuConverter.put("sku1", "stockNumber1");
        skuConverter.put("sku2", "stockNumber2");
        skuConverter.put("sku3", "stockNumber3");
        System.out.println(skuConverter);
        //getting value for the given key from hashmap
        System.out.println("Value of second: "+skuConverter.get("sku2"));
        System.out.println("Is HashMap empty? "+skuConverter.isEmpty());
        skuConverter.remove("sku3");
        System.out.println(skuConverter);
        System.out.println("Size of the HashMap: "+skuConverter.size());
    }
}

Output:

{sku2=stockNumber2, sku3=stockNumber3, sku1=stockNumber1}

Value of second: stockNumber2

Is HashMap empty? false

{sku2=stockNumber2 sku1=stockNumber1}

Size of the HashMap: 2

Depending on the size and number of SKUs you have, it may be better suited for a database, but this might be an option for you. There is a new Java course scheduled to come out in a couple of weeks that will be covering Java Data Structures in further detail.

Hope it helps,

Ken

Hi Ken ,

Thank you so much for your reply !

I will experiment with this when I get home from work to let you know how it works.