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

    Default Classes and division of work

    so start from the top so I hope to clarify my doubts.
    I am creating a small program, and I will use this thread without opening others for help if you will be so kind. I use eclipse and this I have to use.

    I wish that in the beginning there was a button that says "user" and that if pressed present a screen with the options:

    - New user
    if you press this you can enter your name and if you want a password and everything is stored in a txt file.

    - Existing User
    if you press you enter the name and password if necessary.

    after one of these two options we proceed to a screen where you can choose, by push button, a level 1 to 6 (then 6 buttons).

    what I would like to clarify is the creation of the class.
    called the project "MyProject" I create a class and call it for example "LogIn", within this class with the code created a window, which I put on the button.

    Now what follows, the two new buttons "new user" and "existing user" and their implementation must be done always in this class or do I create a new call as "Check Data"?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Classes and division of work

    What have you tried? Be sure to wrap any you post in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Classes and division of work

    hi and thank you for the answer.
    here it is the code that i wrote, the problem, from my point of view, is that is to much like a procedural code. but first look at him and then i ask you something.

    Java Code:
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    import java.io.*;
    
    public class ProvaProgetto {
    
    	public static void main(String[] args) {
    		
            boolean entry_if_correct;
            
    		System.out.println("WELCOME!");
    		System.out.println("- If you are a new User digit 1");
    		System.out.println("- If you have an existent account digit 2");
    		
    		Scanner choice = new Scanner(System.in);
    		int choice_value = choice.nextInt();
    		
    		if(choice_value == 1){
    			System.out.println("WELCOME BACK");
    			System.out.println("Your name?");
    			
    			Scanner name = new Scanner(System.in);
    			String name_used = name.nextLine();
    				if(name_used.equals("mario") || name_used.equals("carlo")){
    					System.out.println("Correct identification");
    					entry_if_correct = true;
    				}else{
    					System.out.println("you are not a registered users");
    					entry_if_correct = false;
    				}
    		}
    		
    		if(entry_if_correct = true){
    			System.out.println("now choose a level");
    			System.out.println("1 2 3 4");
    			
    			Scanner level = new Scanner(System.in);
    			int choice_level = level.nextInt();
    			
    			if(choice_level == 1){
    						System.out.println("READY TO COUNT?");
    						try{
    							Thread.sleep(1000);
    							} catch(InterruptedException ie) {}
    						System.out.println("3");
    						try{
    							Thread.sleep(1000);
    							} catch(InterruptedException ie) {}
    						System.out.println("2");
    						try{
    							Thread.sleep(1000);
    							} catch(InterruptedException ie) {}
    						System.out.println("1");
    						try{
    							Thread.sleep(1000);
    							} catch(InterruptedException ie) {}
    						System.out.println("START COUNTING");
    						try{
    							Thread.sleep(2000);
    							} catch(InterruptedException ie) {}
    				
    				int size = 10;
    				int max = 20;
    				int total = 0;
    				
    				int[] array = new int[size];
    				Random generator = new Random();
    				
    				for(int i=0; i<size; i++){
    					array[i] = generator.nextInt(max);
    				}
    				for(int i = 0; i < array.length; i++){
    					System.out.print(array[i]);
    						try{
    							Thread.sleep(4000);
    							} catch(InterruptedException ie) {}
    					if(i != array.length - 1) System.out.print(" + ");
    				}
    				for(int i=0; i<size; i++){
    					total = total + array[i];
    				}
    				
    				System.out.println("\nTOTAL?");
    				
    				Scanner result = new Scanner(System.in);
    				int result_user = result.nextInt();
    				
    				if(result_user == total){
    					System.out.println("GOOD JOB!");
    				}else{
    					System.out.println("YOUR COUNT IS WRONG!");
    				}
    			}	
    		}	
    	}
    }

    some problem:
    a) if i choose the option number 2 (i'm new user) the program go as i choosed the number 1
    b) all this can in a sort of way go if i use the terminal but i wanna do all this with a window? (JPanel, JFrame)?
    c) because i wanna add some utility like a password associate to a name, and save these and read these to check on a .txt (but it's hard)

    other question when i hear your opinion guys

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Classes and division of work

    if i choose the option number 2 (i'm new user) the program go as i choosed the number 1
    What does the code do that is wrong? Where should the code do something differently?

    Can you copy the full contents of the command window that shows what happens? Add some comments describing what is wrong.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Classes and division of work

    if I choose "1", the program should ask my name and save it in a file txt
    if I choose "2", the program should ask my name and see if it is listed, and if it is that should make me to choose a level of difficulty

    Now if I choose "1" and insert a name that is not in the list the program tells me that they are not properly registered but makes me continue


    more in general i can say that i want make this program in object, i mean i would like to make a class that take name_users insert them on a txt and that provide the function to check if i'm registered, another class that generate the levels, but i not understand which is the main class that coordinate all this actions.

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Classes and division of work

    Pay attention to this part:

    Java Code:
    if(entry_if_correct = true){ // wrong
    if(entry_if_correct){ // correct

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

    Default Re: Classes and division of work

    i handled and solved a couple of problem and now i have
    Java Code:
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    import java.io.*;
    
    public class ProvaProgetto {
    
    	public static void main(String[] args) {
    		
            boolean entry_if_correct;
            
    		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();
    		
    		if(choice_value == 1){
    			System.out.println("WELCOME BACK");
    			System.out.println("Your name?");
    			
    			Scanner name = new Scanner(System.in);
    			String name_used = name.nextLine();
    				if(name_used.equals("mario") || name_used.equals("carlo")){
    					System.out.println("Correct identification");
    					entry_if_correct = true;
    					
    					if(entry_if_correct = true){
    						System.out.println("now choose a level");
    						System.out.println("1 2 3 4");
    						
    						Scanner level = new Scanner(System.in);
    						int choice_level = level.nextInt();
    						
    						if(choice_level == 1){
    									System.out.println("READY TO COUNT?");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("3");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("2");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("1");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("START COUNTING");
    									try{
    										Thread.sleep(2000);
    										} catch(InterruptedException ie) {}
    							
    							int size = 10;
    							int max = 20;
    							int total = 0;
    							
    							int[] array = new int[size];
    							Random generator = new Random();
    							
    							for(int i=0; i<size; i++){
    								array[i] = generator.nextInt(max);
    							}
    							for(int i = 0; i < array.length; i++){
    								System.out.print(array[i]);
    									try{
    										Thread.sleep(4000);
    										} catch(InterruptedException ie) {}
    								if(i != array.length - 1) System.out.print(" + ");
    							}
    							for(int i=0; i<size; i++){
    								total = total + array[i];
    							}
    							
    							System.out.println("\nTOTAL?");
    							
    							Scanner result = new Scanner(System.in);
    							int result_user = result.nextInt();
    							
    							if(result_user == total){
    								System.out.println("GOOD JOB!");
    							}else{
    								System.out.println("YOUR COUNT IS WRONG!");
    							}
    				}else{
    					System.out.println("you are not a registered users");
    					entry_if_correct = false;
    					System.exit(0);
    					}
    				}
    			}
    		}	
    	}	
    }

    @milovan why attention? is use that as validation for the if-statement. explain please


    this is an example of output:
    XML Code:
    WELCOME!
    - If you have an existent account digit 1
    - If you are a new User digit 2
    1
    WELCOME BACK
    Your name?
    carlo
    Correct identification
    now choose a level
    1 2 3 4
    1
    READY TO COUNT?
    3
    2
    1
    START COUNTING
    15 + 11 + 17 + 12 + 10 + 13 + 1 + 12 + 8 + 16
    TOTAL?
    114
    YOUR COUNT IS WRONG!
    i need that this line
    15 + 11 + 17 + 12 + 10 + 13 + 1 + 12 + 8 + 16
    disappear after an amount of second and left me the TOTAL? question, how i can make this happen?
    Last edited by maki; 02-04-2015 at 01:19 AM.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Classes and division of work

    First to explain attention part..

    when you write this:
    Java Code:
    entry_if_correct = true
    you are actually saying to VM to assign value TRUE to your variable entry_if_correct. That means, from that point in code, value of entry_if_correct is set to be true no matter what value it has previously.

    Now, when you testing
    Java Code:
    if(entry_if_correct = true){
    it will always be true as you just (as explained) assigned it to entry_if_correct and the following code will executed.
    Even if it was set to be FALSE before, now it will be assigned TRUE.

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

    Default Re: Classes and division of work

    ok i understand and for what i see is not important here, for 2 reason
    1- i can use a XXXX = YYYY (casual) as validation for the if-statement that follow
    2- the "false" case is after not before the true assignement.

    i used that because i was thinkinh to use that for validation of level create in another classfile but i'm not able to do that.

    i'm more interested on my other question, because i need help to code all this things

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Classes and division of work

    ok i understand and for what i see is not important here
    it is very important since this boolean condition will always be TRUE and the following code inside if block will always be executed. From your explanation, this is the problem you are facing.

    1- i can use a XXXX = YYYY (casual) as validation for the if-statement
    No, you can not. In this case you are using operator '=' which is used in java (other languages as well) as assign operator (used for assigning values to variables, for example). If you want to compare something you need to use '==' operator for primitive data types or .equals(Object o) method for objects.

    Ok, you can put something like
    Java Code:
    if(XXXX == YYYY){
    which is ok for some comparisons (for example if you compare int's) but is little awkward in case of boolean condition. Remember my example from the first post, there you can find common way how to write this line of code.

    Java Code:
    2- the "false" case is after not before the true assignment.
    Again not right. Since you declared entry_if_correct but did not assign value, VM will assign default value which in fact is FALSE.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Classes and division of work

    Quote Originally Posted by milovan View Post
    Since you declared entry_if_correct but did not assign value, VM will assign default value which in fact is FALSE.
    I don't think so. entry_if_correct is a local variable and they are not initialized by the VM. If the OP tries to use it before it is initialized the compiler should generate an error.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Classes and division of work

    true,
    it is in main() method.. i glanced it on the top of the code :-)

  13. #13
    maki is offline Member
    Join Date
    Sep 2013
    Posts
    42
    Rep Power
    0

    Default Re: Classes and division of work

    ok i have understood what are you saying, but let me some space of liberty because the program is under development so maybe that "entry_if_correct" will be fixed and use later.
    now i have this code here:
    Java Code:
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    import java.io.*;
    
    public class ProvaProgetto {
    
    	public static void main(String[] args) {
    		
            boolean entry_if_correct;
            
    		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();
    		
    		if(choice_value == 1){
    			System.out.println("WELCOME BACK");
    			System.out.println("Your name?");
    			
    			Scanner name = new Scanner(System.in);
    			String name_used = name.nextLine();
    				if(name_used.equals("mario") || name_used.equals("carlo")){
    					System.out.println("Correct identification");
    					entry_if_correct = true;
    					
    					if(entry_if_correct = true){
    						System.out.println("now choose a level");
    						System.out.println("1 2 3 4");
    						
    						Scanner level = new Scanner(System.in);
    						int choice_level = level.nextInt();
    						
    						if(choice_level == 1){
    									System.out.println("READY TO COUNT?");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("3");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("2");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("1");
    									try{
    										Thread.sleep(1000);
    										} catch(InterruptedException ie) {}
    									System.out.println("START COUNTING");
    									try{
    										Thread.sleep(2000);
    										} catch(InterruptedException ie) {}
    							
    							int size = 10;
    							int max = 20;
    							int total = 0;
    							
    							int[] array = new int[size];
    							Random generator = new Random();
    							
    							for(int i=0; i<size; i++){
    								array[i] = generator.nextInt(max);
    							}
    							for(int i = 0; i < array.length; i++){
    								System.out.print(array[i]);
    									try{
    										Thread.sleep(4000);
    										} catch(InterruptedException ie) {}
    								if(i != array.length - 1) System.out.print(" + ");
    							}
    							for(int i=0; i<size; i++){
    								total = total + array[i];
    							}
    							
    							System.out.println("\nTOTAL?");
    							
    							Scanner result = new Scanner(System.in);
    							int result_user = result.nextInt();
    							
    							if(result_user == total){
    								System.out.println("GOOD JOB!");
    							}else{
    								System.out.println("YOUR COUNT IS WRONG!");
    							}
    				}else{
    					System.out.println("you are not a registered users");
    					entry_if_correct = false;
    					System.exit(0);
    					}
    				}
    			}
    		}	
    	}	
    }
    that works BUT... it has a "false validation user". i need to implement a way (better to say a step) in which the digit name will be compared with the name on a list. SO...
    I have create a new class named ControlloUtenti (in english UserChecking)
    and i wrote this (NEW VERSION):

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ControlloUtenti {
    
    	public static void main(String[] args) throws IOException {
    		
    		  BufferedReader bufferedReader = new BufferedReader(new FileReader("path from my pc... usersList.txt"));
    		  
    		  StringBuffer stringBuffer = new StringBuffer();
    		  String line = null;
    		 
    		  while((line = bufferedReader.readLine())!=null){
    		 
    		   stringBuffer.append(line).append("\n");
    		  }
    		   
    		  System.out.println(stringBuffer);
    	}
    }
    this print the list of users, good, this meaning that i see them. now i want modify this for check if a user name digit from the other class is content in this list, and return a true or a false to the other class.

    and now MY QUESTION more precisely:
    1- with the code of this second class i open an existent file .txt, load its content in a buffer and i print all to console.
    but i want:
    in the first class (named ProvaProgetto) the program will ask me a name and will pass this name to this second class (named ControlloUtenti) where THIS CLASS will check if the name is present on the filex txt and will return a value (TRUE O FALSE -> "entry_if_correct") to the other class. how i can make this 2 class communicate? some code will be more easy to understand for me that i'm not english motherlanguage
    Last edited by maki; 02-05-2015 at 04:39 PM.

  14. #14
    maki is offline Member
    Join Date
    Sep 2013
    Posts
    42
    Rep Power
    0

    Default Re: Classes and division of work

    UPDATE and EDIT:
    now i have:
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ControlloUtenti {
    
    	public static void main(String[] args) throws IOException {
    		
    		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!");
    		}
    	}
    }
    this allow me if a name is present in .txt file. well very well.
    now i have to make happen that the request for a name to search is done in the main class (the long code upper) and is this little class that check and return a value.

    how i can connect these two class? and i can correctly set up the values (where and how in code) that travel from a class to another one?
    Last edited by maki; 02-05-2015 at 06:49 PM.

Similar Threads

  1. I think this is Java that has Division ..
    By quidd in forum New To Java
    Replies: 1
    Last Post: 01-20-2012, 07:31 AM
  2. Two classes: can someone tell me why this doesnt work?
    By katiebear128 in forum New To Java
    Replies: 12
    Last Post: 11-08-2011, 04:08 AM
  3. tryting to use switches, classes, and cant get it to work
    By cloutier172 in forum New To Java
    Replies: 4
    Last Post: 10-26-2011, 05:01 AM
  4. int division
    By überfuzz in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 08:10 PM
  5. Issues with Division
    By KnotsaboutWood in forum New To Java
    Replies: 11
    Last Post: 01-31-2011, 04: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
  •