Jump to content

Help with Reading and printing a file

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Animus

Animus

    Newbie

  • Members
  • PipPip
  • 26 posts
am i doing this thing right?


public static void main(String[] args)throws Exception {

      File myFile=new File("try.txt");

        if(myFile.exists()){

            BufferedReader reader=new BufferedReader(new FileReader(myFile));//prepare stream


            


                //get limit

                int limit=Integer.parseInt(reader.readLine());


                //read limit

                Student list[]=new Student[limit];


               

                

                //read content

                for(int i=0; i<limit; i++){

                    list[i].name.First=reader.readLine();

                    list[i].name.Midle=reader.readLine();

                    list[i].name.Last=reader.readLine();

                    list[i].chairLocation.Column=Integer.parseInt(reader.readLine());

                    list[i].chairLocation.Row=Integer.parseInt(reader.readLine());

                    list[i].course=reader.readLine();

                    list[i].age=Integer.parseInt(reader.readLine());

                }


                //print contents

                for(int i=0; i<limit; i++){

                    JOptionPane.showMessageDialog(null,"Seated at"+" "+"Column:"+list[i].chairLocation.Column+" "+"Row:"+list[i].chairLocation.Row+"\n"+"is"+

    "\n"+"Name:"+list[i].name.First + " "+list[i].name.Midle+ " "+list[i].name.Last+"\n"+"Age:"+list[i].age+"\n"+"Course:"+list[i].course);

           

}

        }

    }

}


im trying to make an I/O that reads and searches the file the prints out the objects within the file. but i cant compile it cause of a numberFormat Exception.
i dont know what im wrong here. and im just studying file manipulation.
can somebody help out?

heres the text file:

Jaygee Marie

Maiso

Namata

23

ACT

1

1


#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
The first read you're performing is trying to parse an Integer (int limit=Integer.parseInt...) when the first line in your file is a string and contains no integers (Jaygee Marie). You must read the values in the order they appear in the file.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
You could read the lines from the file into a String variable first and test its contents before trying to convert it to an int value.

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Why would you put the limit at the end of the file?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
Animus

Animus

    Newbie

  • Members
  • PipPip
  • 26 posts
sry its been a while.
ok, i figured out how to assign the objects in the file to the classes and print them.
now i have a bit wee simple problem that i bugs me.
i want to print out only the student with the corresponding column and row
Example
i input
column:1
row: 1
i get
Student Name : Jaygee Marie Namata
Seated at Column: 1 Row:1
Course: ACT
Age 23
the problem is it keeps printing the whole student list.
how the hell am i gonna modify the search algo for it??


public static void main(String[] args)throws Exception {

        String key,key2;

        int column;

        int row;

      File myFile=new File("try.txt");

        if(myFile.exists()){

            BufferedReader reader=new BufferedReader(new FileReader(myFile));//prepare stream

    //get array size

            String sec=reader.readLine();

            if(sec.equals("Student")){

                int limit=Integer.parseInt(reader.readLine());

                 //reads the size of the array

                Student list[]=new Student[limit];


    

            

                //initialize file

                for(int i=0; i<list.length; i++){

                    list[i]=new Student();

                }

                //read content and put into classes

                for(int i=0; i<list.length; i++){

                    list[i].chairLocation.Column=Integer.parseInt(reader.readLine());

                    list[i].chairLocation.Row=Integer.parseInt(reader.readLine());

                    list[i].name.First=reader.readLine();

                    list[i].name.Midle=reader.readLine();

                    list[i].name.Last=reader.readLine();

                    list[i].course=reader.readLine();

                    list[i].age=Integer.parseInt(reader.readLine());


       //input for search          

   key = JOptionPane.showInputDialog ( "Please Enter Column Number" );

    key2 = JOptionPane.showInputDialog ( "Please Enter Row Number" );



  if( key == null && key2 == null){

       return;}



            column=Integer.parseInt(key);

            row=Integer.parseInt(key2);

            //search algo for input

            for (int b=0; b<list.length; b++){

                    

                  if(column == list[b].chairLocation.Column && row == list[b].chairLocation.Row)

                


                //print contents

               for(int t=0; t<limit; t++){

                    print(list[b]);

               }

                }

          }


}

    }

    }


  public static void print(Student list) {

      System.out.println("Student Name : " + list.name.First+ " " + list.name.Last);

        System.out.println("Seated at Column: " + list.chairLocation.Column + " " + "Row:"+list.chairLocation.Row );

        System.out.println("Course: " + list.course);

        System.out.println("Age " + list.age);



            }




#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

it keeps printing the whole student list.
Please explain what you want the code to do differently.
You have coded a loop to print out the whole list:
    //print contents

               for(int t=0; t<limit; t++){

                    print(list[b]);

               }
Leave off the for loop if you only want one printed.

The formatting of your code is not proper. The {}s should be aligned vertically so you can easily see the code structure. Your code is messy making it hard to read and understand the logic.
You need to add {}s in your if statements to define what code is part of the if and what is not.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users