Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. why can't I figure out array input

why can't I figure out array input

Scheduled Pinned Locked Moved Java
javadata-structureshelpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LORDA12
    wrote on last edited by
    #1

    I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.

    package biznes;
    import java.io.InputStream;

    public class Vavejdane {

    void Input() {

    int[] client = new int[10];

    for ( int i : client)
    {
    System.out.println("Vavedi masiva");
    System.in.read(client[i]);
    }

    }

    }

    C L N M 4 Replies Last reply
    0
    • L LORDA12

      I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.

      package biznes;
      import java.io.InputStream;

      public class Vavejdane {

      void Input() {

      int[] client = new int[10];

      for ( int i : client)
      {
      System.out.println("Vavedi masiva");
      System.in.read(client[i]);
      }

      }

      }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      LORDA12 wrote:

      for ( int i : client)

      You are iterating over all integers stored in the array. Which means that i will take the values of the integers currently in the array (which are not initialized), which means i will have an undertermined value. The you use this i to access elements of your array, which is wrong. You should write your loop this way:

      for ( int i=0; i

      (not sure anymore if it is client.size or client.lenght, you have to look it up since I do not use arrays this way much often).

      Cédric Moonen
      Software developer
      Charting control [v3.0]
      OpenGL game tutorial in C++

      L 1 Reply Last reply
      0
      • C Cedric Moonen

        LORDA12 wrote:

        for ( int i : client)

        You are iterating over all integers stored in the array. Which means that i will take the values of the integers currently in the array (which are not initialized), which means i will have an undertermined value. The you use this i to access elements of your array, which is wrong. You should write your loop this way:

        for ( int i=0; i

        (not sure anymore if it is client.size or client.lenght, you have to look it up since I do not use arrays this way much often).

        Cédric Moonen
        Software developer
        Charting control [v3.0]
        OpenGL game tutorial in C++

        L Offline
        L Offline
        LORDA12
        wrote on last edited by
        #3

        The problem is not there. Java initializes automatic to 0. The problem is in System.in.read(). I don't know what parameters I need to input the array from the keyboard.

        C 1 Reply Last reply
        0
        • L LORDA12

          The problem is not there. Java initializes automatic to 0. The problem is in System.in.read(). I don't know what parameters I need to input the array from the keyboard.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          LORDA12 wrote:

          The problem is not there. Java initializes automatic to 0.

          There's certainly a problem there anyway: first you shouldn't relly on initialization, this is a very habbit, and second even if everything is initialized at 0, it means that you will always store your character in the first element of your array (all the elements of your array are initially 0, so if you iterate over them and do client[i], it will always point to the first element of your array). Furthermore, did you look at the documentation of System.in.read ? It doesn't accept any parameter and returns a character. So, you have to write it this way:

          client[i] = System.in.read();

          Also, as it is returning a character, you should store the return values inside a character array. What do you want to do exactly ? Ask the user to type in numbers (so, he can enter several characters for one element of your entry) or ask the user to type in characters (one character for each element of your array) ? This makes a big difference.

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • L LORDA12

            I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.

            package biznes;
            import java.io.InputStream;

            public class Vavejdane {

            void Input() {

            int[] client = new int[10];

            for ( int i : client)
            {
            System.out.println("Vavedi masiva");
            System.in.read(client[i]);
            }

            }

            }

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            LORDA12 wrote:

            I've read all articles in the internet

            Really? all 24 billion of them? Wow, you should apply for an entry in the Guinness Book of Records. Did you look in the official Java documentation? the page on System.In? that explains how Read() works. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            1 Reply Last reply
            0
            • L LORDA12

              I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.

              package biznes;
              import java.io.InputStream;

              public class Vavejdane {

              void Input() {

              int[] client = new int[10];

              for ( int i : client)
              {
              System.out.println("Vavedi masiva");
              System.in.read(client[i]);
              }

              }

              }

              N Offline
              N Offline
              Nagy Vilmos
              wrote on last edited by
              #6

              Try this:

              client[i] = System.in.read();

              [editied so it'll work]


              Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

              modified on Monday, November 8, 2010 10:01 AM

              1 Reply Last reply
              0
              • L LORDA12

                I've read all articles in the internet, but couldn't succeed in array input. it gives me an error at System.in.read(client[i]); Can you tell me the simpliest way to input int array? Thanks.

                package biznes;
                import java.io.InputStream;

                public class Vavejdane {

                void Input() {

                int[] client = new int[10];

                for ( int i : client)
                {
                System.out.println("Vavedi masiva");
                System.in.read(client[i]);
                }

                }

                }

                M Offline
                M Offline
                moxwose
                wrote on last edited by
                #7

                " System.in.read(client[i]);" it can be compiled? according my experience ,you may do like : DataInputStream dIn = new DataInputStream(System.in); for ( int i = 0; i<10; i++) { client[i] = dIn.readInt(); }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups