Results 1 to 11 of 11
  1. #1
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Pass a value from main class to another class

    THIS IS MY MAIN CLASS:
    Java Code:
    import java.io.FileNotFoundException;
    
    public class PrimaClasse {
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		SecondaClasse oggettoSeconda = new SecondaClasse();
    		oggettoSeconda.controlloNomi();
    	}
    }
    AND THIS IS MY SECOND CLASS:
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class SecondaClasse {
    	public void controlloNomi() throws FileNotFoundException{
    		
    		System.out.println("Enter your user user name:");
    		Scanner name = new Scanner(System.in);
    		String name_used = name.nextLine();
    		
    		Scanner userList = new Scanner(new File("path to file... usersList.txt"));
    		
    		int val = 0;		
    		while(userList.hasNextLine()){
    			String line = userList.nextLine();
    			
    			if(line.indexOf(name_used) != -1){
    				System.out.println("You are a registered member, WELCOME BACK!");
    				val = 1;
    				break;
    			}else{
    				val = 0;
    				continue;
    			}
    		}
    		if(val == 0){
    			System.out.println("You're not a registered member, RUN TO SIGN UP!");
    		}
    	}
    }
    Now it's working and from the main class i can controll the second class BUT. i want that is the main class that ask the user name, and i want pass that value to the second class. what code i must change?


    ps: not important but why eclipse wants me insert this import java.io.FileNotFoundException; and this throws FileNotFoundException for not give me an error?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Pass a value from main class to another class

    i want that is the main class that ask the user name, and i want pass that value to the second class.
    The first part is easy: use println()/nextLine() to ask and get the name as you already do in controlloNomi(). But now you will do it in main().

    The next step is to pass the name as an argument to controlloNomi(). This will involve changing how you declare the controlloNomi() method:

    Java Code:
    public void controlloNomi(String name_used) throws FileNotFoundException{
             
        //System.out.println("Enter your user user name:");
        //Scanner name = new Scanner(System.in);
        //String name_used = name.nextLine();
             
        Scanner userList = new Scanner(new File("path to file... usersList.txt"));
        ...
    ---

    When you say userList = new Scanner(file) there is always the chance, when you run the program, that the file you are reading from will not be found. In the Java language you often have to acknowledge this at compile time. You don't have to do anything about the possibility that the file might not exist, you can just declare the method to "throw" the error. The effect of throwing an error is that if anything goes wrong in controlloNomi() the program will stop and report the problem back to main(). main() also throws the error and this will cause the program to print an error message and stop completely. (If you type the wrong name of the file you can see this happening.)

    Clearly there are other things you can do about a file not being there, but for now just using "throws" is enough. But now you refer to this exception class (FileNotFoundException) and - just like other classes you use - it has to be imported.

  3. #3
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Pass a value from main class to another class

    ok i understand that explanation for the file not found, btw i create the file so np. i managed the code and is going very well
    ONE IMPORTANT QUESTION: i need that if the user_name is in the list, checked by the method of the second class a value positive come back to main so i can go with a second menu, how i do that?
    ok i made this, and i think that se method in SecondaClasse it's now returning a value but i not know how handle it in the main.
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class SecondaClasse {
    	public int controlloNomi(String name_used) throws FileNotFoundException{
    		
    		Scanner userList = new Scanner(new File("path to file... usersList.txt"));
    		
    		int value_for_main = 0;
    		
    		int val = 0;		
    		while(userList.hasNextLine()){
    			String line = userList.nextLine();
    			
    			if(line.indexOf(name_used) != -1){
    				System.out.println("You are a registered member, WELCOME BACK!");
    				val = 1;
    				value_for_main = 1;
    				break;
    			}else{
    				val = 0;
    				continue;
    			}
    		}
    		if(val == 0){
    			System.out.println("You're not a registered member, RUN TO SIGN UP!");
    			value_for_main = 0;
    		}
    		return value_for_main;
    	}
    }
    after...
    now i need to modify the code for:

    ask if i'm new or a registered user and i make this
    Java Code:
    System.out.println("WELCOME!");
    		System.out.println("- If you have an existent account digit 1");
    		System.out.println("- If you are a new User digit 2");
    
    		Scanner choice = new Scanner(System.in);
    		int choice_value = choice.nextInt();
    now i have to make that:
    a) if i choose "1" the system using the previously post code check if i'm in list and if yes a variable will be set positive (this will allow me to launch a second menu later)
    b) if i choose "2" i can register my name in the list. (with the option name already exist choose another) and only after i can go to the new menu.

    for make this you suggest me to create a new class or a new method inside the class ControlloNomi (i can put 2 method in 1 class... if i have understood right the java oo meaning yes)


    any suggestions to help me?
    Last edited by maki; 02-05-2015 at 11:51 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Pass a value from main class to another class

    You can return val from the checking method. You can also put another method in that calss: but there's no need to because you have already calculated val.

    Java Code:
    public int controlloNomi() throws FileNotFoundException { // declared to return int, rather than being void
        // as before
    
        return val;
    }
    In main() you do whatever you want with the returned value:

    Java Code:
    int response = oggettoSeconda.controlloNomi();
    // response will be zero if they are not a member

  5. #5
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Pass a value from main class to another class

    i made that but wait a second...
    now i have MAIN CLASS:
    Java Code:
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class PrimaClasse {
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		System.out.println("Enter your user user name:");
    		Scanner name = new Scanner(System.in);
    		String name_used = name.nextLine();
    		
    		SecondaClasse oggettoSeconda = new SecondaClasse();
    		oggettoSeconda.controlloNomi(name_used);
    		
    		int response = oggettoSeconda.controlloNomi(name_used);
    		
    		if(response == 1){
    			System.out.println("Now you can choose a level:");
    		}
    	}
    }
    SECONDARY CLASS:
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class SecondaClasse {
    	public int controlloNomi(String name_used) throws FileNotFoundException{
    		
    		Scanner userList = new Scanner(new File("path to file... /usersList.txt"));
    		
    		int val = 0;		
    		while(userList.hasNextLine()){
    			String line = userList.nextLine();
    			
    			if(line.indexOf(name_used) != -1){
    				System.out.println("You are a registered member, WELCOME BACK!");
    				val = 1;
    				break;
    			}else{
    				val = 0;
    				continue;
    			}
    		}
    		if(val == 0){
    			System.out.println("You're not a registered member, RUN TO SIGN UP!");
    		}
    		return val;
    	}
    }

    and this output:
    PHP Code:
    Enter your user user name:
    mario
    You are a registered member, WELCOME BACK!
    You are a registered member, WELCOME BACK!
    Now you can choose a level:
    so yes the method return a value and it's good but why now stamp 2 times the same phrase?
    Last edited by maki; 02-06-2015 at 12:12 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Pass a value from main class to another class

    It looks it is finding a match two times in the while loop. It would be better to return from checking method as soon as you find a match.

  7. #7
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Pass a value from main class to another class

    a curious think, if i wrote as answer "mario" (that is in the txt list) it ok, but i wrote:
    m
    ma
    mar
    mari
    the program accept them anyway as right, i think that is a problem of how he check the list. i not understand here...
    but if i wrote mariA (that is not in the list) say to me rightly that are not registered.

    then i'm having difficult to write the code for reverse an array of int [1 2 3] and i want [3 2 1]. can help?
    Last edited by maki; 02-06-2015 at 03:08 AM.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Pass a value from main class to another class

    i think that is a problem of how he check the list.
    Yes. At the moment you check with "if(line.indexOf(name_used) != -1)" but you really should check that they are equal with

    Java Code:
    if(line.equals(name_used)) {

  9. #9
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Pass a value from main class to another class

    it's not so simply, can you show me the right code?
    with my code now i can't use .equals function because after i have != 1 and the system say to me that the argument is undefined fortype(s) boolean, int.
    the code is:
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class SecondaClasse {
    	public int controlloNomi(String name_used) throws FileNotFoundException{
    		
    		Scanner userList = new Scanner(new File("path to file... /usersList.txt"));
    		
    		int val = 0;		
    		while(userList.hasNextLine()){
    			String line = userList.nextLine();
    			
    			if(line.indexOf(name_used) != -1){
    				System.out.println("You are a registered member, WELCOME BACK!");
    				val = 1;
    				break;
    			}else{
    				val = 0;
    				continue;
    			}
    		}
    		if(val == 0){
    			System.out.println("You're not a registered member, RUN TO SIGN UP!");
    		}
    		return val;
    	}
    }
    and after i stille stamp to console 2 identic messagge and i not know why

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Pass a value from main class to another class

    Don't just replace indexOf(name_used) by equals(name_used). PBrockway's example was spot on. If you look closely, he doesn't have the != -1 behind it, because the equals() method itself returns a boolean, a true or a false.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Pass a value from main class to another class

    i know that, but i ask for the correct code modification because if you saw what i write before in this case all this is in a method of a secondary class, and must stamp a phrase (if the insert username is correct or not) and return a value to the main.

    i not understant how modify the code and little help will be appreciated

Similar Threads

  1. Replies: 3
    Last Post: 12-15-2012, 04:46 AM
  2. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  3. Replies: 1
    Last Post: 08-02-2011, 01:34 PM
  4. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 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
  •