0

I am supposed to print out the information that I just read from a text file and what I actually print is all "null" and zeros.

Here is what my Text File look like

1995    Jun 987 65  Allison
1995    Jul 973 85  Erin
1995    Aug 929 120 Felix
1995    Aug 968 95  Humberto
1995    Aug 965 95  Iris
1995    Aug 945 130 Luis
1995    Sep 949 100 Marilyn
1995    Sep 987 65  Noel
1995    Sep 916 130 Opal
1995    Oct 956 100 Roxanne
1995    Oct 972 75  Tanya
1996    Jul 960 100 Bertha
1996    Jul 985 75  Cesar
1996    Aug 989 70  Dolly
1996    Aug 933 125 Edouard
1996    Aug 946 105 Fran
1996    Sep 935 120 Hortense
1996    Sep 960 100 Isidore
1996    Oct 960 100 Lili
1996    Nov 983 65  Marco
1997    Jul 986 65  Bill
1997    Jul 984 70  Danny
1997    Sep 946 110 Erika
1998    Aug 954 100 Bonnie
1998    Aug 960 70  Danielle
1998    Aug 985 80  Earl
1998    Sep 937 135 Georges
1998    Sep 975 80  Ivan
1998    Sep 969 90  Jeanne
1998    Sep 970 90  Karl
1998    Oct 995 65  Lisa
1998    Oct 905 155 Mitch
1998    Nov 979 75  Nicole
1999    Aug 944 125 Bret
1999    Aug 942 120 Cindy
1999    Aug 962 90  Dennis
1999    Sep 921 135 Floyd
1999    Sep 930 130 Gert
1999    Oct 958 95  Irene
1999    Oct 979 85  Jose
1999    Nov 933 135 Lenny
2000    Aug 950 110 Alberto
2000    Aug 991 75  Debby
2000    Sep 985 70  Florence
2000    Sep 981 70  Gordon
2000    Sep 943 120 Isaac
2000    Sep 975 80  Joyce
2000    Sep 939 120 Keith
2000    Oct 965 85  Michael
2001    Sep 968 105 Erin
2001    Sep 962 100 Felix
2001    Sep 975 70  Gabrielle
2001    Sep 970 90  Humberto
2001    Oct 948 125 Iris
2001    Oct 982 70  Karen
2001    Oct 933 120 Michelle
2001    Nov 986 65  Noel
2001    Nov 973 80  Olga
2002    Sep 960 80  Gustav
2002    Sep 934 110 Isidore
2002    Sep 980 75  Kyle
2002    Sep 938 125 Lili
2003    Jul 979 80  Claudette
2003    Jul 1000    65  Danny
2003    Aug 986 65  Erika
2003    Aug 939 125 Fabian
2003    Sep 915 145 Isabel
2003    Sep 969 90  Juan
2003    Sep 952 110 Kate
2004    Jul 957 105 Alex
2004    Aug 941 130 Charley
2004    Aug 964 95  Danielle
2004    Aug 935 125 Frances
2004    Aug 985 65  Gaston
2004    Sep 910 145 Ivan
2004    Sep 950 105 Jeanne
2004    Sep 938 125 Karl
2004    Sep 987 65  Lisa
2005    Jul 991 65  Cindy
2005    Jul 930 130 Dennis
2005    Jul 929 140 Emily
2005    Aug 970 90  Irene
2005    Aug 902 150 Katrina
2005    Sep 962 100 Maria
2005    Sep 979 80  Nate
2005    Sep 976 75  Ophelia
2005    Sep 985 70  Philippe
2005    Sep 895 155 Rita
2005    Oct 977 70  Stan
2005    Oct 988 65  Vince
2005    Oct 882 160 Wilma
2005    Oct 962 100 Beta
2005    Nov 981 75  Epsilon
2006    Aug 992 65  Ernesto
2006    Sep 974 80  Florence
2006    Sep 955 105 Gordon
2006    Sep 955 105 Helene
2006    Sep 985 75  Isaac
2007    Aug 905 150 Dean
2007    Aug 929 150 Felix
2007    Sep 985 80  Humberto
2007    Sep 988 65  Karen
2007    Sep 990 70  Lorenzo
2007    Oct 980 70  Noel
2008    Jul 952 110 Bertha
2008    Jul 963 85  Dolly
2008    Aug 941 135 Gustav
2008    Aug 977 75  Hanna
2008    Sep 935 125 Ike
2008    Sep 984 70  Kyle
2008    Oct 958 115 Omar
2008    Nov 970 85  Paloma
2009    Aug 943 115 Bill
2009    Sep 958 105 Fred
2009    Nov 975 90  Ida
2010    Jun 946 95  Alex
2010    Aug 942 115 Danielle
2010    Aug 927 125 Earl
2010    Sep 924 135 Igor
2010    Sep 948 120 Julia
2010    Sep 956 110 Karl
2010    Sep 982 75  Lisa
2010    Oct 976 75  Otto
2010    Oct 981 90  Paula
2010    Oct 977 85  Richard
2010    Oct 989 65  Shary
2010    Oct 985 85  Tomas
2011    Aug 942 105 Irene
2011    Aug 942 120 Katia
2011    Sep 983 70  Maria
2011    Sep 994 65  Nate
2011    Sep 940 120 Ophelia
2011    Sep 976 80  Philippe
2011    Oct 966 100 Rina
2012    Jun 974 75  Chris
2012    Aug 973 85  Ernesto
2012    Aug 965 95  Gordon
2012    Aug 965 70  Isaac
2012    Aug 970 90  Kirk
2012    Aug 968 70  Leslie
2012    Sep 964 100 Michael
2012    Sep 978 80  Nadine
2012    Oct 969 80  Rafael
2012    Oct 940 100 Sandy
2013    Sep 979 80  Humberto
2013    Sep 983 75  Ingrid
2014    Jul 973 85  Arthur
2014    Aug 998 70  Bertha
2014    Aug 965 75  Cristobal
2014    Sep 955 105 Edouard
2014    Oct 983 70  Fay
2014    Oct 940 125 Gonzalo
2015    Aug 960 110 Danny
2015    Aug 986 75  Fred
2015    Sep 931 135 Joaquin
2015    Nov 980 75  Kate

The expected output

Year     Name     Pressure     wind Speed
1995    Allison       987        65 
1995    Erin,         973            85
1995    Felix         929        120    
1995    Humberto      968        95 
1995    Iris          965        95 
1995    Luis          945        130
public class HurricaneSelector
{
    public static void main(String[] args) throws IOException
    {
        Scanner in = new Scanner (System.in);
        File fileName = new File ("hurricanedata.txt");
        Scanner inFile = new Scanner(fileName);

        int counter = 0;

        while (inFile.hasNextLine()) 
        {
            inFile.nextLine();
            counter++;
        }

        int [] year = new int[counter];
        String [] month = new String[counter];
        String [] name = new String[counter];
        int [] pressure = new int[counter];
        int [] windSpeed = new int[counter];
        int index = 0; 
        while (inFile.hasNext())
        {
            year[index] = inFile.nextInt();
            month[index] = inFile.nextLine();
            pressure[index] = inFile.nextInt();
            windSpeed[index] = inFile.nextInt();
            name[index] = inFile.nextLine();
            index++;
        }
        for (int n = 0; n < year.length; n++)
        {
            System.out.println(year[n] + "     " + month[n] + "     " + name[n] + "     " + windSpeed[n]);
        }     
    } 
}
Andreas
  • 138,167
  • 8
  • 112
  • 195
  • See also: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/5221149) – Andreas Oct 28 '19 at 00:24

1 Answers1

3

After while (inFile.hasNextLine()) the file stream is exhausted and will need to be re-read from the beginning again.

You should close the file first and then re-open it.

inFile.close ();
inFile = new Scanner(fileName);
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • Thank you for helping, I did what you said and now it tells me " java.util.InputMismatchException", I do not understand this. How am I supposed to fix this? – Liu Steven Oct 28 '19 at 00:35
  • the type of input that you are trying to read e.g. `nextInt` does not match the file. As each line contain all the information that you want I would use just one `readLine` per line and then use `String::split`` to get each individual value. – Scary Wombat Oct 28 '19 at 00:39
  • I really appreciate your help, but I do not really know how to use a string split method, I have never used them before. Can you give me an example? Thank you so much. – Liu Steven Oct 28 '19 at 00:45
  • How about this for a deal - you try it, and when you get stuck you post your code, and someone will help? Stackoverflow has literally thousand of examples. – Scary Wombat Oct 28 '19 at 00:47