1

I am nearly done with my project (technically it's finished), but I am having a big problem with little detail such as output formatting. I am fairly new to JAVA and would apreciatte any help you can provide.

I need to output 2 arrays (String and Int) in some sort of table format. Example: England 4.6 USA 2.6 Japan 7.8 etc

I need exact spacing between the characters. I'll give you one part of my code: (I can apply the logic to the rest of the program)

    double beerStrenghts [] = new double [10];
    for(int x = 0; x < beerStrenghts.length; x++){
        beerStrenghts[x] = beerStrenghts()[x];
    }
    String beerName [] = new String [10];
    for(int x = 0; x < beerName.length; x++){
        beerName[x] = (beerName()[x]);
    }
    String lookup;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter search criteria. Min 3 characters.");
    lookup = keyboard.nextLine();
        while(lookup.length() < 3){
            System.out.println("Please enter at least 3 characters");
            lookup = keyboard.nextLine();
        }
        Formatter fmt = new Formatter();
    int indexInt;
    boolean found;
    for(int x = 0; x< beerName.length; x++){
            found = beerName[x].toUpperCase().contains(lookup.toUpperCase());
            if(found){
                fmt.format("%12s%3d",beerName[x], beerStrenghts[x]);
                System.out.println(fmt);
            }
    }

}

public static String [] beerName(){
String[] beerName = new String[10];
beerName[0] = "Heineken";
beerName[1] = "Bud Light";
beerName[2] = "Coors Light";
beerName[3] = "Leffe Blonde";
beerName[4] = "Budweiser";
beerName[5] = "Erdinger Non-Alcoholic";
beerName[6] = "Bud Premier Select";
beerName[7] = "Corona";
beerName[8] = "Barefoot Bohemian";
beerName[9] = "3 Monts"; 
return beerName;
}
public static double [] beerStrenghts(){
double beerStrenghts [] = new double [10];
beerStrenghts[0] = 4.0;
beerStrenghts[1] = 4.2;
beerStrenghts[2] = 4.3;
beerStrenghts[3] = 6.6;
beerStrenghts[4] = 5.0;
beerStrenghts[5] = 0.0;
beerStrenghts[6] = 7.4;
beerStrenghts[7] = 4.6;
beerStrenghts[8] = 4.0;
beerStrenghts[9] = 8.5;
return beerStrenghts;
4
  • 1
    What does you current output look like? and how exactly you want. Add some images maybe Commented Apr 1, 2016 at 17:39
  • 1
    This smells of a homework question... Commented Apr 1, 2016 at 17:47
  • 1
    Of course it's a homework question, but OP seems to have made a good-faith effort ... would be better if they said what specifically was wrong... Commented Apr 1, 2016 at 17:53
  • Why are you giving floating point values as an example if you want to output an integer? Commented Apr 1, 2016 at 17:58

3 Answers 3

1

You need to (re)read the javadoc for java.util.Formatter. In particular, it says that the formatting code d is for decimal integers. You are dealing with doubles, so the f code is probably more your style. (I'm just guessing though since you've been very light on details.)

Try something like

fmt.format("%-12s%3.1f",beerName[x], beerStrenghts[x]);
Sign up to request clarification or add additional context in comments.

3 Comments

%3.1f ?? That would just give 1decimal precision..coz he is using %3 that doesn't seem desirable
All of OP's beer strengths only had 1 digit after the decimal point. That's why I did %3.1f. 4.7 is 3 characters wide and 1 precision point. OP can easily adjust the precision and width if they want more space.
Agreed..i thought he wants 3 decimal points formatting..thus my answer has that :P
0

just change %3d to %.03for %.3f like :

fmt.format("%-12s %.03f", beerName[x], beerStrenghts[x]);

%d is used for formatting integers.

also have a look at StringBuilder if you want different locales i.e different ways of formatting for different regions. More about formatters with string builders here:https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Comments

0

Big thanks for all help guys. And apologies for not providing enough details... I am also new in stack overflow.

This is my college project indeed (small part of it). Unfortunately majority of solutions-problems haven't being covered during classes - but I am doing my own researches(going very very well - almost done, formatting is the last part) and though I'll give my shoot here :)

Thanks for java.until.formatter reference dcsohl - I'll definitely go through it.

The solution given is almost working but I cant figure out what is the problem this time. Current output while searching for "bud":

Bud Light 4.2 Bud Light 4.2Budweiser 5.0 Bud Light 4.2Budweiser 5.0Bud Premier Select 7.4

1 Comment

Welcome to StackOverflow! This looks like it belongs as an edit to your post. Look at the bottom left of your post for the edit button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.