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

Java Question

Options
  • 16-11-2012 9:47pm
    #1
    Closed Accounts Posts: 31,152 ✭✭✭✭


    Only started java a couple of weeks ago so forgive me for this mess but Im stuck...

    Q is Question 4:
    Write a Java program called OddEven.java. This program should take in a number from the user and determine if the number is odd or even. If the number is odd, multiply the number by 4. If it is even, multiply it by 3.

    Ive written this so far but not sure where Im going wrong, Im a beginner and I know its probably all over the place so please go easy :o
    /*
     *OddEven.java
     *16/11/12
     */
    
     import java.io.*;
    
     public class OddEven{
    	 public static void main(String[]args){
    
    		 BufferedReader bReader = new BufferedReader ( new InputStreamReader (System.in));
    
    		 int number1, ans1, ans2;
    		 String number1String;
    
    				//read in number
    			 try{
    				 System.out.println("Please enter a number: ");
    			 number1String = bReader.readLine();
    			 number1 = int.parseInt (number1String);
    			 
    				//if number is divisable by 2 its even, multiply by 3
    			if(number1%2=0){
    				ans1 = number1*3;
    				System.out.println("You enter an even number and multiplied it x 3, the answer is: "+ans1);
    			}
    				//if number is not divisable by 2 its odd, multiply by 4
    			else if(number1%2!=0){
    				ans2 = number1*4;
    				System.out.println("You have entered an odd number and multiplied it x 4, the answer is: "+ans2);
    			}
    			catch(NumberFormatException E){
    						 		System.out.println("Error");
    						 			System.out.println(E.toString());
    			}
     		}
     	}
    }
    
    
    


Comments

  • Registered Users Posts: 3,739 ✭✭✭johnmcdnl


    Line 23 in the if statement should be == I think rather than = I think.. Your basically assigning number1%2 0 rather than comparing them. Used to make that mistake a few times myself :p

    I think that's it but tbh I haven't looked at code in 6 months so that could be completely wrong but hope it helps.


  • Closed Accounts Posts: 31,152 ✭✭✭✭KERSPLAT!


    Have 9 errors :o that hasnt changed it, seems line 20
    number1 = int.parseInt (number1String);
    
    is giving most trouble, thanks though :)


  • Registered Users Posts: 3,739 ✭✭✭johnmcdnl


    IPAM wrote: »
    Have 9 errors :o that hasnt changed it, seems line 20
    number1 = int.parseInt (number1String);
    
    is giving most trouble, thanks though :)

    Integer.parseInt instead of int.parseInt should sort that out for you ;) you almost were there.


  • Registered Users Posts: 3,636 ✭✭✭dotsman


    IPAM wrote: »
    Have 9 errors :o that hasnt changed it, seems line 20
    number1 = int.parseInt (number1String);
    
    is giving most trouble, thanks though :)

    johnmcdnl is correct with regards the ==. A single = is used to assign a value, a == is used to test equality between 2 values.

    With regards line 20,
    number1 = int.parseInt (number1String);
    

    This should read:
    number1 = Integer.parseInt (number1String);
    

    You need to remember that an int is a primitive while an Integer is a (Wrapper) object. A primitive has no methods, whereas an object does. An Integer object (as it is a Wrapper) can then be easily treated as an int.

    Your also have your closing brackets messed up. The catch block should be outside the try block,not inside like it currently is. I'm not sure if it's just how the code is posted on this thread, but you should really get into the habit of indenting the code correctly as it will make things like this far easier to spot. While the Sun official recommendation is for 1TBS style, I think it makes code far easier to read if you use the Allman style (which style to choose often leads to infinite debate but, to me, the disadvantages to Allman no longer apply in a modern development setting, and the advantages of code readability are one of the biggest factors (if not the biggest) when it comes to writing good code. Ultimately, as I assume you are a student, you will need to drill this into yourself now, because if you get into a bad habit, it is very hard to get out of. For more about indentation styles, see here

    Finally, once you have the above tidied up, you will also need to add a second catch block to pick up the IOException that the StringBuffer.readline() throws.


  • Closed Accounts Posts: 31,152 ✭✭✭✭KERSPLAT!


    Wow... Thanks for taking the time to reply! I'm not at the laptop now but I'll give that a go. Your right I'm a student, I do try to indent the code but when I get errors I mess it all up :o also I haven't been shown the correct way to do it so I'll look up your suggestion

    Thanks again :)


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Just right click and hit format to indent


  • Closed Accounts Posts: 31,152 ✭✭✭✭KERSPLAT!


    All sorted, thanks everyone! Wasnt too far off was I?! :D


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Try to work on not messing up your indentation, it will help you so much in the future to have nice readable code.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    dotsman wrote: »
    While the Sun official recommendation is for 1TBS style, I think it makes code far easier to read if you use the Allman style (which style to choose often leads to infinite debate but, to me, the disadvantages to Allman no longer apply in a modern development setting, and the advantages of code readability are one of the biggest factors (if not the biggest) when it comes to writing good code.

    I have always used the Allman style but lately I have been trying to use K&R because of the sheer popularity of it. I just don't get it, its horrible and counter productive.


Advertisement