what i am trying to do is to read this data in from a file,

1980 Aug	945	100	Allen

1983 Aug	962	100	Alicia

1984 Sep	949	100	Diana

1985 Jul	1002	65	Bob

1985 Aug	987	80	Danny

1985 Sep	959	100	Elena

1985 Sep	942	90	Gloria

1985 Oct	971	75	Juan

1985 Nov	967	85	Kate

1986 Jun	990	75	Bonnie

1986 Aug	990	65	Charley

1987 Oct	993	65	Floyd

1988 Sep	984	70	Florence

1989 Aug	986	70	Chantal

1989 Sep	934	120	Hugo

1989 Oct	983	75	Jerry

1991 Aug	962	90	Bob

1992 Aug	922	145	Andrew

1993 Aug	960	100	Emily

1995 Aug	973	85	Erin

1995 Oct	942	100	Opal

1996 Jul	974	90	Bertha

1996 Sep	954	100	Fran

1997 Jul	984	70	Danny

1998 Aug	964	95	Bonnie

1998 Sep	987	70	Earl

1998  Sep	964	90	Georges

1999 Aug	951	100	Bret

1999 Sep	956	90	Floyd

1999 Oct	987	70	Irene

2002 Oct	963	80	Lili

2003 Jul	979	80	Claudette

2003 Sep	957	90	Isabel

2004 Aug	972	70	Alex

2004 Aug	941	130	Charley

2004 Aug	985	65	Gaston

2004 Sep	960	90	Frances

2004 Sep	946	105	Ivan

2004 Sep	950	105	Jeanne

2005 Jul	992	65	Cindy

2005 Jul	930	130	Dennis

2005 Jul	929	135	Emily

2005 Aug	975	85	Irene

2005 Aug	902	150	Katrina

2005 Sep	960	100	Maria

2005 Sep	979	80	Nate

2005 Sep	976	80	Ophelia

2005 Sep	985	70	Phillipe

2005 Sep	897	150	Rita

2005 Sep	979	70	Stan

2005 Sep	987	65	Vince

2005 Sep	882	150	Wilma

2005 Sep	960	100	Beta

2005 Sep	979	75	Epsilon

2006 Aug	995	65	Ernesto

2006 Sep	972	80	Florence

2006 Sep	955	105	Gordon

2006 Sep	954	110	Helene

2006 Sep	985	75	Isaac

but what is getting me is i need to read all of the years in one, months is another, and so on. im going to read them into an array so im doing something like this

import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import java.io.File;
public class Hurricanes2
{
    public static void main(String [] args) throws IOException
    {
    
    Scanner inFile = new Scanner(new File("hurcdata2.txt"));
   

    
    for (int monthIndex = 0; monthIndex <= 58;)
    {
       
    String month[] = new String[58];
    month[monthIndex] = inFile.next(); 
    System.out.println(month[monthIndex]);
    monthIndex++;
    }
    
    for (int pressureIndex = 0; pressureIndex <= 58; pressureIndex++)
    {
    int pressure[] = new int[58];
    }
    
    for (int speedIndex = 0; speedIndex <= 58; speedIndex++)
    {
    int speed[] = new int[58];
    }
    
    for (int nameIndex = 0; nameIndex <= 58; nameIndex++)
    {
    String month[] = new String[58];
    }
}
    
}

i think that i need to use delimeters, but i am not familiar with them so if anyone can help thank you.

Lines 25, 30, and 35 are in the wrong spots. You need a broader scope for these variables. They will disappear outside of the for-loops they were created in. Declare and set aside space for your arrays at the top. Then read in at most a line at a time from the file, parse or convert the data if necessary and stick the data in the appropriate array. You should have ONE for-loop that ranges from 0 to 58.

create a Hurricane Object with fields year, month, presure and speed i.e.

public class Hurricane{
   private String name;
   private int year;
   private String month;
   private double speed;
   private double pressure;

   // add required setters and getter methods
   //...
}

Then you can create an array of Hurricane objects to hold each record from the file from which u can now process your data i.e.

Hurricane[] hurricanes = new Hurricane[58];

