Solved

Java printf with tab \t

Posted on 2011-09-24
3
264 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 104
Is there a simpler dropbox system? 10 44
java imports not found 4 47
running on tomcat not jboss eap 7.0 3 28
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

679 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