0

Essentially what I need to do is match the spacing of the name (line 1) with the price (line 2) but it has not gone as planned and I'm not certain why. For whatever reason I never had an issue using a test set of ints in place of price but now that I have switched it to a double the spacing is quite off.

Output:

Chips               Cookies             Applesauce          Cracker-n-Cheese    
0.75             1.0              0.5                 1.0                       

Apple               Snickers            BabyRuth            MilkyWay            
0.5                    1.0                       1.0                       1.0                       

M&M's               Kit-Kat             TrailMix            CrunchyCheetos      
1.0              1.0                1.5                 0.75                        
What would you like to buy?

Code:

    for (int i = 1; i <= 3; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            /*System.out.println("What is the name of the snack item you'd like to add to row " + i +", column " + j + "?");
            name = in.next();
            System.out.println("What is the price of  " + name +"?");
            price = in.nextDouble();
            product = new Product(name, price);
            products[i-1][j-1] = product;*/ 

            //Change back to what's above when done testing
            name = fin.next();
            price =fin.nextDouble();
            product = new Product(name, price);
            products[i-1][j-1] = product;


            if(i == 1 && j == 1)
                max = name.length();
            else if(name.length() > max)
                max = name.length();
        }
    }

    System.out.println("\n\n ");
    while (!in.equals("-999"))
    {
        for (int i = 0; i < 3; i++)
        {
            System.out.println();
            for (int j = 0; j < 4; j++)
            {
                numSpaces = max + 4 - products[i][j].getName().length();
                System.out.print(products[i][j].getName());
                for (int k = 0; k < numSpaces; k++)
                    System.out.print(" ");
            }
            System.out.println();
            for (int j = 0; j < 4; j++)
            {
                len = "" + products[i][j].getPrice(); //Can't cast from double to string :(
                numSpaces2 = numSpaces + products[i][j].getName().length() - len.length();//Doesnt work
                System.out.print(products[i][j].getPrice());
                for (int k = 0; k < numSpaces2; k++)
                    System.out.print(" ");
            }
            System.out.println();
        }


        System.out.println("What would you like to buy?");
        itemRequested = in.next();

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (itemRequested.equalsIgnoreCase(products[i][j].getName()))
                    boughtItem = products[i][j];
            }
        }

        if(boughtItem != null)
        {
            System.out.println("Thank you for selecting " + boughtItem.getName() + "!\nThat will cost $" + boughtItem.getPrice());
            System.out.println();
            //Add delay for 7 seconds later
        }
        else
        {
            System.out.println("We dont carry that!\n");
            //Add delay for 7 seconds later
        }

    }

    in.close();

}
  • Please post the first portion of your code (the part that initializes `products`). Next, look into [formatted io](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax). – Elliott Frisch Apr 20 '15 at 00:35
  • @ElliottFrisch products is initialized to null originally and is not changed until those lines of code. Also I appreciate the recommendation but I'm trying to avoid using format for the sake of learning. –  Apr 20 '15 at 00:44

2 Answers2

1

If you print a tab character (\t), that will align the output after it to the next tab-stop. Then you wouldn't have to count characters.

Rosa
  • 642
  • 6
  • 20
  • 1
    The only trouble happens when one tab isn't enough because the word extends past the next tab stop and the number doesn't – D. Ben Knoble Apr 20 '15 at 00:44
0

Two issues in there

  1. You don't know about converting a double to a string - Converting double to string
  2. You're spacing the price according to the name of the product when you should be spacing it according to the "max"
Community
  • 1
  • 1
Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23
  • if i space according to max that will not align it with the product name either –  Apr 20 '15 at 00:42
  • `numSpaces = max + 4 - products[i][j].getName().length();` - the products are 4+max apart. You need the costs to be 4+max apart too. – Ashley Frieze Apr 20 '15 at 00:44
  • Slightly off there but that comment made me realize the issue. not sure why I was originally adding numSpaces. the only thing I needed to change with yours for it to work was to have products[i][j].getName().length(); be changed to len.length(); –  Apr 20 '15 at 00:51
  • Ah yes... though `4 + max - len.length` would probably also do it. – Ashley Frieze Apr 20 '15 at 01:05