// to capture your input do this
// create index to read each line
int index = 0;
while(inFile.hasNextLine()){
    // you know that each line holds five items in a specific order
    Hurricane h = new Hurricane();
    h.setYear(inFile.nextInt());
    h.setMonth(inFile.next());
    h.setPressure(inFile.nextDouble());
    h.setSpeed(inFile.nextDouble());
    h.setName(inFile.next());
    
    // add hurricane to our array
    hurricanes[index++] = h;       // increment index after use
}

// by now u should have your required data
// so you can now do what you want with your data

if u get stuck hola back

create a Hurricane Object with fields year, month, presure and speed i.e.

public class Hurricane{
   private String name;
   private int year;
   private String month;
   private double speed;
   private double pressure;

   // add required setters and getter methods
   //...
}

Then you can create an array of Hurricane objects to hold each record from the file from which u can now process your data i.e.

Hurricane[] hurricanes = new Hurricane[58];

// to capture your input do this
// create index to read each line
int index = 0;
while(inFile.hasNextLine()){
    // you know that each line holds five items in a specific order
    Hurricane h = new Hurricane();
    h.setYear(inFile.nextInt());
    h.setMonth(inFile.next());
    h.setPressure(inFile.nextDouble());
    h.setSpeed(inFile.nextDouble());
    h.setName(inFile.next());
    
    // add hurricane to our array
    hurricanes[index++] = h;       // increment index after use
}

// by now u should have your required data
// so you can now do what you want with your data

if u get stuck hola back

Yes, very good point. A single array or type Hurricane would be better than several arrays, each containing one part of the hurricane, like the name, date, etc. You should create a Hurricane class. You might want to consider combining month and year into Date.

well i can only have a main method, we havent gone over having multiple in one program. pretty lame right? lol, but if anyone can advise the best way to go about doing this in one method, thats what i am going for.

well i can only have a main method, we havent gone over having multiple in one program. pretty lame right? lol, but if anyone can advise the best way to go about doing this in one method, thats what i am going for.

I think my post 2 is your best approach then:

// declare/set aside space for 5 arrays of the appropriate types of size 59.

// open file

for (int i = 0; i < 59; i++)
{
   // read from file into year[i];
   // read from file into month[i];
   // read from file into pressure[i];
   // read from file into speed[i];
   // read from file into name[i];
}

// close file

See post 3 for some commands on how to do lines 7 through 11 above. You'll have to modify since you have no Hurricane class.

I think my post 2 is your best approach then:

// declare/set aside space for 5 arrays of the appropriate types of size 59.

// open file

for (int i = 0; i < 59; i++)
{
   // read from file into year[i];
   // read from file into month[i];
   // read from file into pressure[i];
   // read from file into speed[i];
   // read from file into name[i];
}

// close file

See post 3 for some commands on how to do lines 7 through 11 above. You'll have to modify since you have no Hurricane class.

public static void main(String [] args) throws IOException
    {
    int year[] = new int[58];
    int yearIndex=0;
    String month[] = new String[58];
    int monthIndex = 0;
    int pressure[] = new int[58];
    int pressureIndex = 0;
    int speed[] = new int[58];
    int speedIndex = 0;
    String name[] = new String[58];
    int nameIndex = 0;
    
    Scanner inFile = new Scanner(new File("hurcdata2.txt"));
   
            while (inFile.hasNext())
        {   
            year[yearIndex] = inFile.nextInt();
            month[monthIndex] = inFile.next();
            pressure[pressureIndex] = inFile.nextInt();
            speed[speedIndex] = inFile.nextInt();
            name[nameIndex] = inFile.next();
            yearIndex++;
            monthIndex++;
            pressureIndex++;
            speedIndex++;
            nameIndex++;
        }
        inFile.close();
        System.out.println(year[yearIndex]);
        System.out.println(pressure[pressureIndex]);
        System.out.println(speed[speedIndex]);
        System.out.println(name[nameIndex]);
}
    
}

i think this is how you suggested, but i am still getting some errors. any advice?

just an update, but i have gotten it to work somewhat correctly with this code. im an now having problems trying to figure out how big my array is supposed to be and my index is messed up to where if i add index++ to my loop i get the wrong output. if anyone has any advice it is appreciated and thank you in advance.

