Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: String Import

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default String Import

    As the forum states "inheritance", I thought this might fit here. Anyways I've looked around quite a bit on importing Strings from another class and what ever I find hasn't seemed to work.

    I want to be able to grab the username/password data from the text file, save it to a String, and import it to the login class for user input verification. Now I know it's not safe to make a login this way, but it's simply a theory test.

    In "Login" the situation is where case String(a,c): /case String(b,d): are. I would like to be able to call these strings from class playerbase.

    If I'm doing this in a completely ridiculous way, please notify me and give me suggestions on how to improve.


    Main

    package test.theory;
     
     
    public class Main{
    	public static void main(String[] args){
     
    		Login loginObject = new Login();
    		loginObject.login(args);
     
     
    	}
    }

    Login

    package test.theory;
    import test.theory.playerbase.*;
    import javax.swing.JOptionPane;
     
     
    public class Login{
     
    	public void login(String[] args){
     
     
     
    		System.out.println("Heroes of Iskast V0.0.1");
    		String a = JOptionPane.showInputDialog("Enter your username","Username");
    		switch (a){
    		case String(a,c): System.out.println("Username accepted.");  //This line, the 'a' and 'c' aren't being referenced from playerbase.java, same with line 17
    		String b = JOptionPane.showInputDialog("Enter your password","Password");
    		switch (b){
    		case String(b,d): JOptionPane.showMessageDialog(null,"You have successfully logged in, congradulations.", "Login",JOptionPane.PLAIN_MESSAGE);
    		return;
     
    		default: JOptionPane.showMessageDialog(null,"You entered the wrong password, please try again.", "Login",JOptionPane.PLAIN_MESSAGE);
    		Main.main(args);
    		}
    		default: JOptionPane.showMessageDialog(null,"You entered the wrong username, please try again.", "Login",JOptionPane.PLAIN_MESSAGE);
    		Main.main(args);
    		}
     
    		/* add an entry-per-second limiter */
     
    	}
     
    }

    playerbase

    package test.theory;
    import java.io.*;
    import java.util.*;
     
     
    public class playerbase {
    	public Scanner x;
     
    	public void openFile(){
     
    		try{
    			x = new Scanner(new File("playerbase.txt"));
    		}
    		catch(Exception e){
    			System.out.println("error found...somewhere");
    		}
     
    	}
    	public void readFile(){
     
    		while(x.hasNext()){
    			String a = x.next();
    			String b = x.next();
    			String c = x.next();
    			String d = x.next();
     
    			System.out.printf("%s%s%s%s\n", a,b,c,d); // Not quite to sure what this is going to do yet? Haven't been able to execute the code because of the String problem.
    		}
    	}
    	public void closeFile(){
    		x.close();
    	}
    }


    playerbase.txt:

    OceansRythme accept Danaku1416 danaku


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: String Import

    Your idea of using a Login object is interesting. I didn't look at the code you posted, so forgive me if my assumptions are off, but your Login class could contain userName and password fields (probably String objects) collected from the user attempting a logon that could then be passed to a validator that compares the contents of the Login object to the values stored in the file of logon names and passwords.

    Pretty simple, really. I'm not sure what you mean by "import," but I think you're making it too hard.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    OceansRythme (September 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Import

    Quote Originally Posted by GregBrannon View Post
    Your idea of using a Login object is interesting. I didn't look at the code you posted, so forgive me if my assumptions are off, but your Login class could contain userName and password fields (probably String objects) collected from the user attempting a logon that could then be passed to a validator that compares the contents of the Login object to the values stored in the file of logon names and passwords.

    Pretty simple, really. I'm not sure what you mean by "import," but I think you're making it too hard.
    Sounds brilliant, unfortunately I'm not too sure on how to go about making a validator, but I'm sure there's a tutorial out there somewhere, thanks for your input

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: String Import

    I imagine a validator would be a custom equals() method that compares the two login objects, one login object created during the user's login attempt, and the other login object created from the data stored in the file.

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Import

    Quote Originally Posted by GregBrannon View Post
    I imagine a validator would be a custom equals() method that compares the two login objects, one login object created during the user's login attempt, and the other login object created from the data stored in the file.
    Ah yes, that worked, tyvm.

Similar Threads

  1. [SOLVED] Which is more efficient? "import java.package.subpackage" or "import java.package.*"
    By Andrew R in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2013, 01:11 PM
  2. Import if Import is Found, Else
    By blazedGinger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 9th, 2013, 06:43 PM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. from where i should import
    By javaking in forum Java SE APIs
    Replies: 0
    Last Post: April 12th, 2010, 07:51 AM
  5. [SOLVED] Do i need to import anything here?
    By straw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 6th, 2010, 06:42 PM

Tags for this Thread