Link to home
Start Free TrialLog in
Avatar of WalterAPO
WalterAPOFlag for United States of America

asked on

Java printf with tab \t

Dear Experts,

I new to Java and I'm working on a homework problem.   I'm stuck and need help. I don't want someone to do my homework; I just need some help figuring out my problem.

I can't get my menu to display correctly.  The first line has an enter tab because the word "Okinawa Soba" has 2 words.  If I deleted the word Soba the menu works correctly.  How do I get the menu to display correctly?  I tried using JOptionPane.showInputDialog, but that don't work.  I put comments on the other lines while I was working on this problem.

Thank you.




      import javax.swing.*;

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

      public class HumkoAssignment3Menu  {

      //main method
          public static void main (String[] args){
                
      
                String[] itemLetter = {"A", "B", "C", "D"};
              String[] itemName = {"Okinawa Soba", "Udon", "Ramen", "Coke"};
              String[] itemPrice = {"$5.45", "$4.49", "$7.79", "$0.95"};                      
                
                //JOptionPane.showMessageDialog(null, "Welcome to Walter's Ramen Shop.  " +
                //"Our menu is in English and our food is delicious.");
        //  JOptionPane.showInputDialog(itemPrice);
         
              sortLetter(itemLetter);
                 for(int j=0; j<itemLetter.length; j++){
                       System.out.printf(itemLetter[j] +"\t" + itemName[j] + "\t"+"\t"+ itemPrice[j] );
                       
                           if (j % 1 == 0)  // used to make separate lines
                              System.out.println("");  // prints out a blank line after 5 numbers



A      Okinawa Soba            $5.45
B      Udon            $4.49
C      Ramen            $7.79
D      Coke            $0.95

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WalterAPO

ASKER

Thank you for the help.  I was trying the %3s, but I didn't know how to put them all together.  This solution works great.   Please let me know how I can improve my programming.  I would like to start learning the right way using OOP, instead of top down programming.
:)

A couple of things to start you off in the right direction:

a. don't use parallel arrays, make a class to encapsulate the data that belong together.
b. prices should be double or float