Solved

Java printf with tab \t

Posted on 2011-09-24
3
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 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