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

Thread: Input into array.

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Input into array.

    I have to do this assignment, and its really weird and complicated, so ill save you that. But I need to read multiple lines of input into an array, the first line of input gives me the number of lines that will follow it, so I dont count that. I have code set up, but it doesnt seem to get all of the lines. Its missing one. Not really sure whats wrong with it.

    import java.util.*;
    public class Mastermind
    {
       public static void main(String [] args)
       {
           Scanner in = new Scanner(System.in); 
           System.out.println("Please enter a number followed by that number of guesses: ");
           int numL = Integer.parseInt(in.next());       
           String [] guess = new String [numL];
           //guess [0] = in.nextLine();
     
           int c = 0;
           while (in.hasNextLine() && c < numL)
            {
                guess[c] = in.nextLine();
                c++;
            }
     
            for (int i = 0; i < numL; i++)
            {
                System.out.println(guess[i]);
            }
        }
    }

    Thanks for the help!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Input into array.

    Which line does it miss?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,189
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Input into array.

    Add some println statements that print out the values of all the variables used to control the loop and to show what was read. Use a small input file for testing. Execute the code and look at the values printed out to see why the code is doing what it is doing.

    What does the current print out show? You should have delimiters with the println you use so you can see what it being printed:
    println("i=" + i + " " + guess[i] + "<");
    Last edited by Norm; May 15th, 2012 at 09:02 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Input into array.

    Why are you parseInting the input for the integer.

    Why not just use

    in.nextInt() and avoid the parse int?

    I'm wondering if it's somehow taking in an empty line as input as storing that somewhere.

    Yep, a simple test confirmed as much.

       import java.util.*;
       public class Mastermind
       {
          public static void main(String [] args)
          {
             Scanner in = new Scanner(System.in); 
             System.out.println("Please enter a number followed by that number of guesses: ");
             int numL = Integer.parseInt(in.next());       
             String [] guess = new String [numL];
           //guess [0] = in.nextLine();
     
             int c = 0;
             while (in.hasNextLine() && c < numL)
             {
                guess[c] = in.nextLine();
     
                c++;
     
             }
     
             for (int i = 0; i < numL; i++)
             {
                if (guess[i].equals(""))
                   System.out.println("This one is empty.");
                System.out.println(guess[i]);
             }
          }
       }

     

    Please enter a number followed by that number of guesses:
    4
    1
    2
    3
    4
    This one is empty.

    1
    2
    3




     

    Please enter a number followed by that number of guesses:
    5
    1
    9
    8
    -1
    2
    This one is empty.

    1
    9
    8
    -1


    Last edited by javapenguin; May 15th, 2012 at 02:15 PM.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    I didnt think of that before javapenguin, its changed now. Thanks! But if I do what norm suggested, I get this:

     

    Please enter a number followed by that number of guesses:
    3
    1
    2
    3
    i=0 <
    i=1 1<
    i=2 2<





    So its not that its missing the last line, its just taking in guess[0] as "" for some reason, therefore it runs out of room for the last line.
    I shall go examine further to figure out why, post if you know the answer plox

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,189
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Input into array.

    ts just taking in guess[0] as "" for some reason
    What you are seeing is what the Scanner class does when it has a newline character left in its buffer after you entered a line and pressed.Enter. The next() method does NOT read the newline character from Scanner's buffer.
    If the code used nextLine() (instead of next()) the newline character would be removed from Scanner's buffer.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    Ahhh. Well i'm using nextInt() now, I think what ill do is make some bogus String that I dont use to take up the newline character. That sounds easiest!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,189
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Input into array.

    A call to nextLine() will read the newline out of the buffer.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    Got it working now, thanks guys Will post on this again if I have another issue.

  10. #10
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    So the program I have to write deals with a Mastermind game, which some of you may be familiar with. I have to take input of the format:
    2
    BYOG 3 0
    BGOY 3 0

    and tell whether it is a valid problem or not. Obviously this one is not, because 3 of them were supposed to be correct, 2 were switched and it still said 3.

    But a problem like:
    3
    GROW 2 1
    BROW 1 2
    GOBW 4 0

    is valid. What I am trying to think of, is the correct algorithms to check these.

    I guess, regarding the first input, I could compare each letter and count how many are different and if at least one less than the "correct" number is different, and the number doesnt change, it is invalid. But that opens the door for a lot of options on which way to go after that and what to expect. I'm just looking for some input on this I guess. If you dont want to comment on it, thats fine, I'm going to keep trying.

    If I used a statement like

            int count = 0;
           if(num1[0] == num1 [1])
           {
           for (int i = 0; i < 3; i++)
           {
               if(guess[0].compareTo(guess[1]) != 0)
               {
                   if (guess[0].charAt(i).compareTo(guess[1].charAt(i)) != 0)
                   count++;
     
                }
     
                if (count >= (num[1] - 2))
                System.out.println("Invalid Feedback");
            }        
        }

    It wouldnt work because you cant use compareTo with a char, but I think i'm on the right track. How would I set that up so I can use a String?

  11. #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 into array.

    But you can type cast char to a String. Anyways, there can be other solutions too. But if you want to compare then try equality operator or type cast to String.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,189
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Input into array.

    type cast char to a String
    @Mr.777 Can you post code that does that?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #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 into array.

    Quote Originally Posted by Norm View Post
    @Mr.777 Can you post code that does that?
    My bad with words. By type casting i meant Converting.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  14. #14
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    Well what do you mean by that? You can't just type (String) in front of it. That doesn't work.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,189
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Input into array.

    He's either confused or ???

    You can use numeric operators: ==, <, > etc with char values to compare them.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    Lol I think so.

    Thanks!

  17. #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 into array.

    Or you can do
    String val = Character.toString(charactervaluehere);
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  18. #18
    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 into array.

    Quote Originally Posted by Norm View Post
    He's either confused or ???
    I think you didn't read my comment above.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  19. #19
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    You know, ive been trying to find ways to test and see if the input was invalid, but what if I just find one way to see if it is valid, and then else print invalid?

    I'm trying of think how to test it though.

    3
    GROW 2 1
    BROW 1 2
    GOBW 4 0

    is correct.

    But how to test that and put it into code....

    I guess something along the lines of saying that if it changes less than than one less of the correct number (without the correct number changing), that the numbers changing equals no less than the correct and wrong place numbers added together, but possibly more. Something like that. I'll get working.

  20. #20
    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 into array.

    GROW 2 1
    BROW 1 2
    GOBW 4 0

    If we compare GROW and BROW, only 1 character is replaced and if we compare GROW and GOBW, 2.
    Now if we do BROW and GROW, again 1, BROW and GOBW, 3.
    So shouldn't it be like

    GROW 2 1
    BROW 1 3
    GOBW 4 0

    Or i took the problem wrong?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  21. #21
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    Nah the second number is how many are right, but in the wrong place.

    So they got rid of the R completely, added a G, and moved the B and O.

    the first number is how many are correct completely.

    and I'm glad you pointed this out, because it might have cause me a problem while coding.

  22. #22
    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 into array.

    Can you kindly provide me the reference to study about the rules? May be i could help you after reading the rules.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  23. #23
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Input into array.

    Yeah no problem, ill paste the whole assignment.
     

    Mastermind is a logic game for two players. In each round, one player plays the part
    of codemaker, and the other plays codebreaker. The codemaker secretly creates a
    sequence of four (4) colored pegs, selected from among six (6) different colors. In
    our case we will not use duplicate colors. The codebreaker then creates guesses in
    the same way, and receives feedback regarding each guess. The feedback consists
    of two numbers. The first number specifies how many pegs in the guess are both
    the correct color and position. The other number represents correct colors that are
    in the wrong position. Using this feedback and comparing the results of several
    guesses, the codebreaker should be able to deduce the codemaker’s actual
    sequence. (See the examples to help clarify the rules.)
    Ryan enjoys the game, but he sometimes suspects that the codemaker may be
    cheating by giving false feedback. In fact, it sometimes seems that there is no
    possible sequence consistent with all of the feedback he has received. But Ryan
    doesn’t want to go to the trouble of proving this himself, and he has asked you to
    write a program to find out.
    You will be given a set of guesses with their corresponding feedback, and your task
    is to answer whether there is any possible sequence that is consistent with all the
    feedback.
    Input Format:
    The input will begin with a single integer N, which will be followed by N lines, each
    describing a guess and its feedback in the following format:
    XXXX A B
    where XXXX are four uppercase characters identifying the colors in the guess, A is
    the number of correct pegs in the guess, and B is the number of pegs of correct
    color but incorrect position. The list of possible colors is Red, Green, Blue, Yellow,
    Orange, White, each identified in the input by its first letter.
    Output Format:
    Your output should be either “Valid Feedback” or “Invalid Feedback”, depending
    on whether any possible sequence of colors could have produced the given
    feedback for all the guesses.

    Input:
    2
    BYOG 3 0
    BGOY 3 0

    Output:
    Invalid Feedback

    Input:
    3
    GROW 2 1
    BROW 1 2
    GOBW 4 0

    Output:
    Valid Feedback



Similar Threads

  1. Input in a two Dimensional Array.
    By Mr.777 in forum File I/O & Other I/O Streams
    Replies: 29
    Last Post: December 14th, 2011, 10:40 PM
  2. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 09:43 AM
  3. Append data from user input into array
    By brncao in forum Collections and Generics
    Replies: 1
    Last Post: October 11th, 2011, 04:37 AM
  4. Taking Input in String Array
    By syehjafa in forum Collections and Generics
    Replies: 8
    Last Post: July 25th, 2011, 07:18 AM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 05:07 AM