Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java printf with tab \t

Posted on 2011-09-24
3
Medium Priority
?
301 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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

704 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