public static void main(String [] args) throws IOException
    {
     
    
    int Index = 0;
    
    
    int year[] = new int[232];
    String month[] = new String[232];
    int pressure[] = new int[232];
    int speed[] = new int[232];
    String name[] = new String[232];
    

    Scanner inFile = new Scanner(new File("hurcdata2.txt"));
   
            while (inFile.hasNext())
        {   
            
            year[Index] = inFile.nextInt();
            month[Index] = inFile.next();
            pressure[Index] = inFile.nextInt();
            speed[Index] = inFile.nextInt();
            name[Index] = inFile.next();
            
       
            
        }
        inFile.close();
        
}

You keep writing over the same data because you don't change Index. Index++ should be at the end of the loop. You have 59 lines in your file (I assume those blanks in your first post aren't blank lines)? Defining the array as size 58 isn't going to work if you have 59. Maybe make it 60 to be safe, but 232 is overkill.

You only need one Index variable and it increments at the end of the loop. You should be displaying the data as you go so you see what exactly is going on.

You keep writing over the same data because you don't change Index. Index++ should be at the end of the loop. You have 59 lines in your file (I assume those blanks in your first post aren't blank lines)? Defining the array as size 58 isn't going to work if you have 59. Maybe make it 60 to be safe, but 232 is overkill.

You only need one Index variable and it increments at the end of the loop. You should be displaying the data as you go so you see what exactly is going on.

i got all that fixed up, now i am trying to get my wind speeds, out them into categorys, and calculate the average category. for example say i have 5 speeds that are 155+, i see what category they are, which is 5, and then i add all the numbers up and divide to get my average. im having difficulties. if you see a solution, your guidance is appreciated.

