Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Finding smaller number of 2 numbers inputted by user

Options
1235

Comments

  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Head is wrecked at the minute, how do I pass the answer of one method to another method to be calculated?
    Scanner sc = new Scanner(System.in);
    
    
    
    		System.out.println("Please enter num: ");
    		int a = sc.nextInt();
    
    		System.out.println("Please enter num2: ");
    		int b = sc.nextInt();
    
    		System.out.println("Answer is: "+ getNumber(a,b));
    
    		System.out.println("Please enter a num: ");
    		int c = sc.nextInt();
    
    		System.out.println("Please enter another num: ");
    		int d = sc.nextInt();
    
    		myResult = getSum(c,d);
    
    		System.out.println("Answer is: "+getSum(c,d)+myResult);
    
    
    
    
    
    	}
    
    	public static int getNumber(int a,int b)
    	{
    		return a*b;
    	}
    
    	public static int getSum(int c,int d)
    	{
    		return c*d;
    	}
    


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    What are you trying to achieve? Your two methods are performing the same task.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Pelvis wrote: »
    What are you trying to achieve? Your two methods are performing the same task.

    I want to pass the answer from the first method to the second so it can be calculated but I'm not sure how to do it.


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    Both methods return a single int but expect two arguments. So you can pass the answer from the first but what will be your second argument?
    getSum( getNumber(a,b) , ???)
    


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Pelvis wrote: »
    Both methods return a single int but expect two arguments. So you can pass the answer from the first but what will be your second argument?
    getSum( getNumber(a,b) , ???)
    

    I figured it out I think
    import java.util.Scanner;
    public class Practice2{
    	public static void main(String[] args){
    
    			Scanner sc = new Scanner(System.in);
    
    			System.out.println("Please enter a number: ");
    			int num1 = sc.nextInt();
    
    			System.out.println("Please enter a second number: ");
    			int num2 = sc.nextInt();
    
    			System.out.println("Answer is: " +getProduct(num1,num2));
    
    			int sum = getProduct(num1,num2);
    
    
    
    			System.out.println("Please enter another number: ");
    			int num3 = sc.nextInt();
    
    			System.out.println("and just One more: ");
    			int num4 = sc.nextInt();
    
    			int sum2 = getNum( num3, num4);
    
    			System.out.println("Answer is: "+sum * +sum2);
    
    
    
    	}
    
    	public static int getProduct(int num1,int num2)
    	{
    		return num1*num2;
    	}
    
    	public static int getNum(int num3,int num4)
    	{
    		return num3*num4;
    	}
    


  • Advertisement
  • Registered Users Posts: 98 ✭✭haskellgeek


    again your still using duplicating one method instead put a total_count outside your main, or in your main whichever you prefer and ditch the second method like this
    import java.util.Scanner;
    public class Practice2{
    	public static void main(String[] args){
    			Scanner sc = new Scanner(System.in);
    			System.out.println("Pleaser a number: ");
    			int num1 = sc.nextInt();
    			System.out.println("Pleaser a second number: ");
    			int num2 = sc.nextInt();
    			int sum = getProduct(num1,num2);
    			System.out.println("'Answer" + sum);
    			System.out.println("Pleaser another number: ");
    			int num3 = sc.nextInt();
    			System.out.println("and One more: ");
    			int num4 = sc.nextInt();
    			int sum2 = getProduct( num3, num4);
    			int total_count = getProduct(sum, sum2);
    			System.out.println("Answer " + total_count);
    			sc.close();
    	}
    
    	public static int getProduct(int num1,int num2){
    		return num1*num2;
    	}
    }
    


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Lets say you don't want the user to enter integer values, how would catch that?
    import java.util.Scanner;
    import java.util.InputMismatchException;
    public class Country{
    
    	public static void main(String[]args){
    
    
    		boolean isValid = true;
    		String Country1 = "Ireland";
    		String Country2 = "England";
    		String Country3 = "France";
    
    		countryOfOrigin(isValid,Country1,Country2,Country3);
    	}
    
    	public static void countryOfOrigin(boolean isValid,String Country1,String Country2,String Country3){
    
          Scanner sc = new Scanner(System.in);
          do{
             try{
    	  		System.out.println("Please enter your country of origins: ");
    	  	    String Country = sc.nextLine();
    	  	    int Country6 = Integer.parseInt(Country);
    
    	  	    if(Country.equals("Ireland") || Country.equals("England") || Country.equals("France")){
    	  	       System.out.println("Thank");
    	  	       isValid = true;
    	  	    }
    	  	    else{
    	  	    	System.out.println("Wrong,please enter another country again: ");
    	  	    	isValid = false;
    	  	    }
    	  	}
    	  	catch(InputMismatchException e)
    	  	{
    	  		System.out.println("Please enter strings only!");
    	  	}
    	  }while(!isValid);
    	}
    
    
    


  • Posts: 0 [Deleted User]


    Quick and durty, off the top of my head :
    try {
        Integer.parseInt(Country);
    } catch (NumberFormatException nfe) {
        System.out.println("Hey assbutt, " + Country + " is not an 'int'");
    }
    

    JavaDoc References:
    However, a better idea would be to validate the input String (generally considered bad practice to use Exceptions to control program flow):
    if ( /* Your code here */ ) {
        System.out.println("Hey assbutt, " + Country + " is not an 'int'");
    }
    

    JavaDoc References:


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Is there anyway to get view the code of a .exe file?


  • Registered Users Posts: 6,236 ✭✭✭Idleater


    Is there anyway to get view the code of a .exe file?

    Look up decompiler or debugger


  • Advertisement
  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Idleater wrote: »
    Look up decompiler or debugger

    Looks complicated enough, I'll leave it for now I think.


  • Registered Users Posts: 6,236 ✭✭✭Idleater


    Looks complicated enough, I'll leave it for now I think.

    Indeed, c decompiling is an art in itself. I wouldn't have the faintest idea where to start, but knew someone who was very good at it.

    What is it that you would like to understand from that program? Perhaps that is an easier question for the interwebnet to answer.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Idleater wrote: »
    Indeed, c decompiling is an art in itself. I wouldn't have the faintest idea where to start, but knew someone who was very good at it.

    What is it that you would like to understand from that program? Perhaps that is an easier question for the interwebnet to answer.

    We're doing a testing module ATM and our instructor gave us a .exe file to test and do test logs etc but I'm interested to see what the code looks like that's all really :p I love reading other people's code and trying to understand it.


  • Posts: 0 [Deleted User]


    I love reading other people's code and trying to understand it.

    Don't worry, the novelty will wear off.

    :D


  • Registered Users Posts: 27,053 ✭✭✭✭GreeBo


    I love reading other people's code and trying to understand it.

    Wait until you have been coding a few years, then you get to look at crappy code and realise that the "other people" in this case are you. :eek::o


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    We're doing a testing module ATM and our instructor gave us a .exe file to test and do test logs etc but I'm interested to see what the code looks like that's all really :p I love reading other people's code and trying to understand it.

    A decompiler won't give you a clean version of the code though, some of it would be garbage.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Just reviewing an exercise we were given and below is the instructors solution, I didn't have a chance to ask him, Can anyone tell me why he put int max = Integer.MIN_VALUE; and why int min = Integer.MAX_VALUE; I thought it would have been the other way round, int max = Integer.MAX_VALUE and min = Integer.MIN_VALUE, the program takes in 10 integers and gives back the min and max, the sum of the 10 numbers and the Average.
    import java.util.Scanner;
    
    public class TenStats {
    
    	private final int num_nums = 10;
    
    	public static void main(String[] args) {
    		new TenStats().getUpAndRun();
    	}
    
    	public void getUpAndRun() {
    		Scanner in = new Scanner(System.in);
    		int num = 0;
    		int max = Integer.MIN_VALUE;
    		int min = Integer.MAX_VALUE;
    		int tot = 0;
    		for(int i = 1; i <= num_nums; i++) {
    			System.out.print("Enter integer " + i + ": ");
    			num = in.nextInt();
    			tot += num;
    
    			if (num < min)min = num;
    
                if (num > max)max = num;
    
    
            System.out.println("\n\n");
    		System.out.println("Sum: " + tot);
    		System.out.println("Avg: " + tot / num_nums);
    		System.out.println("Max: " + max);
    		System.out.println("Min: " + min);
    		in.close();
    


  • Closed Accounts Posts: 1,758 ✭✭✭Pelvis


    Let's assume the following...

    Integer.MIN_VALUE = -999
    Integer.MAX_VALUE = 999

    Therefore your max is -999 and min is 999. Now let's say your first num input is 10.
    if (num < min)min = num;
    

    This would therefore be...
    if (10 < 999)min = 10;
    

    this evaluates to true and so your min value will be changed. If you initialized your min value to be Integer.MIN_VALUE, then if (num < min) will always evaluate to false, as no number you input will be less than the lowest possible number.

    Same logic with the max value.

    Have you done much debugging? Stepping through the code and seeing the variables change as you go really helps to clear up what's happening.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Pelvis wrote: »
    Let's assume the following...

    Integer.MIN_VALUE = -999
    Integer.MAX_VALUE = 999

    Therefore your max is -999 and min is 999. Now let's say your first num input is 10.
    if (num < min)min = num;
    

    This would therefore be...
    if (10 < 999)min = 10;
    

    this evaluates to true and so your min value will be changed. If you initialized your min value to be Integer.MIN_VALUE, then if (num < min) will always evaluate to false, as no number you input will be less than the lowest possible number.

    Same logic with the max value.

    Have you done much debugging? Stepping through the code and seeing the variables change as you go really helps to clear up what's happening.

    Cheers! makes sense.


  • Registered Users Posts: 22,015 ✭✭✭✭Esel


    Is your instructor following this thread? ;):D

    Not your ornery onager



  • Advertisement
  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Esel wrote: »
    Is your instructor following this thread? ;):D

    Who knows :eek:


    *waves*


    :pac:


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    The code below basically takes in two numbers and there's a method that checks the first number entered isn't -1, then it should add both number and return the result, instead though it just keeps asking to enter a number until -1 is entered and then breaks out of the loop and then it asks for the second number, how do I get it to break out of the loop after a number is entered and add the two numbers together?
    public static int num1;
             static void Main(string[] args)
            {
                
                Boolean exit = true;
                
                while(exit){
                    num1= getNum1();
                    exit = checkNum(num1);
                    
                }
               
                int num2 = getNum2();
    
                int result = Add(num1,num2);
    
                Console.WriteLine("Result: " +Add(num1,num2));
               
            }
            public static int getNum1(){
                Console.WriteLine("Please enter a number: ");
                num1 = Convert.ToInt32(Console.ReadLine());
    
                return num1;
            }
            public static Boolean checkNum(int num1){
                if(num1 == -1){
                    Console.WriteLine("Exiting......");
                    return false;
                    
                }else{
                    return true;
                }
            }
            public static int getNum2(){
                Console.WriteLine("Please enter a second number: ");
                int num2 = Convert.ToInt32(Console.ReadLine());
    
                return num2;
            }
            public static int Add(int num1,int num2){
                 return num1+num2;
            }
            
        }
        
    }
    


  • Registered Users Posts: 772 ✭✭✭pillphil


    What's the purpose of the loop and the check for -1?

    Should the program exit entirely if you enter -1?


  • Closed Accounts Posts: 2,910 ✭✭✭begbysback


    The only escape from the while loop is if -1 is entered - Checknum keeps returning true unless -1 is entered, therefore while loop keeps running


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    pillphil wrote: »
    What's the purpose of the loop and the check for -1?

    Should the program exit entirely if you enter -1?

    Yes it should exit entirely if -1 or less is entered,other it should continue on and ask for second number and then calculate them.


  • Registered Users Posts: 772 ✭✭✭pillphil


    Yes it should exit entirely if -1 or less is entered,other it should continue on and ask for second number and then calculate them.

    In that case, do you need the loop? it would make sense if you were checking for non-numerical characters being entered, but since you're not, can you just remove it? or do you intent to add that functionality?


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    The problem you're facing is an obvious one. Since you're learning I think that sometimes giving a nudge in the right direction is better than giving an answer. If you draw out and use pseudo code to describe the problem you're trying to solve the answer should become obvious. You might find you learn a lot more this way


  • Registered Users Posts: 974 ✭✭✭Remouad


    So I think the first thing you need to do is define the problem.
    Are you trying you add 2 numbers once or repeatedly until -1 is entered

    As for the issue you're having the following bit of code is your problem
    while(exit){
                    num1= getNum1();
                    exit = checkNum(num1);
                    
                }
               
    

    This will keep looping, asking for the first number, until you enter -1 (exit=false)
    There is no logical path for adding the 2 numbers together.

    The following code will add the numbers in a loop until -1 is entered for the first number (note: num1 doesn't need to be static).
    public static void Main(string[] args)
            {
                int num1, num2;
                Boolean exit = true;
                while(exit){
                    num1= getNum1();
                    exit = checkNum(num1);
    
                    if(exit){            
                        num2 = getNum2();
        
                        Console.WriteLine("Result: " +Add(num1,num2));                
                    }else{
                        Console.WriteLine("Exiting");
                    }                 
                }
            }
               
    


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    The problem you're facing is an obvious one. Since you're learning I think that sometimes giving a nudge in the right direction is better than giving an answer. If you draw out and use pseudo code to describe the problem you're trying to solve the answer should become obvious. You might find you learn a lot more this way

    This is a big problem for me, instead of sitting down and really analyzing the problem and writing it out I just jump in and try to write code straight away and it gets me nowhere.


  • Advertisement
  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    This is a big problem for me, instead of sitting down and really analyzing the problem and writing it out I just jump in and try to write code straight away and it gets me nowhere.

    If you want to improve as a developer, then this is a skill that you should learn. It'll be invaluable in the future.


Advertisement