1
double pullPrice(String input){
        if(input.length() < 3){
            System.out.println("Error: 02; invalid item input, valid example: enter code here'milk 8.50'");
            System.exit(0);
        }
        char[] inputArray = input.toCharArray();
        char[] itemPriceArray;
        double price;
        boolean numVal = false;
        int numCount = 0;
        for(int i = 0; i <= inputArray.length-1; i ++){
            //checking if i need to add char to char array of price
            if(numVal == true){
                //adding number to price array
                itemPriceArray[numCount] = inputArray[i];
                numCount++;
            }
            else{
                if(inputArray[i] == ' '){
                    numVal = true;
                    //initializing price array
                    itemPriceArray = new char[inputArray.length - i];
                }
                else{

                }
            }


        }
        price = Double.parseDouble(String.valueOf(itemPriceArray));
        return price;
    }

Problem: attempting to pull the sequence of chars after white space between 'milk 8.50' as input. Initialization error occurs because I am initializing char array inside an if else statement that will initialize the array if it finds whitespace.

Question: since I don't know my char count number until I find a whitespace is there another way I can initialize? Does the compiler not trust me that I will initialize before calling array.

Also, if I am missing something or there are better ways to code any of this please let me know. I am in a java data structures class and learning fundamental data structures but would also like to focus on efficiency and modularity at the same time. I also have a pullPrice function that does the same thing but pulls the item name. I would like to combine these so i don't have to reuse the same code for both but can only return items with same datatype unless I create a class. Unfortunately this exercise is to use two arrays since we are practicing how to use ADT bags. Any help is greatly appreciated?

user3238382
  • 31
  • 1
  • 5
  • I decided to initialize the array the same length as original input array and when parsing it to Double it removed the remaining whitespaces. – user3238382 Sep 10 '17 at 21:53
  • *"Does the compiler not trust me?"* Heck no. What if the `input` has no spaces? Then what should `String.valueOf(itemPriceArray)` be? *You* have to decide that, not the compiler. – Andreas Sep 10 '17 at 21:57
  • Possible duplicate of [Java variables not initialized error](https://stackoverflow.com/questions/14484550/java-variables-not-initialized-error) – Bernhard Barker Sep 10 '17 at 23:34
  • Or a duplicate of [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Bernhard Barker Sep 10 '17 at 23:36

4 Answers4

2

Try something like this:

double pullPrice(String input)
{
   try
   {
      // Instantiate a new scanner object, based on the input string
      Scanner scanner = new Scanner(input);
      // We skip the product (EG "milk")
      String prod = scanner.next();
      // and read the price(EG 8.5)
      double price = scanner.nextDouble();
      // We should close the scanner, to free resources...
      scanner.close();
      return price;
   }
   catch (NoSuchElementException ex)
   {
      System.out.println("Error: 02; invalid item input, valid example: enter code here 'milk 8.50'");
      System.exit(0);
   }

}
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
  • I would modify your solution to allow for products with more than one string in name and use of try-with-resource since Scanner implements AutoCloseable. – sebast26 Sep 10 '17 at 22:05
0

I will add to the other answers, since you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new:

int oldItems[] = new int[10];
for (int i=0; i<10; i++) {
  oldItems[i] = i+10;
}
int newItems[] = new int[20];
System.arraycopy(oldItems, 0, newItems, 0, 10);
oldItems = newItems;
0

If you are sure that you program will get only proper input data then just initialize your array with null:

char[] itemPriceArray = null;

The main problem why the compiler is complaining - what happens if your program accesses uninitialized variable (for instance with wrong input data)? Java compiler prevents this kind of situations completely.

algrid
  • 5,600
  • 3
  • 34
  • 37
0
char[] itemPriceArray = new char[inputArray.length];
M.Fneish
  • 1
  • 2