Results 1 to 12 of 12
  1. #1
    anonymous445 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default loop terminated sentinal that would put the values you enter in a table.

    Java Code:
    import java.lang.*;
    import java.util.Scanner;
    public class TABLETEST {
        public static void main(String args[])
        {
               Scanner clavier = new Scanner(System.in);
               int numero=clavier.nextInt();
               int [] liste1=new int[numero];
               int x;
               for(x=0; numero!=10;x++)
               {
                   numero=clavier.nextInt();
                    liste1=new int[numero];
               }
               System.out.println("\n\nNuméros");
               System.out.println("-------");
    
               for(x=0; numero !=10;x++)
               {
                   System.out.println(liste1[x]+"     ");
               }
        }
    }
    Hello, in this code, i'm reading a series of numbers with a loop terminated sentinal. The loop that read the numbers and the sentinal works well. The sentinal terminates the loop like its supposed to. I'm trying to figure out really bad why the numbers doesn't show up in the table. Only the println show up for the title of the table. Any ideas of how the loop could write the values you enter in the table. Thats what it does right now when you enter the values, and after you enter 10 to terminate the loop.



    5
    6
    5
    10


    Numéros
    -------




    Any idea of how i should do it????? Because i have no clue. Thanks.

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

    Default

    Each time around the loop you create a new array instead of storing the value in the existing array.

  3. #3
    anonymous445 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Each time around the loop you create a new array instead of storing the value in the existing array.
    really??? how could i store the value in the existing array???

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

    Default

    Are you serious?

    You know how to assign a value to a variable (you have done it when assigning to numero) and you know how to access a value in an array (you have done it in the loop to print out values) so just combine the two concepts together.

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by anonymous445 View Post
    really??? how could i store the value in the existing array???
    if you want assign a value to an array element, simply do:
    Java Code:
    liste1[0] = 23;
    liste1[1] = 17;
    if you want to use a loop to populate an array of int called liste1, you can use a for loop like follows:

    Java Code:
    for(int x = 0; x < liste1.length; x++)  {
       liste1[x] = some value;
    }
    You must read up more on how to access arrays, please see this link for your reference: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Best,
    --user0--

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

    Default

    I could have posted that too but don't you think they would learn more if they worked it out themselves?

  7. #7
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    I could have posted that too but don't you think they would learn more if they worked it out themselves?
    You're right they will definitely learn more if they learn themselves. I figured it's a simple task to access an element of an array - and in this case, the OP really had no clue. I didn't give an entire solution. I simply showed the OP how to access an element of an array using a loop. OP will have to work out the specific solution.
    --user0--

  8. #8
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Besides, the OP showed some effort, and wrote code(which I admit is partially incorrect) but hey it's better than some people who post here asking for all the code without showing any effort on their part.

    Best,
    --user0--

  9. #9
    anonymous445 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    hum this example was an example that i created myself to try to understand how can a loop populate a table. Because yea, we have an assignment based on this so i made a simple example and tried to figure it out. Ive spent countless hours figuring out, its just that we have started java a couple of days ago. Its similar to c# its just that we didn't see how to use the tables.

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

    Default

    Java doesn't have tables.

  11. #11
    anonymous445 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java doesn't have tables.
    then whatever its called the thing where you put the numbers for example.


    For example

    Java Code:
    import java.lang.*;
    import java.util.Scanner;
    public class testtableau1 {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            int [] studentnum;
            System.out.print("How many numbers you want? ");
            int nbnb=keyboard.nextInt();
            studentnum=new int[nbnb];
            for (int x=0; nbnb >x; x++)
            {
             studentnum[x] = keyboard.nextInt();
            }
            System.out.println("Numéro");
            System.out.println("-----------");
    
            for (int x=0; x<nbnb;x++)
            {
                System.out.println(studentnum[x] +                   "            ");
            }
    
        }
    }
    Like in this code, you enter the number of numbers that you want to enter, enter them and it puts them in the table. Its the just sentinal confusing me. The two loops both have the same condition. idk.

  12. #12
    anonymous445 is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

Similar Threads

  1. use of ajax and jsp to extract values from a table
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-27-2010, 11:26 AM
  2. Replies: 7
    Last Post: 11-22-2010, 02:38 PM
  3. Replies: 6
    Last Post: 08-26-2010, 02:26 PM
  4. Replies: 18
    Last Post: 11-29-2009, 11:58 PM
  5. Replies: 21
    Last Post: 06-11-2009, 02:18 AM

Posting Permissions

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