Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default (help w/ class) Running out of time...project due thursday..Please save me...

    PLEASE SAVE ME!
    I am running out of time for this java program..... thats due thursday.. and i am completely stumped on how to build up the player.java class.... idk what i am missing
    here is what my teacher asks: Your program must be implemented using 3 classes:

    1) A class Player stored in file "Player.java". This class must store all information related to a player of the game (ex: Name, Id, Rounds played, Rounds Won) in private instance variables. It must also have methods to allow a player to:
    a) Login this will test a player's Id against a file (whose name is passed into the method as a parameter). If the id is found in the file the player's logged status will be marked as "true"; otherwise it will be marked as "false".

    b) Register this will test a player's Id against a file (whose name is passed into the method as a parameter). If the Id is not found in the file it will be added to the end of the file and the player's logged status will be marked as "true"; otherwise it will be marked as "false".

    c) Update his/her wins and update his/her losses (since the main program will not be able to access the instance variables directly)

    d) Return a formatted String version of the player's information for output

    e) Write the player's information back to the appropriate file (based on the player's Id).

    2) A class Scramble stored in file "Scramble.java". This class must have the following:

    a) Two instance variables (one for the original word and one for the scrambled version)

    b) A constructor that takes a file name as an argument and reads a random word from the file. It then creates a "scrambled version" of the word and stores both the original and the scrambled version. Think carefully about how you can select a random word from the file (Hint: generate a random number based on the number of words in the file and use it to determine how many words to skip) and about how you can scramble a string (Hint: use a StringBuilder and some of the mutators in that class to exchange characters).

    c) Two accessors one to return the original word and one to return the scrambled version of the word.

    3) A main program called Assig2 stored in file "Assig2.java". This program will utilize the Player and Scramble classes to actually execute the game, as described in the paragraphs above. For example, the game requires that a returning player be given 3 chances to log in. Thus your main program should call the "Login" method of your Player object up to 3 times before either continuing with the game or quitting. As another example, after a player logs in, your program should create a Scramble object (using the "words.txt" file). Its accessors can then be used by the program to obtain the word and its scrambled version.

    You may not use any arrays (or ArrayLists or other similar types) in this program.


    i am only concerned about part 1..where i am stuck.. how do i get user to log in???? how do i get them to verify their id is off of players.txt? i must have another .txt for the user ( Last Name, First Name, # of Games Played, # of Games Won.)

    here is what i have...
    Java Code:
    import java.io.*;
    import java.util.*;
    
    import java.util.Scanner;
    public class Assig2 {
        public static void main(String[] args) throws IOException { //method  
            
            Player p = new Player(); 
              
        Scanner inScan = new Scanner (System.in);
            String answer;
            String id;
            String lastName;
            String firstName;
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("Welcome to The Scrambler");
            System.out.print("Have you visited with us before? (yes or no)");
            answer = keyboard.nextLine();
              
        
         if (answer.equalsIgnoreCase("yes")) { 
            
                System.out.print("Please enter your ID:");
                String theId = inScan.next();
        
          }
             
         if (answer.equalsIgnoreCase("no")){ 
         
        System.out.print("Welcome new player!\n");
         System.out.print("Please enter your Id:");
         id = keyboard.nextLine();
         
         System.out.print("Please enter your last name:");
         lastName = keyboard.nextLine();
         System.out.print("Please enter your first name:");
         firstName= keyboard.nextLine();
         
           }
     }
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Player
    {
           //Instance Fields     
                private Scanner inScan;
                private String Name, Id1; 
                private int roundsPlayed, roundsWon;
                    
             /** Constructor 
                 Name The users full name 
                 i The id
                 Rp The number of rounds played
                 Rw The number of round player has won
              */
             
                public Player()
                {
                      File players = new File ("players.txt");
                }
        
                public void login(String name)
                {
                    
                }
          } //ends class

  2. #2
    semitrix is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    lol I think i'm in your class.. were you able to do the scramble class?

  3. #3
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    lol really? pitt? ramirez? i havent even gotten to the scrambler class yet..still working on the stupid player.java class... did you get that class? if so what did you use? how did you use Player player = new player(); ? i am so lost

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    No one in the forum doing assignments for others, that's not the idea behind a forum. However if you could ask your question specifically then we can help you to sort out the problem.

    On what you've stuck?

  5. #5
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Hey Eranga thanks for the reply... i am really not asking for someone to do my project for me.. since i am a cs major and all... my question now is first.. did i write this player.java class okay? and i cannot get my valid method to work?

    heres my code for my player.java class
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Player
    {
      
        
    
            //Instance Fields     
                private Scanner inScan;
                private String Name, Id; 
                private int roundsPlayed, roundsWon;
                
             /** Constructor 
                 Name The users full name 
                 i The id
                 Rp The number of rounds played
                 Rw The number of round player has won
              */
             
                public Player()
                {
                      File players = new File ("players.txt");
                      File ids = new File ("ids.txt");
                }
        
                
                public void login(String name, String id)
                {
                
                    name = Name; 
                    id = Id;
                
                FileInputStream player;
                 try {
                    player = new FileInputStream("players.txt");
                    
                     player.close();
                      }
             catch ( IOException e)
                    {
                 System.err.println ("Unable to read from file");
                 System.exit(-1);
    
                    } [CODE]
    }//ends player class[/CODE]

    Code for my Main class
    Java Code:
    import java.io.*;
    import java.util.*;
    
    import java.util.Scanner;
    public class Assig2 {
    
        
        public static void main(String[] args) throws IOException { //method  
            
            Player p = new Player(); 
              
            
         Scanner inScan = new Scanner (System.in);
            String answer;
            String id;
            String lastName;
            String firstName;
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("Welcome to The Scrambler");
            System.out.print("Have you visited with us before? (yes or no)");
            answer = keyboard.nextLine();
              
        
         if (answer.equalsIgnoreCase("yes")) { 
            
                System.out.print("Please enter your ID:");
                String theId = inScan.next();
             p.login(theId, "players.txt");
                
                if (p.valid())
                {System.out.println("Hello " +  theId);
                 System.out.println("\nThank you for coming back to the Scrambler");
                
                
                }
                
            
                
                
                
          }
        
         if (answer.equalsIgnoreCase("no")){ 
         
        System.out.print("Welcome new player!\n");
         System.out.print("Please enter your Id:");
         id = keyboard.nextLine();
         
         System.out.print("Please enter your last name:");
         lastName = keyboard.nextLine();
         System.out.print("Please enter your first name:");
         firstName= keyboard.nextLine();
         
           }
          
         
         }
         
        }

    now i have an error at line 31
    stating... Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method valid() is undefined for the type Player

    at Assig2.main(Assig2.java:31)

    did i write my player.java okay?

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You call the method valid(), but have not defined it in your player class. Next, your constructor:
    Java Code:
     public Player()
                {
                      File players = new File ("players.txt");
                      File ids = new File ("ids.txt");
                }
    Do you know how scope works? The two File instances you create in this constructor cannot be accessed outside of this constructor. Example:
    Java Code:
    public void notCorrect() {
      if(true) int a = 5;
      System.out.println(a); //will produce an error at compile time
    }
    
    public void Correct() {
      int a = 0;
      if(true) a = 5;
      System.out.println(a); //compiles and runs correctly
    }
    And since you keep a record of players, you don't really need to ask if you have played before. In pseudocode, it would look like this:
    get the players ID
    search the records
    if ID found
    log in
    else
    create a new ID
    Last edited by m00nchile; 10-13-2010 at 08:51 AM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    my first suggestion is not to ask the player "if he has visited us" but if he want to register or to login. so your player class could have a login and a register() class. to your error: since there is no valid() method in the player don't call it.

  8. #8
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    no i have never heard of scope... ok so i get rid of my two files right there... then how do i solve my problem with my method?

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Easy, add a valid() method to your player class.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    ok cool thanks guys, now heres my other question ... soo i need to have a file of user ids called "players.txt" which i have displayed already... Where each id should have no spaces or other "white space" characters (ex: tabs, new lines), but any other characters are ok.... i think i did that right by just formatting it right on the txt document....

    I now have a file for each user containing 4 lines. Last Name, First Name, # of Games Played, # of Games Won. These user files have to be named based on user ids. For a given user id, say user1, the file will be named "user1.txt". Note that given an id obtained from the "players.txt" file above, the individual user file can easily be determined and accessed. This user file will be created and initialized when a player first registers, and it will be updated after each game to reflect the new number of games played and games won.

    how do i go about doing that? how do i take 1 file such as my players.txt and make it so each user in that .txt document has a user.txt with the last name stored, first name...etc

  11. #11
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Also back to my vaild method... whenever i define the method in my player.java is has to be boolean correct? i have tried this
    Java Code:
    public boolean valid() {
                
                return true;
            }
    which does really nothing..

    this is my first program assignment where we are doing all this...so i am sorry if i am asking stupid questions...

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ok, the method is fine. What you want to do in that. If you could explain it I can help you out. Since I'm middle of this I've no complete idea.

  13. #13
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    what i want to do with that i make it so that:
    Java Code:
        if (p.valid())
                {System.out.println("Hello " +  theId);
                 System.out.println("\nThank you for coming back to the Scrambler");
             
                }
    will run in my main program.... but whenever i have that in there...it doesnt display hello + id...

    if i am not clear... all i want to do is say Hey my valid method is true... so move along in my program.. and if it is false say sorry you did not enter a valid id.


    you guys are really helping...again i am sorry and i really appreciate your time
    Last edited by basketball8533; 10-13-2010 at 10:06 AM.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If you can't see that line is printing, means that the valid method is not return true. Before that check object p is exist or not, check for null to test.

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    I had a top level view of your code.

    Here
    if (answer.equalsIgnoreCase("yes")) {

    System.out.print("Please enter your ID:");
    String theId = inScan.next();
    p.login(theId, "players.txt");

    if (p.valid())
    {System.out.println("Hello " + theId);
    System.out.println("\nThank you for coming back to the Scrambler");


    }





    }


    Put printstatements whether are u able to come inside the if condition or not.
    print the "answer" .It might not be "yes".
    Ramya:cool:

  16. #16
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    ok guys so i just met with my teacher again...and i am like pulling my hair out because i am so freaking stuck! ahhhh

    sooo this is where i am at now
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Player
    {
      
        
    
            //Instance Fields     
                private Scanner inScan;
                private String Name, Id; 
                private int roundsPlayed, roundsWon;
                private boolean isValid;
                private String compare;
               
                FileInputStream pfile;
             
                /** Constructor 
                 Name The users full name 
                 i The id
                 Rp The number of rounds played
                 Rw The number of round player has won
      
              */
          
                
                public void login(String id, String fName) throws FileNotFoundException
                {
                
                 
                    Id = id;  //user entered id 
                
               
                 
                     pfile= new FileInputStream (fName); // file 
                     inScan = new Scanner(pfile); // gives inscan the file 
                    
            
                     
                     
                     }
               
                     // need while loop 
                
     public boolean valid() {
                
                
                return isValid;
            }
    I Was to put a while loop in my login method... but i am freaking lost.. i would seriously pay somebody to help me get through this...i am losing my faith in myself becoming a cs major...if i cant grasp this then...ughhhh

  17. #17
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    As soon as you have loaded the file, you need to read in each line of the I'd file. As each line is read then you need to compare the I'd with the one passed in to the method. If a match is found then terminate the loop.

    Regards.

  18. #18
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Hey Ronin, could you provide an example? ... can i write something like... inScan.nextLine(); ?

  19. #19
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    I cannot. The best I can do for you is to point you in the direction of suitable tutorials.

    Data Input Stream.
    Read the second lot of code just after the title DataInputStream(FileOutputstream finp
    The example is basically what you need to complete the read in part of the assignment. The only thing that is missing is the comparison between the ids in the file and the one passed into the method and the associated break statement if a match is found.

    Writing to a file is just as simple. All you have to do is sustitute all reference to Input with Output.

    Java Code:
    DataInputStream stream = new DataInputStream(fstream);
    becomes

    Java Code:
    DataOutputStream stream = new DataOutputStream(fstream);
    You can then call methods such as writeUTF (For Strings), writeDouble(), etc...
    The only thing to watch for is I think the default operation of writing to a file is overwriting all data in it. It needs some minor alterations if you wish to append instead.

    Regards.

  20. #20
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Java Code:
    public void login(String id, String fName) throws FileNotFoundException
                {
                
                 
                    Id = id;  //user entered id 
                
               
                 
                     pfile= new FileInputStream (fName); // file 
                     inScan = new Scanner(pfile); // gives inscan the file 
                    while(inScan.toString() != null ){
                        
                        String pfileName = Id + ".txt";
                        Scanner pScan = new Scanner(new File(pfileName));
    
                     
                     }
                }
    would something like that be suitable?

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help running 2 frames at the same time.
    By Nuclear Mosquito in forum AWT / Swing
    Replies: 4
    Last Post: 07-03-2010, 06:11 PM
  2. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM
  3. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  4. Improve the slow running time
    By coolFrenzi in forum Advanced Java
    Replies: 1
    Last Post: 04-12-2009, 12:15 PM
  5. Calculating the Running time
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 10:32 PM

Posting Permissions

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