Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default a little help with reading in files, and class usage =/

    Hi guys!
    This is my first time coming to this site & also my first time threading! :D okay soo i am in a intermediate programming class for my university and i had a few questions that i am stuck with in this class. I have to create a scrambler game where you have to first start by logging with a user id.. if you do not have a login you must put your first and last name and make up a user id that has not been used already.

    that is where i am stuck!
    i started with this: import java.io.*;
    import java.io.IOException;
    import java.util.Scanner;

    public class Player
    {
    public static void main(String[] args) throws IOException
    {
    File file = new File("Players.txt"); // to create a new players txt document


    }
    }




    The criteria for this program is to have 2 classes: one called Player.java, Scrambler.java and A main program called Assig2 stored in file "Assig2.java"

    Can i do that and put all of them together?? how do i do that??
    ... on my main program(which is a class) i have System.out.println("welcome to the scrambler"); and my loop System.out.print("have you been here before? (y/n");

    i am confused on a few of these problems.... could someone be a lifesaver and sort me in the right direction? I know there is a lot but I will do anything if somebody explains this to me... i am dumb at this stuff...


    I also am programming with Eclipse...

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Firstly, you need to post any code in [code] tags. It helps us read it and you'll be more likely to get an answer when doing so. Also, asking specific questions (not just "how do I do this project" but something more pertinent to your actual situation, if you catch what I mean) is helpful to those wishing to assist you as well.

    Second, if your main program is Assig2, then why do you have a main() function in the Player class?

    Thirdly, you seem to know how a class should be set up, so that's a good place to start. Set up your three classes, in the same package (and obviously in their own files) to get your setup going.

    And finally (yeah, I know, you're tired of reading at this point ;)), if you have any code (like those println() statements), please include them when posting the code you have already.

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

    Default

    Thank you Zack ,
    This was the first time i posted on here and i will do that for all my following posts... this site is pretty cool though, glad i got somewhere to go if i get stuck. Thank you for correcting my Assig2, i was getting soo stuck on my classes vs methods.. i was building a main class and had another class with player next to it but i dont/didnt quite know how to run one class with the other class... but thank you here is my code

    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).

    Java Code:
    import java.io.*;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Player
    {
        public static void main(String[] args) throws IOException // so i change this main method and put that in my original code? what method do i call here? 
    
    
        {
                File ID = new File("Players.txt"); // players is the PLAYERS ID 
                File Name = new File("Last")
                
                
                
                
                
        }
    }
    right now all i am worried about is the name, id, rounds played, and rounds won

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

    Default

    could i also do like
    Java Code:
     private String Name;
     private String Id; Private Roundsplayed; private RoundsWon;
    and maybe build from there? what do you think?

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your second "guess" is the actual solution; creating each variable as a "File" type in the main method will do nothing to help you.

    Do you know where those variables go in the code? Have you tried compiling the code with them in there?

    PS: Do you know the 'int' type? Or 'double' or 'float'? If the ID is just a number, you could use the 'int' type instead. If it's an actual word, then ignore this PS. ;)

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

    Default

    okay cool :) i am on the right track then... i do not know where i am going to use them yet... but i know that i must have the user enter their id ... soo i plan on this
    Java Code:
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter your id:"); 
    id = keyboard.nextLine();
    is this okay ? i need to be able to read in and validate if the user has his/her id on file already...

    also where does this code go? in my player.java class? or my main program?

    THANK YOU SO MUCH ZACK

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Depends how you want to set your program up. Most likely, the private variables are declared in the Player class, while the actual Scanner object is handled in the Assig2 class (main method), and creates a new Player object to contain the data.

    Have you worked with object creation, and using getter/setter methods?

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

    Default

    ooh really... hmmm no i have never worked with objects... i just created basic programs that didn't require the usage of other classes/ methods.. In class we barely even touched on this information because there is so much to cover in little time...soo here i am trying to teach myself out of the book

    the main problem i am having is basically understanding classes and methods.. i am programming on eclipse and i created my main program under a class option and i did the same for players.java

    i have all of my private variables in my players.java i have to use Name, Id, Rounds played, Rounds Won in private instance variables...which i did already? right? because they have to store information... now do i have to create a method for all of these in my players.java?

    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".

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

    Default

    Also Zack so far you have been an excellent help already! what could i use for the method for this class? since i cannot call it public static void main?

    Java Code:
    import java.io.*;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Player
    {
    
                private String Name;
                private String Id;
                private String RoundsPlayed;
                private String RoundsWon;
        
    }

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You *could* call it that, but since it's not actually a main method, you *shouldn't*.

    Here's some articles that you will want to read:
    Object Creation
    Getter/Setter Methods
    Creating Methods
    Calling Methods in other Objects

    Those will be of great help to you should you actually choose to read and comprehend them.

    I have to head to bed for college tomorrow (blargh), so should you run into any issues others should be able to help you or, in any scenario, I'll be around tomorrow night. ;)

    Best of luck, and hope these links are helpful to you!

    PS: Once you're comfortable with everything above--and I mean that (don't just dive into this first)--you can start reading up on basic Java I/O for reading the login file. :)

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

    Default

    Hey Zack, i read your articles that you have given to me here is what my code looks like at the moment
    Java Code:
    import java.io.Console;
    import java.io.*;
    import java.util.Scanner;
    public class Assig2 {
    
        
        public static void main(String[] args) throws IOException { //method  
            String answer;
            String id; 
          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();
            
         while (answer.equalsIgnoreCase("yes")) { 
            
            System.out.print("Please enter your ID:");
               File myFile = new File ("players.txt");
               Scanner players = new Scanner(myFile);
               id = keyboard.nextLine(); 
        
            
            players.close(); // this is needed to close objects source of input 
            
         } // if answer is yes end bracket
             
             
         if (answer.equalsIgnoreCase("no")){ System.out.print("Welcome new player!\n");
         System.out.print("Please create an ID:");
      
        FileWriter fwriter = new FileWriter("Players.txt", true);
         id = keyboard.nextLine();
        PrintWriter outputFile = new PrintWriter (fwriter);
    
         }
             
             
        }
    and my players .java
    Java Code:
    import java.io.*;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Player
    {
      
        
    
                
                
                private String Name;
                
                
                private String Id;
                private String RoundsPlayed;
                private String RoundsWon;
               
                
    
              public Player (String loginName) 
                { 
                  loginName = Name; 
                  
                  
                  
                  
                  
    }
    }
    I really do not know what to do with that players.java file.

Similar Threads

  1. Bug Reading txt files
    By MHardeman25 in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:03 PM
  2. Reading .bin files
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-22-2010, 04:39 PM
  3. Reading .txt files
    By cvcs1 in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:07 AM
  4. Replies: 1
    Last Post: 11-17-2008, 09:32 AM

Posting Permissions

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