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 13 of 13

Thread: Pls check my code

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Pls check my code

    Im new to java. Im from the philippines. Please check my code.
    My goal is to accept 10 numbers using array then print only the even numbers afterwards.



    <package javaapplication1;
    import java.util.Scanner;
    public class JavaApplication1 {


    public static void main(String[] args) {
    Scanner od = new Scanner (System.in);
    /* variables */
    int nums [] = new int [10];
    int snum []= new int [10];
    /* code */

    System.out.println("Input 10 Real Numbers and I");
    System.out.println("will display the Even numbers entered");

    for(int i = 0; i <=10; i++){
    nums[i]= od.nextInt();

    if(nums[i]%2==0)
    {
    snum[i] = nums[i];
    }
    nums[i]= i+1;

    }

    for (int x = 0; x < snum.length; ++x)
    System.out.println(snum[x]);



    }




    }

    >


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Pls check my code

    Can you explain what the problem is?
    Do you get errors? Copy the full text of the error messages and paste it here.
    Does the program give wrong results when executed? Copy the full contents of the console and paste it here and add comments saying what is wrong with the output.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    There were no errors. I just cant get the right output.
    The output should look like this:

    Enter 10 numbers...
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    The even numbers are:
    2
    4
    6
    8
    10

    It should look like that.. :screwy:

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Pls check my code

    I just cant get the right output.
    Does the program give wrong results when executed? Copy the full contents of the console and paste it here and add comments saying what is wrong with the output.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Norm View Post
    Does the program give wrong results when executed? Copy the full contents of the console and paste it here and add comments saying what is wrong with the output.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    I have wrapped the code already. :screwy:
    Im getting sick of those braces

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Pls check my code

    I have wrapped the code already
    Where? Did you preview the post before (or after) saving it?
    The code should look like this:
       //------------------------------------------------------------
       //  Reverse a section of a String between b and e
       static String reverse(String s, int b, int e) {
          String res = "";
          for(; e >= b; e--) {    //  start at end and move to left
             res += s.charAt(e);
          }
          return res;
       }

    The < and > are not code tags. The code tags are: [code] and [/code]

    --- Update ---

    Copy the full contents of the console and paste it here and add comments saying what is wrong with the output.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    javamonkey30 (March 14th, 2013)

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Im sorry i cant understand.
    Does it look like this?

    //-----------------------------------

    package javaapplication1;
    import java.util.Scanner;
    public class JavaApplication1 {


    public static void main(String[] args) {
    Scanner od = new Scanner (System.in);
    /* variables */
    int nums [] = new int [10];
    int snum []= new int [10];
    /* code */

    System.out.println("Input 10 Real Numbers and I");
    System.out.println("will display the Even numbers entered");

    for(int i = 0; i <=10; i++){
    nums[i]= od.nextInt();

    if(nums[i]%2==0)
    {
    snum[i] = nums[i];
    }
    nums[i]= i+1;

    }

    for (int x = 0; x < snum.length; ++x)
    System.out.println(snum[x]);
    }
    }

    //-------------------

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Pls check my code

    Does it look like this?
    Can you do a preview BEFORE saving any updates?

    Add this before the code:
    [code]

    then add this after the code:
    [/code]
    See post#6 for how it should look.

    Does the program give wrong results when executed? Copy the full contents of the console from when the program is executed and paste it here and add comments saying what is wrong with the output.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    :
    package javaapplication1;
    import java.util.Scanner;
    public class JavaApplication1 {


    public static void main(String[] args) {
    Scanner od = new Scanner (System.in);
    /* variables */
    int nums [] = new int [10];
    int snum []= new int [10];
    /* code */

    System.out.println("Input 10 Real Numbers and I");
    System.out.println("will display the Even numbers entered");

    for(int i = 0; i <=10; i++){
    nums[i]= od.nextInt();

    if(nums[i]%2==0)
    {
    snum[i] = nums[i];
    }
    nums[i]= i+1;

    }

    for (int x = 0; x < snum.length; ++x)
    System.out.println(snum[x]);



    }




    }

    [/code]

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,949
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Pls check my code

    Add this before the code:
    [code]

    then add this after the code:
    [/code]
    See post#6 for how it should look.

    The beginning code tag is wrong. Take out the /
    The ending tag looks ok.

    Does the program give wrong results when executed? Copy the full contents of the console from when the program is executed and paste it here and add comments saying what is wrong with the output.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Pls check my code

    you create array with fixed length. So the output print with zeros..

    you refer this link: Java array declaration without hard coding the size - Stack Overflow
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  13. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Pls check my code

    ...
    Last edited by copeg; March 18th, 2013 at 03:25 PM. Reason: removed spoonfeeding

  14. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Pls check my code

    @Arun Kumar, please read the forum rules and the following:
    The Problem with Spoon-feeding

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Getting Error in code pls help
    By gokul1242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 09:35 AM
  3. Code snippet pls?
    By srinivas0615 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 30th, 2011, 03:28 AM
  4. Could some pls help me out with this code??
    By Pearl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 12:01 PM
  5. Pls help me to debug this code
    By coen1604 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2011, 10:00 AM