Results 1 to 6 of 6
  1. #1
    apcshelp is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Help with formatting arrays

    I am almost done with this program but I cannot find a way to print the the arrays in an organized table. I tried using a for statement for each array and the program compiles but no input screen appears to accept used input. Any help would be appreciated. I am trying to make it look like this image:
    Help with formatting arrays-java.jpg

    Here is what I have so far:
    Java Code:
    import java.util.Scanner;
    
    class AnnualClimate1
    {
    
    public static void main (String [ ] args)
    {
    
        // define city and state
        Scanner in = new Scanner(System.in);
        String city = " Tallahassee ";
        String state = " Florida ";
       //input data into string
        String month [] ={"Jan", "Feb", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec." };
        double temperature [] ={51.8, 54.8, 61.1, 66.4, 74.4, 80.4, 82.4, 82.1, 78.9, 69.1, 60.4, 53.7,};        //initialize with Fahrenheit values
        double precipitation [] ={5.4, 4.6, 6.5, 3.6, 5.0, 6.9, 8.0, 7.0, 5.0, 3.3, 3.9, 4.1};  //initialize with inches values
        double averageTemp = 0.0, totalPrecip = 0.0;
        double sumTemp = 0;
        String tempLabel = "F";   //label
        String precipLabel = "cm"; //label
    
        //input
        System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): ");
        String tempChoice = in.next();
    
        //temp conversions
        if(tempChoice.equalsIgnoreCase("F"))
        {
            for( int n = 0; n < temperature.length; n++)
            {  
                tempLabel = "(F)";
                sumTemp += temperature[n];
                averageTemp = sumTemp/12;
                //System.out.println(temperature[n]);
            }
        }
        if(tempChoice.equalsIgnoreCase("C"))
        {
            for( int n = 0; n < temperature.length; n++)
            {  
                 tempLabel="(C)";
                sumTemp += temperature[n];
                averageTemp = ((sumTemp/12)-32)*5/9;
                //System.out.println(temperature[n]);
            }
        }
        System.out.print("Choose the precipitation scale (i = inches, c = centimeteres): ");
        String precipChoice = in.next();
        //precipitation conversion
        if(precipChoice.equalsIgnoreCase("c")){
           for( int n = 0; n < precipitation.length; n++)
            {  
            precipLabel="(cm)";
            totalPrecip += precipitation[n] * 2.54;
            //System.out.println(precipitation[n]);
        } 
    }
    if(precipChoice.equalsIgnoreCase("i")){
            for( int n = 0; n < precipitation.length; n++)
            {  
                precipLabel ="(in)";
                totalPrecip += precipitation[n];
                //System.out.println(precipitation[n]);
            }
    }
        //output
        System.out.printf("%30s\n", "Climate Data");
        System.out.printf("%19s\n", "Location: " + city +", " + state);
        System.out.printf("%18s", "Temperature",tempLabel,"Precipitation",precipLabel);
        System.out.println("***************************************************");
        for (int n = 0; n<=11; n++)
        {
            System.out.printf(month[n], temperature[n], precipitation[n]);
        }
        System.out.println("***************************************************");
        System.out.println(averageTemp);
        System.out.println(totalPrecip);
    }
        }//end main
    //end Annual Climate
    Last edited by apcshelp; 11-20-2013 at 12:04 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with formatting arrays

    no input screen appears to accept used input
    The Scanner class's methods read its input from the keyboard on a command prompt screen. There isn't a special screen.
    If you want to present a dialog box for the user to enter data in, look at the JOptionPane class.

    What does the program's output look like? When pasting the output be sure to wrap it in code tags to preserve its formatting.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    apcshelp is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with formatting arrays

    I made some progress my adding in a third for statement which now prints the info from each array. However, I am still having trouble setting up the formatting of the table

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with formatting arrays

    If you want any help, please post the output and point out the problems with it.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    apcshelp is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with formatting arrays

    Help with formatting arrays-help.jpgHelp with formatting arrays-help.jpg

    The program prints the months array one after another but not the precipitation and temperature arrays.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with formatting arrays

    I would like to copy some of the program's output so I could paste it here, but that is not possible to do with an image.

    Check how you have coded the args for the printf() statements. There are rules for how to code them that you need to read.
    Go read the API doc for the method in the PrintStream class
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. formatting jsp tags
    By mahdijalilvand in forum New To Java
    Replies: 1
    Last Post: 04-18-2013, 03:46 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. Formatting String
    By ersachinjain in forum Advanced Java
    Replies: 13
    Last Post: 10-21-2009, 09:26 PM
  4. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 07:11 PM
  5. formatting String
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 07:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •