Jump to content

Here are some documentation to help

 

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

 

Also NewFile should be separated.

File abFile = new File("myaddresses.txt");Scanner s = new Scanner(abFile);
Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3775859
Share on other sites

Link to post
Share on other sites

 

Here are some documentation to help

 

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

 

Also NewFile should be separated.

File abFile = new File("myaddresses.txt");Scanner s = new Scanner(abFile);

i don't really get what they are saying 

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3775879
Share on other sites

Link to post
Share on other sites

i don't really get what they are saying 

 

I also showed you the code to create the scanner. Then you just need to use it.

 

What does your file contain? It's hard to tell you how to read that information without knowing that.

 

Here's an example of reading numbers from a file. 

Scanner sc = new Scanner(file);while (sc.hasNextLine()) {    int i = sc.nextInt();    System.out.println(i);}sc.close();

You can do it with strings or any other types as well (it shows you all the methods in the documentation).

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3775917
Share on other sites

Link to post
Share on other sites

I also showed you the code to create the scanner. Then you just need to use it.

 

What does your file contain? It's hard to tell you how to read that information without knowing that.

 

Here's an example of reading numbers from a file. 

Scanner sc = new Scanner(file);while (sc.hasNextLine()) {    int i = sc.nextInt();    System.out.println(i);}sc.close();

You can do it with strings or any other types as well (it shows you all the methods in the documentation).

Well this is what i got so far:

public class Assignment {    public static void main(String[] args) {    Scanner s = new Scanner(abfile);    file abfile = new file("myadrersses.txt");    file abfile = new file(bla.txt);    Scanner reader = new Scanner(abfile);    Person P = new Person();    P.setfirstname(reader.next());    P.setmiddlename(reader.next());    P.setlastname(reader.next());    P.setage(reader.nextInt());    friends.getentry(p);    }}     class Person{     private int age;     private String firstname;     private String lastname;     private String middlename;         public Person(){     age = 0;     firstname = "";     lastname ="";     middlename ="";          }     public Person(String first, String middle, String last){     this.age=age;     this.firstname= first;     this.middlename=middle;     this.lastname=last;         }          public String getfirstname(){     return firstname;         }     public String getmiddlename(){     return middlename;         }     public String getlastname(){     return lastname;         }          public void setfirstname(String F){     firstname = F;         }     public void setmiddlename(String I){     middlename = I;         }     public void setlastname(String L){     lastname = L;       }     public void setage(int age){     this.age = age;     }           public String getfullname(){     return firstname+" "+middlename+" "+lastname;         }        public int getage(){     return this.age;         }              }     class Addressbook{     private Person[] entries;     private int numPeople;     public Addressbook(){     numPeople = 0;     entries = new Person [150];     }     public Addressbook(Person[]people){     numPeople = people.length;     entries = people;     }     public int getsize(){     return numPeople;     }     public Person getPerson(int position){     return entries[position];         }     public Person getentry(int index){     return entries [index];         }     public int addentry(Person added){     entries[numPeople] = added;     numPeople++;     return numPeople;     }     public Addressbook filterbyage(int lower, int upper){     Addressbook found = new Addressbook();         for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getage()>= lower && entry.getage()<= upper){     found.addentry(entry);       }     return found;     }     }     public Addressbook filterbyfirstname(String firstname){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getfirstname().equals(firstname)){     found.addentry(entry);     }     return found;     }     }     public Addressbook filterbymiddlename(String middlename){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getfirstname().equals(middlename)){     found.addentry(entry);     }     return found;     }     }     public Addressbook filterbylastname(String lastname){     Addressbook found = new Addressbook();      for(int i=0; i<numPeople;i++){     Person entry;     entry = this.getentry(i);     if(entry.getlastname().equals(lastname)){     found.addentry(entry);     }     return found;     }     }     
Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3775932
Share on other sites

Link to post
Share on other sites

Yes, I'm aware of your other question and it's code. That's not the information I'm looking for. What are the contents of "myaddresses.txt"?

 

Copy and paste an example of what the text file will contain.

 

It could be names line by line like so

Jim SmithJohn Doeetc

Or maybe it's comma separated like so

Jim Smith, John Doe, etc

Or maybe it looks completely different. I can't help you read the file if you can't tell me what the file contains.

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3775968
Share on other sites

Link to post
Share on other sites

Yes, I'm aware of your other question and it's code. That's not the information I'm looking for. What are the contents of "myaddresses.txt"?

 

Copy and paste an example of what the text file will contain.

 

It could be names line by line like so

Jim SmithJohn Doeetc

Or maybe it's comma separated like so

Jim Smith, John Doe, etc

Or maybe it looks completely different. I can't help you read the file if you can't tell me what the file contains.

The file doesn't have anything. It's suppose to add people's names into it and store it. Am i suppose to make another method for it?

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3775996
Share on other sites

Link to post
Share on other sites

So you need to write to the file, not read from it?

I guess so. 

These are the instructions i got from my teacher:

// Your program must provide the following features either all in the main
    // method or, preferably as a collection of static methods in this class.
    // Each of the methods can recieve the ProgramState object as a way of 
    // passing all the variables around
    
    // Present the user a menu of different options for manipulating their 
    // address book:
    
    // 1) Add a Person to the address book (remember to reset the view)
    //    You will need to prompt the user for each property and the set that
    //    value on the person, then add to the address book
    // 2) Display how many people are in the address book and current view
    //    (e.g. "Showing 5 of 37 entries")
    // 3) Print the current address book view
    //    This should nicely print all entries in the current view to System.out
    // 4) Allow the user to filter the current address book view
    //    using any of the filter methods defined above (you'll need to ask them
    //    which they want to do and get input/handle accordingly)
    // 5) Remove any filters on the address book
    //    Just return the currentView to the full address book
    // 6) Save the address book to a file (i.e. Save As)
    //    Optionally: "Save current view" and/or "Save all addresses"
    //    You'll need to prompt the user for a file name the write the contents
    //    to the file in a way that you can read back in with operation (7)
    //    below.
    // 7) Load the address book from a file (i.e. Open)
    //    Consider how you wrote the contents of the address bok out in (6).
    //    Open this file and "scan" through it creating new Person objects, each
    //    of which you add the to the address book
Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776020
Share on other sites

Link to post
Share on other sites

Ok so #6 means you'll need a method that writes to the file and #7 means you'll need a method that reads from the file.

 

This provides some simple examples for writing to a file. I've already given you some information on reading from a file above. So you should be able to attempt something now.

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776044
Share on other sites

Link to post
Share on other sites

Ok so #6 means you'll need a method that writes to the file and #7 means you'll need a method that reads from the file.

 

This provides some simple examples for writing to a file. I've already given you some information on reading from a file above. So you should be able to attempt something now.

ok. what about that scanner? For some reason file and abfile have red lines under it 

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776067
Share on other sites

Link to post
Share on other sites

ok. what about that scanner? For some reason file and abfile have red lines under it 

If you're referring to this code

public static void main(String[] args) {    Scanner s = new Scanner(abfile);    file abfile = new file("myadrersses.txt");    file abfile = new file(bla.txt);    //...}

There are a few problems.

  1. You can't have 2 variables with the same name. You declare abfile twice. Change the name of one of them.
  2. file isn't written correctly. It needs to be capitalized. So use File
  3. bla.txt isn't wrapped in quotes. Use "bla.txt"
  4. You create Scanner s before you create abfile. You need to create the file before you create the scanner that uses the file.
Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776134
Share on other sites

Link to post
Share on other sites

 

If you're referring to this code

public static void main(String[] args) {    Scanner s = new Scanner(abfile);    file abfile = new file("myadrersses.txt");    file abfile = new file(bla.txt);    //...}

There are a few problems.

  1. You can't have 2 variables with the same name. You declare abfile twice. Change the name of one of them.
  2. file isn't written correctly. It needs to be capitalized. So use File
  3. bla.txt isn't wrapped in quotes. Use "bla.txt"
  4. You create Scanner s before you create abfile. You need to create the file before you create the scanner that uses the file.

 

ok does this look good? For some reason abfile still has read lines in the scanner and printwriter.

    int p;    Scanner s = new Scanner(abfile);    File abfile = new File("myadrersses.txt");    Printwriter writer = new Printwriter(abfile);    for(int i=0; i<friends.getsize();i++){    Person t = friends.getentry(i);    System.out.println(t.getfirstname()+'\t'+ t.getmiddlename()+'\t'+ t.getlastname()+'\t'+ t.getage()+'\t');    }    File Afile = new File("bla.txt");    Scanner reader = new Scanner(abfile);    Person P = new Person();    P.setfirstname(reader.next());    P.setmiddlename(reader.next());    P.setlastname(reader.next());    P.setage(reader.nextInt());    friends.getentry(p);    }}
Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776184
Share on other sites

Link to post
Share on other sites

 

ok does this look good? For some reason abfile still has read lines in the scanner and printwriter.

 

If it's good then you shouldn't be getting any red lines ;)

 

Scanner s is still created before abfile which is why you're still getting that issue. And PrintWriter is spelt incorrectly. You need a capital W in writer

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776220
Share on other sites

Link to post
Share on other sites

If it's good then you shouldn't be getting any red lines ;)

 

Scanner s is still created before abfile which is why you're still getting that issue. And PrintWriter is spelt incorrectly. You need a capital W in writer

scanner s should go after file abfile? the only red line is the abfile now. everything looks okay. 

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776236
Share on other sites

Link to post
Share on other sites

scanner s should go after file abfile?

 

Yes because it's using the abfile variable. It can't use it if it's not created.

Scanner s = new Scanner(abfile); // uses abfile but it's not created until the next lineFile abfile = new File("myadrersses.txt"); // abfile is created here but it's too late

You need to switch those lines around.

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776266
Share on other sites

Link to post
Share on other sites

Yes because it's using the abfile variable. It can't use it if it's not created.

Scanner s = new Scanner(abfile); // uses abfile but it's not created until the next lineFile abfile = new File("myadrersses.txt"); // abfile is created here but it's too late

You need to switch those lines around.

I did that and now there's more red lines.

This is what i am seeing: http://puu.sh/dLPFC/23b3fa3239.png

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776293
Share on other sites

Link to post
Share on other sites

Why is it failing? You need to provide more information. What error is it giving you? Is it failing to build or is it failing when running?

this is what happens if i run the program:

Dec 27, 2014 4:13:13 PM assignment.Assignment main
SEVERE: null
java.io.FileNotFoundException: myadrersses.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at assignment.Assignment.main(Assignment.java:25)
Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776418
Share on other sites

Link to post
Share on other sites

Remember to create the file before trying to read from it. You can't read something that doesn't exist. That is what the FileNotFoundException is telling you.

 

PrintWriter will create the file for you or you can do it manually with File createNewFile.

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776432
Share on other sites

Link to post
Share on other sites

 

Remember to create the file before trying to read from it. That is what the FileNotFoundException is telling you.
 
PrintWriter will create the file for you or you can do it manually with File createNewFile.

 

so where should i put that? how do i create the file?

Link to comment
https://linustechtips.com/topic/278563-how-do-i-make-a-scanner/#findComment-3776449
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×