Results 1 to 9 of 9
  1. #1
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default Few errors with for loop/printf

    Ok so this is the purpose of the program.
    1. Print a series of years
    2. Print a series of names, 1 name matches 1 year, etc.
    3. Print a series of categories; categories are determined by windspeed(part 4). each category matches a name.
    4. Print a series of windspeed.Matches to each row
    5. Print a series of pressure. Matches to each row
    (There's more I have to do that I'll figure out later but I think printing the values is important).
    So the output should look something like this:
    ERRORS: The data in the last 3 columns do not match in spacing. Meaning, if you look at 1980, go across to category, it is right where it should be but if you go down to 1983, it is spaced 1 more time.

    Java Code:
    Years	Hurricance	Category	Pressure(mb)	Wind Speed(mph)
    1980	Allen                  3                 945             115.077945
    1983	Alicia                  3                 962             115.077945
    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class Hurricanes2
    
    {
        public static void main(String[] args) throws IOException
        {    
          int [ ] year = new int[59];
          String [ ] month = new String[59];
          int [ ] pressure = new int[59];
          int [ ] speed = new int[59];
          double [ ] speedmph = new double[59];
          int [ ] category = new int[59];
          int cat1 = 0;
          int cat2 = 0;
          int cat3 = 0;
          int cat4 = 0;
          int cat5 = 0;
          String [ ] name = new String[59];
          int average = 0;
          File fileName = new File("hurcdata2.txt");
          Scanner inFile = new Scanner(fileName);
          System.out.print("Years\t\tHurricance\tCategory\tPressure(mb)\tWind Speed(mph)");
          System.out.println();
          for(int x = 0; x < 59; x++)
          {
              year[x] = inFile.nextInt();
              month[x] = inFile.next();
              pressure[x] = inFile.nextInt();
              speed[x] = inFile.nextInt();
              name[x] = inFile.nextLine();
              speedmph[x] = (int)speed[x] * 1.15077945;
              average = 
              if((speed[x] <= 82) && (speed[x] >= 64))
              {
                  category[x] = 1;
                  cat1++;
              }
              else if((speed[x] <= 95) && (speed[x] >= 83))
              {
                  category[x] = 2;
                  cat2++;
              }
              else if((speed[x] <= 113) && (speed[x] >= 96))
              {
                  category[x] = 3;
                  cat3++;
              }
              else if((speed[x] <= 135) && (speed[x] >= 114))
              {
                  category[x] = 4;
                  cat4++;
              }
              else
              {
                  category[x] = 5;
                  cat5++;
              }
              System.out.printf("%-15d%2s%15s%20d%21.0f\n", year[x], name[x], category[x], pressure[x], speedmph[x]);
              
              
              
            }
             System.out.println(average);
          
          
          inFile.close();
          
              
          
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code is missing something here:
    Java Code:
    average =  // ?? missing something here
    Also, can you post the data file, also using code tags just like you did for the code?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Nevermind, I see the problem. %2s is way to small a string size for the hurricane name. Make it bigger and go through the format String to make sure that the proper number and format specifier matches with the data. I'm betting that %2s was for a month column that was removed.

  4. #4
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    The spacing isn't ligning up because the names are different, so the second one, its pushing everything to the right one. You can use justify, so:

    System.out.println("%-15d%2s%15s%20d%21.0f\n"+year[x]+Format.justify('l',name[x],23)+category[x]+pressure[x]+speedmph[x]);

    BTW, i dont know what %-15d%2s%15s%20d%21.0f\n is

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    rp181, I know that you mean well, but you may not wish to answer a question about printf if you're not familiar with its associated format strings.

    To the OP, my suggestion is the correct one.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For example, here's some data that uses two format Strings, one with a String format that's too small:

    Java Code:
    public class Fubar2
    {
      public static void main(String[] args)
      {
        String[][] data =
        {
            {"2001", "Joe Schmo", "3.14159265"},
            {"2002", "Frank Taglianicci", "4"},
            {"2003", "Tai Bo", String.valueOf(Math.E)}
        };
    
        String formatString1 = "%-6d%2s%10.5f%n";
        String formatString2 = "%-6d%17s%10.5f%n";
        
        for (String[] datum : data)
        {
          int year = Integer.parseInt(datum[0]);
          String name = datum[1];
          double value = Double.parseDouble(datum[2]);
          System.out.printf(formatString1, year, name, value);
        }
        System.out.println();
        for (String[] datum : data)
        {
          int year = Integer.parseInt(datum[0]);
          String name = datum[1];
          double value = Double.parseDouble(datum[2]);
          System.out.printf(formatString2, year, name, value);
        }
    
      }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rp181 View Post
    BTW, i dont know what %-15d%2s%15s%20d%21.0f\n is
    This is a format String that formats the data that follows. It will place a decimal number left justified and give it a 15 char width, a String right justified, 2 char width, String r justified, 15 char width, decimal r justified 20 char, and a floating point right justified, 21 char width, no chars after decimal point (truncated).

  8. #8
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    Ok how do I add all of the data in an INT array and then divide by the total?
    Also, how would I print the max of windspeed and min of windspeed WITHOUT java max/min?
    Current code:
    Average says 0;
    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class Hurricanes2
    
    {
        public static void main(String[] args) throws IOException
        {    
          int [ ] year = new int[59];
          String [ ] month = new String[59];
          int [ ] pressure = new int[59];
          int [ ] speed = new int[59];
          double [ ] speedmph = new double[59];
          int [ ] category = new int[59];
          int cat1 = 0;
          int cat2 = 0;
          int cat3 = 0;
          int cat4 = 0;
          int cat5 = 0;
          String [ ] name = new String[59];
          int average = 0;
          File fileName = new File("hurcdata2.txt");
          Scanner inFile = new Scanner(fileName);
          System.out.print("Years\tHurricance\tCategory\tPressure(mb)\tWind Speed(mph)");
          System.out.println();
          for(int x = 0; x < 59; x++)
          {
              year[x] = inFile.nextInt();
              month[x] = inFile.next();
              pressure[x] = inFile.nextInt();
              speed[x] = inFile.nextInt();
              name[x] = inFile.nextLine();
              speedmph[x] = (int)speed[x] * 1.15077945;
              average += category[x]/59;
              if((speed[x] <= 82) && (speed[x] >= 64))
              {
                  category[x] = 1;
                  cat1++;
              }
              else if((speed[x] <= 95) && (speed[x] >= 83))
              {
                  category[x] = 2;
                  cat2++;
              }
              else if((speed[x] <= 113) && (speed[x] >= 96))
              {
                  category[x] = 3;
                  cat3++;
              }
              else if((speed[x] <= 135) && (speed[x] >= 114))
              {
                  category[x] = 4;
                  cat4++;
              }
              else
              {
                  category[x] = 5;
                  cat5++;
              }
              System.out.printf("%d%7s%15s%20d%23f\n", year[x], name[x], category[x], pressure[x], speedmph[x]);
              
              
              
            }
             System.out.println(average);
          
          
          inFile.close();
          
              
          
        }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    um... you're welcome?

    I wish you much luck solving the new problem.
    Last edited by Fubarable; 03-12-2009 at 10:15 PM.

Similar Threads

  1. Printf error
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 06:59 AM
  2. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  3. printf error with int and double
    By o0n1 in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 08:58 PM
  4. Demonstration of printf() method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:37 PM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM

Posting Permissions

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