import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import java.io.File;
public class Hurricanes2
{
    public static void main(String [] args) throws IOException
    {
     
    
    int Index = 0;
    int year[] = new int[59];
    String month[] = new String[59];
    int pressure[] = new int[59];
    int speed[] = new int[59];
    String name[] = new String[59];
    
    int category[] = new int[59];

    

    Scanner inFile = new Scanner(new File("hurcdata2.txt"));
   
            while (inFile.hasNext())
        {   
            
            year[Index] = inFile.nextInt();
            month[Index] = inFile.next();
            pressure[Index] = inFile.nextInt();
            speed[Index] = inFile.nextInt();
            name[Index] = inFile.next();
            if(year[Index] != 0)
                Index++;
                        
            }
            
            inFile.close();
            
            //category
            int categorySpeed = 0;
            
            for (int counter = 0; counter <= 58;)
            {
            if(speed[counter] > 155)
            {
                category[counter] = 5;
            }
            
            else if(speed[counter] > 130)
            {
                category[counter] = 4;
            }
            
            else if(speed[counter] > 111)
            {
                category[counter] = 3;
            }
            
            else if(speed[counter] > 96)
            {
                category[counter] = 2;
            }
            else
            {
                category[counter] = 1;
            }

            
            category[counter] = categorySpeed;
            System.out.println(category[counter]);
            counter++;
            }

now i am trying to get my wind speeds, out them into categorys, and calculate the average category.

So it seems like you have your steps figured out. It looks like you got your wind speeds, put them into their categories. . but did not calculate the average. The formula to calculate the average should be something like this:

[ Category (Ex: 5) * number of items in that category ] + [ Next Category * Number of items in that category ] . . . / total items

Although, in my opinion, that kind of algorithm is pretty pointless anyway. The better way to do it would be to add up all of the wind speeds, divide by the total number of hurricanes, then see what category that speed fits into. Whatever category it fits in is the average category.

alright, i have most of the code. i have been working on it for a while. im pretty desperate. if anyone can help me to make this a working program it would be a miracle.

import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import java.io.File;
public class Hurricanes2
{
    public static void main(String [] args) throws IOException
    {
     
    
    int Index = 0;
    int year[] = new int[59];
    String month[] = new String[59];
    int pressure[] = new int[59];
    int speed[] = new int[59];
    String name[] = new String[59];
    
    int category[] = new int[59];

    int speedAverage = 0;
    int speedSum = 0;
    
    int categorySum = 0;
    int categoryAverage = 0;
    int categorySpeed = 0;
    
    int pressureSum = 0;
    int pressureAverage = 0;
    
    Scanner inFile = new Scanner(new File("hurcdata2.txt"));
   
            while (inFile.hasNext())
        {   
            
            year[Index] = inFile.nextInt();
            month[Index] = inFile.next();
            pressure[Index] = inFile.nextInt();
            speed[Index] = inFile.nextInt();
            name[Index] = inFile.next();
            if(year[Index] != 0)
                Index++;
                        
            }
            
            inFile.close();
            
            //category
            
            
            for (int counter = 0; counter <= 58;)
            {
            if(speed[counter] > 155)
            {
                category[counter] = 5;
            }
            
            else if(speed[counter] > 130)
            {
                category[counter] = 4;
            }
            
            else if(speed[counter] > 111)
            {
                category[counter] = 3;
            }
            
            else if(speed[counter] > 96)
            {
                category[counter] = 2;
            }
            else
            {
                category[counter] = 1;
            }

            categorySum += category[counter];
            categoryAverage = categorySum/ 58;
            counter++;
            }
        
            //speed
            for(int counter1 = 0; counter1 <= 60;)
            {
                
                speedSum += speed[Index];
                speedAverage = speedSum/58;
                counter1++;
                
            }
            
            //pressure
            for (int counter2 = 0; counter2 <= 60;)
            {
                pressureSum += pressure[Index];
                pressureAverage = pressureAverage/58;
                counter2++;
            }
        
}
    
}
for (int counter2 = 0; counter2 <= 60;)
{
pressureSum += pressure[Index];
pressureAverage = pressureAverage/58;
counter2++;
}

Should be

for (int counter2 = 0; counter2 <= 60; counter2++)
{
pressureSum += pressure[Index];
pressureAverage = pressureAverage/58;
}

And the main problem is that nobody is helping because you didn't mentioned exactly what isn't working or where your errors are, or what you are even trying to do at this point, or anything specific that you need help with. I'd love to help but I don't really know what you're having trouble with. Specifics.

for (int counter2 = 0; counter2 <= 60;)
{
pressureSum += pressure[Index];
pressureAverage = pressureAverage/58;
counter2++;
}

Should be

for (int counter2 = 0; counter2 <= 60; counter2++)
{
pressureSum += pressure[Index];
pressureAverage = pressureAverage/58;
}

And the main problem is that nobody is helping because you didn't mentioned exactly what isn't working or where your errors are, or what you are even trying to do at this point, or anything specific that you need help with. I'd love to help but I don't really know what you're having trouble with. Specifics.

my bad, sometimes i loose my thought. i just start typing. what i am trying to do is create averages from those arrays. im not quite sure what my problem is, thats whats hard to describe. i am getting an array out of bounds error, but it doesnt make any sense because im using the same dimensions that i am using through out the program and none of those have errors. so im really trying to figure out whats wrong, and then fix it.

i have got all of the errors out, but my averages just plain out arent correct. im trying to figure out what the problem is. if someone sees something obvious please let me know. thank you.

import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import java.io.File;
public class Hurricanes2
{
    public static void main(String [] args) throws IOException
    {
     
   int Index = 0; 
   int Index2 = 0;
  
    int year[] = new int[60];
    String month[] = new String[60];
    int pressure[] = new int[60];
    int speed[] = new int[60];
    String name[] = new String[60];
    
    int category[] = new int[60];

    int speedAverage = 0;
    int speedSum = 0;
    
    int categorySum = 0;
    int categoryAverage = 0;
    int categorySpeed = 0;
    
    int pressureSum = 0;
    int pressureAverage = 0;
    
    Scanner inFile = new Scanner(new File("hurcdata2.txt"));
   
            while (inFile.hasNext())
            {
                
            year[Index] = inFile.nextInt();
            month[Index] = inFile.next();
            pressure[Index] = inFile.nextInt();
            speed[Index] = inFile.nextInt();
            name[Index] = inFile.next();
            Index++;
            }   
         
            
            inFile.close();
            
            //category
            
            
            for (int counter = 0; counter <= 60;counter++)
            {
            if(speed[Index] > 155)
            {
                category[Index2] = 5;
            }
            
            else if(speed[Index] > 130)
            {
                category[Index2] = 4;
            }
            
            else if(speed[Index] > 111)
            {
                category[Index2] = 3;
            }
            
            else if(speed[Index] > 96)
            {
                category[Index2] = 2;
            }
            else
            {
                category[Index2] = 1;
            }

            categorySum += category[Index2];
            categoryAverage = categorySum/ 60;
            
            }
        
            //speed
            for(int counter = 0; counter <= 60; counter++)
            {
                
                speedSum += speed[Index];
                speedAverage = speedSum/60;
                
                
            }
            
            //pressure
               
            for (int counter = 0; counter <= 60; counter++)
           {    
                pressureSum += pressure[Index];
                pressureAverage = pressureAverage/60;
   
          }
          
          System.out.println(categoryAverage);
          System.out.println(speedAverage);
          System.out.println(pressureAverage);
        
}
    
}

First, if you haven't already, after you've read in your data, you need to display it to make sure it's reading in correctly. You also need to make sure that the number of elements you are reading in equals the number you are dividing by to get the average. Also realize that if you divide an integer by an integer with the / operator, it's going to truncate. 7 / 2 = 3, not 3.5. If you want 3.5, you need different types. And as Best Jew Since JC says, specifics. "Aren't correct" is vague. What are the real averages? What averages do you get?

It's harmless, but you should really take your averages AFTER the loop is over, not inside the loop. As I said, it's harmless. All that happens is a bunch of meaningless intermediate calculations that get overwritten, but cause no damage. But still, take the division out of the loop.

All that said, your REAL problem is that you have a loop control variable called "counter", but you use "Index" and "Index2" as your index variables inside of the loop. Use "counter" instead.

first off, thanks vernondozier. you have helped me multiple times. i took the averages out of the loop like so:

for (int counter = 0; counter <= 59;counter++)
            {
            if(speed[counter] > 155)
            {
                category[counter] = 5;
            }
            
            else if(speed[counter] > 130)
            {
                category[counter] = 4;
            }
            
            else if(speed[counter] > 111)
            {
                category[counter] = 3;
            }
            
            else if(speed[counter] > 96)
            {
                category[counter] = 2;
            }
            else
            {
                category[counter] = 1;
            }
            
            categorySum += category[counter];
            
            
            }
        
            //speed
            for(int counter = 0; counter <= 59; counter++)
            {
                
                speedSum += speed[counter];
                
                
                
            }
            
            //pressure
               
            for (int counter = 0; counter <= 59; counter++)
           {    
                pressureSum += pressure[counter];
                
   
          }
          categoryAverage = categorySum/ 59;
          speedAverage = speedSum/59;
          pressureAverage = pressureAverage/59;
          
          System.out.println(categoryAverage);
          System.out.println(speedAverage);
          System.out.println(pressureAverage);
        
}
    
}

i haven't exactly figured out what my averages should be, but what i get when i run the program is:

1
91
0

which couldn't possibly be right. MAYBE the 91, but the other two couldnt be.

i did what you said and tested all the data that is being read in, and it is all right. i went through and tried each array by putting a println(array[counter]); in each loop, and they all outputted data that made sense. im not sure where the error could be if all the data is good to that point.

Get some trial data that's easy to calculate and that has only 4 or 5 records, not 58 or 59 or 60 or whatever. You need known results that you can quickly check against. Fix the code so it works with this small amount of data, then when it's all fixed, go back to your original data file. You're one off on the data count, whichever it is. You're going through a loop 60 times and then dividing by 59. If you have 59 elements, your indexes are 0 to 58, not 0 to 59. You design your loop to correspond to your valid indexes, which you design around the number of values in the data file.

Look at line 52 compared to lines 50 and 51 and see if it makes sense to you. That's at least one problem.

Get a nice easy input file, like speeds of 60, 80, 100, 120, 140, and 160. It's really easy to average to 110 and it gets you into a few different categories. You can figure out the correct results on paper, then compare. 60 or so fairly random data points obscures things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.