Results 1 to 13 of 13

Thread: sample question

  1. #1
    agater is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default sample question

    hi guys "

    I made this java code that asks the user to put 10 numbers inside an array :

    PHP Code:
     import javax.swing.*;
    public class hhh
    {
    public static void main ()
    {
    int x [] = new int [10];
    String s;
    
    for (int i = 0; i < x.length; i = i + 1)
    {
    s = JOptionPane.showInputDialog("enter a number");
    x [i] = Integer.parseInt(s);
    System.out.println(x[i]);
    }
    }
    }

    and I want to know how to find the numbers that less than 50.

    I know I should write this first :

    if (x[i] < 50)
    {
    but then I don't know the next step.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    It depends on what you mean by "find". Do you want to print them out? Store them in another array? Add them? Output them to file? Something else?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default

    Use an enhanced for statement to loop though the array and test each saved numbers individually and if the number tests to be less then 50 you increment a counter varaible:
    Java Code:
    int numLessThan50 = 0;
    for(int y: x){
           if(x[y] < 50)
            	numLessThan50++;
    }

  4. #4
    agater is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    It depends on what you mean by "find". Do you want to print them out? Store them in another array? Add them? Output them to file? Something else?
    I mean the output , like this :

    " the numbers less than 50 are "

  5. #5
    AlRav is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by agater View Post
    I mean the output , like this :

    " the numbers less than 50 are "
    You could then try something like peter suggested, and add the following:

    Java Code:
    System.out.println("the numbers less than 50 are:");
    int numLessThan50 = 0;
    for(int y: x){
           if(x[y] < 50)
           {
                    System.out.println(x[y]);
            	numLessThan50++;
       }
    }

  6. #6
    agater is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    thanks AlRav and peterhabe for your help.

    the code worked very well .

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I think this code is wrong:
    Java Code:
    int numLessThan50 = 0;
    for(int y: x){
           if(x[y] < 50)
            	numLessThan50++;
    }
    You say:
    for each integer y contained in the integerarray x{
    if( the integer on the y'th position of x < 50 ){...}
    }
    "y" could be 2500 for an array of 10
    Last edited by Jodokus; 07-15-2011 at 12:28 AM.
    No bug ever had to calculate its fitnessfunction.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Jodokus View Post
    I think this code is wrong:
    Yep!

    @peterhabe

    If you are going to spoonfeed code at least make sure it is correct.

  9. #9
    agater is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Jodokus View Post
    I think this code is wrong:

    You say:
    for each integer y contained in the integerarray x{
    if( the integer on the y'th position of x < 50 ){...}
    }
    "y" could be 2500 for an array of 10
    in the first time the code didn't work , but when I changed

    for(int y: x){

    by :

    for (int i = 0; i < x.length; i ++){

    it worked.

  10. #10
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Java Code:
     but when I changed 
    
    for(int y: x){ 
    
    by :
    
    for (int i = 0; i < x.length; i ++){
    
    it worked.
    Can't be. What was your complete loop? (or program, it isn't that big.)

    (Don't hurry: bedtime )
    Last edited by Jodokus; 07-15-2011 at 01:41 AM.
    No bug ever had to calculate its fitnessfunction.

  11. #11
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by peterhabe View Post
    Use an enhanced for statement to loop though the array and test each saved numbers individually and if the number tests to be less then 50 you increment a counter varaible:
    Java Code:
    int numLessThan50 = 0;
    for(int y: x){
           if(x[y] < 50)
                    System.out.println(x[y]);
            	numLessThan50++;
    }
    This is clearly wrong. If you use the enhanced for loop, there is no explicit loop counter - you get each array item in turn. Instead of 'x[y]' in x[y] < 50 and System.out.println(x[y]);, it should just be 'y'.

  12. #12
    agater is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    I wrote the code like this :

    PHP Code:
     import javax.swing.*;
    public class hhh
    {
    public static void main ()
    {
    int x [] = new int [5];
    String s;
    int numLessThan50 = 0;
    float sum = 0;
    float average;
    for (int i = 0; i < x.length; i = i + 1)
    {
    s = JOptionPane.showInputDialog("enter a number");
    x [i] = Integer.parseInt(s);
    if (x[i] < 50)
    {
    numLessThan50 ++;
    System.out.println(" The number of grades less than 50 are " + numLessThan50);
    sum = sum + x[i];
    }
    }
    average = sum / x.length;
    System.out.println(" the average is " + average);
    }
    }
    when I entered 3 nembers less than 50 the output became like this:

    The number of grades less than 50 are 1

    The number of grades less than 50 are 2

    The number of grades less than 50 are 3


    is there anyway I can make the code writes "The number of grades less than 50 are 3 " one time without Repetition.

  13. #13
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    - When I said "Can't be" I ment that you must have changed the contents of the loop, otherwise there would be an undeclared "y". You did.
    - When you post code try to get the indentation right (most IDE's help you do that.)
    - I don't understand your claim that the code runs: the main() has no argument, I never saw it working that way (whether you use the argument or not).
    Then you get:

    Java Code:
    import javax.swing.*;
    public class hhh2{
    	public static void main ( String[] args ){
    		int[] x = new int [5];
    		String s;
    		int numLessThan50 = 0;
    		float sum = 0;
    		float average;
    		for( int i = 0; i < x.length; i++ ){
    			s = JOptionPane.showInputDialog( "enter a number" );
    			x[i] = Integer.parseInt( s );
    			if( x[i] < 50 ){
    				numLessThan50++;
    				System.out.println( "The number of grades less than 50 are " + numLessThan50 );
    				sum = sum + x[i];
    			}
    		}
    		average = sum / x.length;
    		System.out.println( " the average is " + average );
    	}
    }
    is there anyway I can make the code writes "The number of grades less than 50 are 3 " one time without Repetition.
    Sure. Then your printstatement must be moved outside the loop.
    (Try to give the user feedback during dataentry: "you entered the number ... larger then / less then 50".
    Make clear to the user that you only averaged the input under 50. (Already done.)
    If you want to make it difficult: how does the program react when the user enters "potatosalad" in stead of "26"?)
    Last edited by Jodokus; 07-16-2011 at 12:56 PM. Reason: formatting
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. sample jar file
    By etnad7 in forum New To Java
    Replies: 12
    Last Post: 10-28-2010, 08:04 AM
  2. Reg: AWT sample application
    By b_srinath_b in forum AWT / Swing
    Replies: 0
    Last Post: 02-15-2010, 01:06 PM
  3. Sample ListViewer
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:41 PM
  4. TextPane Sample
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •