Solved

Java printf with tab \t

Posted on 2011-09-24
3
251 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:WalterAPO
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 36592200
There are lots of things to say about your code, but the first things is - don't use tabs. Try
for(int i = 0;i < itemName.length;i++) {	
	    System.out.printf("%-3s%-24s%-8s\n", itemLetter[i], itemName[i], itemPrice[i]);
	}

Open in new window

0
 

Author Closing Comment

by:WalterAPO
ID: 36594292
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36595460
:)

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
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now