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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: Input in a two Dimensional Array.

  1. #1
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Input N number of times!!!

    Can anyone give me idea of getting input 'N' number of times with a space difference between every input.
    Forexample:
    1 2 3 4 . . .

    Note: We don't know the 'N' initially and i want to get input exactly 'N' number of times.

    Kindly give me an idea.
    Thanks
    Last edited by Mr.777; December 9th, 2011 at 09:39 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    When do you find out the value of 'N'?
    If the input record contains more than 'N' values, how will you know when you have enough and can ignore the rest of the values?

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    Quote Originally Posted by Norm View Post
    When do you find out the value of 'N'?
    If the input record contains more than 'N' values, how will you know when you have enough and can ignore the rest of the values?
    Let's assume, i will start my program and get a number from user say 'N'
    And no, input record can not contain more than 'N' values. All values will be space separated. I've managed to do this by reading it in BufferedReader as a String and then after performing some operations, i however able to get this but i want efficient way of getting such input.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    i however able to get this but i want efficient way of getting such input.
    Sorry, where is the problem. You ask the user for the value of N and then read in N values from the user. All the values would have to be separated to be recognized as separate values.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    I don't want to read String everytime. What if i need to read integer values separated by a space each, 'N' times. I am looking for efficient method to read Integers separated by space 'N' times.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    I don't want to read String everytime. What if i need to read integer values
    If you are reading from the keyboard, you can not read integer values. Everything typed in is a character.
    How is the data entered for you to read? Is there a person at the keyboard that is entering the numbers?

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    How is the data entered for you to read? Is there a person at the keyboard that is entering the numbers?
    Yes, user will enter through Keyboard.
    Last edited by Mr.777; December 12th, 2011 at 07:45 AM. Reason: Wrong Code

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    Then the program will be getting Strings.

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    Sorry Norm, i know i am not able to explain what i need. Let me try once more... uhh......huh
    What i need is, the most efficient way of getting 'N' times input separated by a space between each input and place it in the array at the same time. I don't want to read in a String first and then after some operations, move it to the array. In short i want to get the most efficient way of doing this.
    I think i am quiet clear now Well, if not, at least for me, i am quiet clear now
    Thanks alot for your time Norm.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    There are two parts to this:
    Read the input from the user
    Store the input into an array.

    I don't want to read in a String first and then after some operations, move it to the array.
    That is the way it is done. Read a String, convert it to int, store it in the array.

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

    Mr.777 (December 12th, 2011)

  12. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    Okay, may be there is nothing what i think of achieving in it. Thanks for the time Norm.

  13. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Input in a two Dimensional Array.

    I'm still confused. How will the data be entered?
    1[enter]2[enter]3[enter]
    or
    1 2 3[enter]

    If user enters how many values will be entered 'N' then simply loop 'N' times for the first case. For the second case read entire String and split.
    Improving the world one idiot at a time!

  14. #13
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    If user enters how many values will be entered 'N' then simply loop 'N' times for the first case. For the second case read entire String and split.
    1 2 3 (Enter)
    This is how data will be entered.
    And i am doing exactly like this but the problem is i want to know the most efficient way of getting input like this. Do you know any?

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    the most efficient way of getting input like this.
    Read the String, separate the words, convert the words to int, store the int in the next slot in the array.

    What do you mean by "most efficient"?
    Fewest lines of java code?
    Fastest execution time?

  16. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    Quote Originally Posted by Norm View Post
    Read the String, separate the words, convert the words to int, store the int in the next slot in the array.

    What do you mean by "most efficient"?
    Fewest lines of java code?
    Fastest execution time?
    Ofcourse Fastest execution time. I don't care for lines of code actually.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    Have you found any other solutions than what have been posted so far?

  18. #17
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    Yes, and that is
    int val;
    int [] arr= new int[5];
    int i=0;
    while((val=System.in.read())==' '){
    arr[i]=val;
    i++;
    }
    But i am not sure if it's fast enough. Is it?

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    Add some code to time it.
    What do you get in the array when you execute the code?

  20. #19
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    What do you get in the array when you execute the code?
    The problem is i can not get data inside array. Array still prints 0 as it's not even assigned any value. I know this will read character as it prints me ASCII but i somehow unable to manage to move it out of loop. It keeps getting input and doesn't move inside the loop as condition satisfies.

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    Read into a variable and print out the contents of the variable before using the data in the condition of a while loop. You need to see what read() returns.

  22. #21
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    It doesn't even get inside the loop I don't know why.
    Also, if i do
    int val=0;
    try{
    val=System.in.read();
    }catch(Exception e){
    System.out.err(e.getStackTrace());
    }
    System.out.print(val);
    It prints the ASCII value which clearly means that it reads it as a character.

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    What value does the variable need to have to get into the loop?

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

    Mr.777 (December 14th, 2011)

  25. #23
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    while((val=System.in.read())==' '){
    //here
    }
    If i am not wrong, it's an empty character. Damn :@
    Norm, thanks, i got it. I must get space ASCII How idiot i am

  26. #24
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Input in a two Dimensional Array.

    No, i ain't idiot
    That didn't work too. I actually want it to get input as far as there is no Enter pressed. Well i managed to get it working.....
    int val=0;
            int arr[]=new int [5];
            try{
                int i=0;
                while((val=System.in.read())!='\n'){
                    arr[i]=val;
                    i++;
                }
            }                                  catch(Exception e){
                System.out.println("N");
            }  for(int j=0;j<5;j++){
                System.out.println(arr[j]);
            }
    But i know there are many problems with it for now as if i will enter 34 it will add ASCII's but Norm, do you think, this will execute more efficiently than InputStreamReader or BufferedReader?

  27. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input in a two Dimensional Array.

    For testing write a simple program with a loop that reads what you type in and prints it on the screen.
    will execute more efficiently than InputStreamReader or BufferedReader?
    Write a test program that times how long each techniques takes.

    You're wasting a lot of time worrying about efficiency when the program is dealing with a user.

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

    Mr.777 (December 14th, 2011)

Page 1 of 2 12 LastLast

Similar Threads

  1. 2 dimensional array coding
    By Bighairjersey in forum Java Theory & Questions
    Replies: 8
    Last Post: July 22nd, 2011, 02:30 PM
  2. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 08:32 PM
  3. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  4. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM