Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help me plss! :(

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help me plss! :(

    here's my code
    import java.io.*;
    class AllOddNum1
    {
     public static void main(String args[])
     {
      try{
     BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter 20 numbers!!"); 
     int num=1;
     while(num<=20)
     {
     num=Integer.parseInt(input.readLine());
     num++;
     }
     if(num%2!=0)
     { 
     System.out.println(num);
     }
     num++;
     }
     catch(Exception e){}
     }
    }
    i need to enter 20 numbers then the odd numbers that i enter will display in the output but my program cant display it. help me..

    output:
    Enter 10 numbers:
    11
    2
    4
    5
    6
    7
    8
    10
    3
    9
    Odd numbers is:11,5,7,9,3

    help me
    Last edited by FutureExpert; January 5th, 2011 at 09:49 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me plss! :(

    "pls" is not a word in the English language.

    When posting code, use the CODE tags to preserve formatting.

    Honestly, I'm not even sure where to start with that code. Why do you only loop 5 times? Why are you only keeping track of one number, then incrementing it, then overwriting it without doing anything with it? Why is your logic for determining whether a number is odd outside of the loop? Why do you increment the variable again if it's even? Why do you have an empty catch block?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Help me plss! :(

    import java.io.*;
    public class AllOddNum1 {
        public static void main(String args[]) {
         try{
            BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter 20 numbers!!");
            int num=1;
            while(num<=20) {
                 int number=Integer.parseInt(input.readLine());
                 if( (number % 2 )!=0) {
                    System.out.println("Odd number: " + number);
                 }
                 num++;
            }
        }
        catch(Exception e){}
        }
    }

  4. The Following User Says Thank You to JavaHater For This Useful Post:

    FutureExpert (January 5th, 2011)

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help me plss! :(

    Wow. For at least the third time today:

    Spoonfeeding. Is. Not. Helping.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me plss! :(

    Thanks Bro! i will try to seperate the odd numbers thanks again.

  7. #6
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Help me plss! :(

    I am not quite sure about whether you want to display the odd number as soon as it's entered.

    What I got is, you are going to enter 10 numbers first constantly. And then once the number feeding is done, you need to display the odd numbers out of the numbers entered.

    If thats the case, then store all the numbers in some list for e.g. ArrayList base on your modulus criteria. And then iterate through the list and display the numbers which are odd.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  8. The Following User Says Thank You to goldest For This Useful Post:

    FutureExpert (January 5th, 2011)

  9. #7
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Help me plss! :(

    API documentation links for your reference

    ArrayList: ArrayList-API

    Java Iterator: Iterator-API

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  10. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help me plss! :(

    Hello FutureExpert.

    Welcome to the Java Programming Forums.

    I like the username, it's always good to be optimistic!

    As Kevin says, there are flaws in your logic but you are getting there.
    Following goldest's posts, I have expanded on your code for you. The numbers entered are now stored in an array.

    Spoonfeeding is not always the best option but seeing as you are new and I'm in a good mood, hopefully this example will help you to understand what you are missing.

    import java.io.*;
    /**
     * JavaProgrammingForums.com
     * Spread the word!
     */
    class AllOddNum1 {
     
    	public static void main(String args[]) {
     
    		try {
    			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));			
    			System.out.println("Enter 20 numbers!!");
     
    			int num = 0;
    			// Setup int array
    			int[] myArray = new int[20];
     
    			while (num <= 19) {
     
    				// Add each number to the array
    				myArray[num] = Integer.parseInt(input.readLine());
    				num++;
    			}
     
    				System.out.print("Odd Numbers: ");
     
    			// Loop through array
    			for(int i = 0; i < myArray.length; i++){
    				if (myArray[i] % 2 != 0) {
    				System.out.print(myArray[i] + " ");
    				}
    			}
     
    		} catch (Exception e) {
    		}
     
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. The Following User Says Thank You to JavaPF For This Useful Post:

    FutureExpert (January 5th, 2011)

  12. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me plss! :(

    Thank you sir..I'm just a beginner in java, i need to know lot more things in java.i got confused in the loopings.

Similar Threads

  1. help with code plss
    By tonka in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 5th, 2009, 08:12 AM