1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

New To JAVA please Help

Discussion in 'Software Development' started by BKB83, Jan 27, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. BKB83

    BKB83 Thread Starter

    Joined:
    Jan 27, 2011
    Messages:
    1
    I am currently teaching myself Java & I've run across a problem; I'm attempting to write program to allow the user to choose a mathematical operation e.g add, subtract whatever then proceed to enter two integers & then the output is displayed which would be the total ; however the program debugs fine but the problem I'm coming across is the Sum will not print & if I input a # above 4 i put in a if statement to ask for only an operation listed 1-4 to prevent user error ; however when I put a number into that prompt (the prompt that states please enter 1-4 if not done correctly the 1st time) the program will not go to next prompt (asking for integers) this is my third Java program I know its kinda early to b stumped but I am ;
    Please help & any advice for learning Java would be greatly appreciated.


    import java.util.Scanner;

    // Program to do 4 mathmatical operations


    public class Operations {

    public static void main(String[] args)

    {
    Scanner input = new Scanner (System.in); // Allows to read information

    int Choice; // What operation you pick

    int Number1; // 1st number

    int Number2;// 2nd number

    System.out.print( " Please Choose 1 of the following operations for your integers : 1- Add , 2- Subtract, 3- Multiply, 4-Divide " ); // Prompt for Choice

    Choice = input.nextInt(); // Read Choice From Prompt

    if ( (Choice > 4 ) || ( Choice < 1 ) )
    {

    System.out.print(" Please Choose An Operation Numbered 1 - 4 : " );

    Choice = input.nextInt(); // Read Choice Prompt If 1st Choice Not Available

    }

    else
    {

    System.out.print(" Please Enter 1st Integer : " ); // Prompt 1st Number

    Number1 = input.nextInt();

    System.out.print(" Please Enter 2nd Integer : " ); // Prompt 2nd Number

    Number2 = input.nextInt();

    if ( Choice == 1 ) { // Addition Choice

    System.out.printf(" Your first integer added to your second integer is : " , (Number1 + Number2) ) ; // Provides Sum

    }

    else if ( Choice == 2 ) {// Subtraction Choice

    System.out.printf(" Your first integer Subtracted by your second integer is : " , (Number1 - Number2) ); // Provides Sum
    }

    else if ( Choice == 3 ) {// Multiplication Choice

    System.out.printf(" Your first integer multiplied by your second integer is : " , (Number1 * Number2) ); // Provides Sum
    }

    if ( Choice == 4 ) { // Division Choice

    System.out.printf(" Your first integer divided by your second integer is : " , (Number1 / Number2) ); // Provides Sum
    }

    }

    } // End method main

    }// end class
     
  2. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    696
    This is probably best for the Software Development forum. Click on "Report" at the bottom right of your post and ask that your question be moved to that forum. You'll probably get more help.

    I'm no Java programmer, but the scenario of entering an operation numbered >4 prompts for a valid number, but then exits. You'll need something much more robust. In Java, you should be able to do something like this pseudo-code:

    Code:
    while ( input not valid ) {
      prompt for valid input
    }
    do stuff
    Sorry, can't help with the actual coding in Java...
     
  3. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    yes, please move this to Software Development section. You have post this in web designing section
     
  4. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Hi there,

    First of all the reason that the result does not display is because of the way printf works. The first argument you give is the format and then the following arguments are the values you want printed. In the first argument, the format, you have text but to include a number that is specified in the next argument, you need to place %d. (more info here http://www.java2s.com/Tutorial/Java/0120__Development/UsingJavasprintfMethod.htm)
    Code:
    System.out.printf(" Your first integer added to your second integer is : %d", (Number1 + Number2) ) ; // Provides Sum
    However for your program you can just use the println function as it is simper in this case:
    Code:
    System.out.println(" Your first integer added to your second integer is : " + (Number1 + Number2) ) ; // Provides Sum
    The reason that it only asks you to enter correct number for the first time is because after your if statement that checks if the input is in the range 1-4, your code waits for the next input and then reaches the end of main. To get this to work as intended, you can use a while loop like in the following code:
    Code:
    import java.util.Scanner;
    
    // Program to do 4 mathmatical operations 
    
    
    public class Operations {
    
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner (System.in); // Allows to read information
    		int Choice; // What operation you pick 
    		int Number1; // 1st number
    		int Number2;// 2nd number
    		
    		System.out.print( " Please Choose 1 of the following operations for your integers : 1- Add , 2- Subtract, 3- Multiply, 4-Divide " ); // Prompt for Choice 
    		
    		Choice = input.nextInt(); // Read Choice From Prompt 
    		
    		while((Choice > 4) || (Choice < 1)) 	//this will check if Choice is between 1 and 4, if it isn't, it will execute the code, if it is, the loop will be skipped
    									//if the user continually enters an incorrect number, the loop will be executed until correct input is recieved
    		{
    			System.out.print(" Please Choose An Operation Numbered 1 - 4 : " );
    			
    			Choice = input.nextInt(); // Read Choice Prompt If 1st Choice Not Available 
    	
    		}
    		
    		System.out.print(" Please Enter 1st Integer : " ); // Prompt 1st Number 
    		
    		Number1 = input.nextInt();
    		
    		System.out.print(" Please Enter 2nd Integer : " ); // Prompt 2nd Number
    		
    		Number2 = input.nextInt();
    		
    		switch(Choice)		//a switch statement takes the value you want to query against other values,
    						//the code between the case will a matching number and  the break statements will be run.
    						//e.g. if Choice was 2, the code under case 2: would be executed
    		{
    			case 1:
    				System.out.println(" Your first integer added to your second integer is : " + (Number1 + Number2) ) ; // Provides Sum
    				break;
    			case 2:
    				System.out.println(" Your first integer Subtracted by your second integer is : " + (Number1 - Number2) ); // Provides Sum
    				break;
    			case 3:
    				System.out.println(" Your first integer multiplied by your second integer is : " + (Number1 * Number2) ); // Provides Sum
    				break;
    			case 4:
    				System.out.println(" Your first integer divided by your second integer is : " + (Number1 / Number2) ); // Provides Sum 
    				break;
    		}
    	
    	} // End method main
    
    }// end class
    The things that I've changed from you code is the use of the while loop. The code in the loop will run every time the user enters incorrect input.
    I'm using a switch statement instead of your if statements. I've explained that in the comments

    Two notes of advice about the division one. Firstly you should be checked if Number2 is 0, because if it is and you try to divide by it, java will through a divide by zero exception. You can use a simple if statement to prevent that.

    Secondly, as both Number1 and Number2 are ints, the value of the division will not be accurate. For example if you enter 25 as #1 and 9 as #2, it will output 2. To get exact division results, you could change the types of Number1 and Number2 to double.


    As for general tips about learning java, I would definitely indent your code. This not only helps you organise it, it helps other who are helping you to understand what is going on :)

    I would also recommend following a good tutorial series. http://www.thenewboston.com/?cat=36&pOpen=tutorial, is good and comprehensive but there are loads out there

    Andy
     
  5. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
  6. shannon08

    shannon08

    Joined:
    Jul 30, 2008
    Messages:
    355
  7. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
  8. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Hi BKB83,

    It's been some time you have created this thread. what is the status now? Solved or Not?
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/977380

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice