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?

4
  • I decided to initialize the array the same length as original input array and when parsing it to Double it removed the remaining whitespaces. Commented Sep 10, 2017 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. Commented Sep 10, 2017 at 21:57
  • Possible duplicate of Java variables not initialized error Commented Sep 10, 2017 at 23:34
  • Or a duplicate of Variable might not have been initialized error Commented Sep 10, 2017 at 23:36

4 Answers 4

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);
   }

}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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;

Comments

0
char[] itemPriceArray = new char[inputArray.length];

Comments

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.