Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default [SOLVED] pls help...

    hi everyone,
    can someone help me w/ this
    here is my code:
    import java.io.*;
    public class Num2
    {
    public static void main(String[]args)throws IOException
    {
    BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
    String comma;
    int r1, c1, r2, c2;
    int[][]cards;
    cards=new int[4][4];
    for(int r=0; r<4; r++)
    {
    for(int c=0; c<4; c++)
    {
    cards[r][c]=(int) (Math.random() * 8 + 1);
    System.out.print(cards[r][c]);
    }
    System.out.print("\n");
    }
    for (int r=0; r<4; r++)
    {
    for (int c=0; c<4; c++)
    {
    System.out.print("*");
    }
    System.out.print("\n");
    }
    System.out.print("\nPlease insert the first card row and column seperated by a comma.");
    r1=Integer.parseInt(a.readLine());
    comma=a.readLine();
    c1=Integer.parseInt(a.readLine());
    System.out.print("\nPlease insert the second card row and column seperated by a comma.");
    r2=Integer.parseInt(a.readLine());
    comma=a.readLine();
    c2=Integer.parseInt(a.readLine());
    //reveal
    for(int r=0; r<4; r++)
    {
    for (int c=0; c<4; c++)
    {
    if((r==r1)&&(c==c1))
    {
    System.out.print(cards[r][c]);
    }
    else if((r==r2)&&(c==c2))
    {
    System.out.print(cards[r][c]);
    }
    else
    {
    System.out.print("*");
    }
    System.out.println();
    }
    //match?
    if (cards[r1][c1]==cards[r2][c2])
    {
    }
    else
    {
    }
    //this pushes the next board onto a blank screen
    for (int b=0; b<=20; b++)
    System.out.println();
    }
    }
    }

    my problem here is once i entered the row and columns,there is nothing happens..pls help,
    here's the error...

    ---------- Capture Output ----------
    > "C:\jdk1.3.0_02\bin\java.exe" Num2
    5373
    7563
    5732
    8555
    ****
    ****
    ****
    ****

    Please insert the first card row and column seperated by a comma.1,2
    java.lang.NumberFormatException:
    at java.lang.Integer.parseInt(Integer.java:426)
    at java.lang.Integer.parseInt(Integer.java:454)
    at Num2.main(Num2.java:29)
    Exception in thread "main"

    and also how to hide the values of the cards,
    i mean only the * can only be seen,
    once the row and the column is inputted,
    the values will only show,
    pls help w/ my program

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Can you post the code with code tags around it?

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default read the whole line?

    How are you inputting the first card row and column? Something like 2345,5678 ?
    If so, the method you are using is "readLine()" which, as it's name says, reads the line of string, including the the comma. Therefore the comma is also being parsed as an int (which it can't) and this is what is giving the parseInt() method indigestion and eventually ending up in sending the "NumberFormatException" nastygram.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    ok,
    what if i erase the comma..
    the comma=a.readLine?
    is that solve the problem?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    The way you are working is wrong, basically the way you are handling numbers are wrong.

    Once your input is something like 1,2 you try to convert that string into a number using Integer wrapper class. So what should be the final value, it's 1,2 This is not a number anymore. That's why you get a NumberFormatException, simply it's an invalid number.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default I don't think so ...

    You didn't answer my question:
    How are you inputting the first card row and column? Something like 2345,5678 ?
    • Are you inputting all at once, as one line: 1234,5678?
    • Are you inputting first 1234 (enter), then the comma (enter) and last 5678 (enter) ?


    Taking out the the following will not improve/hurt the program in any way because it's not used:
    Java Code:
    comma=a.readLine();
    It would probably be better to read everything (1234,5678) as one string and then pull out what you want using the substring() method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    what if i erase the comma?
    here is the code:
    Java Code:
    import java.io.*;
    public class Num2
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
    	
    	int r1, c1, r2, c2;
    	int[][]cards;
    	cards=new int[4][4];
    	for(int r=0; r<4; r++)
    	{
    		for(int c=0; c<4; c++)
    		{
    			cards[r][c]=(int) (Math.random() * 8);
    			System.out.print(cards[r][c]);
    		}
    		System.out.print("\n");
    	}
       	 for (int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
               System.out.print("*");
            }
    	System.out.print("\n");
    	}
    	System.out.print("\nPlease insert the first card row and column.");
    	r1=Integer.parseInt(a.readLine());
    	
    	c1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the second card row and column.");
    	r2=Integer.parseInt(a.readLine());
    	
    	c2=Integer.parseInt(a.readLine());
    	//reveal
    	for(int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
    	if((r==r1)&&(c==c1))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else if((r==r2)&&(c==c2))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else
    	{
    	System.out.print("*");
    	}
    	System.out.println();
    	}
    	//match?
        	if (cards[r1][c1]==cards[r2][c2])
        	{
        	}
        	else
        	{
        	}
        	//this pushes the next board onto a blank screen
        	for (int b=0; b<=20; b++)
            System.out.println();
    	}	
    }
    }
    it doesn't print any errors the problem is once the user enter their desired row and column,nothing happens..the number on the row and column they type didn't appear..waht's wrong?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by beginner21 View Post
    what if i erase the comma?
    here is the code:
    Java Code:
    import java.io.*;
    public class Num2
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
    	
    	int r1, c1, r2, c2;
    	int[][]cards;
    	cards=new int[4][4];
    	for(int r=0; r<4; r++)
    	{
    		for(int c=0; c<4; c++)
    		{
    			cards[r][c]=(int) (Math.random() * 8);
    			System.out.print(cards[r][c]);
    		}
    		System.out.print("\n");
    	}
       	 for (int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
               System.out.print("*");
            }
    	System.out.print("\n");
    	}
    	System.out.print("\nPlease insert the first card row and column.");
    	r1=Integer.parseInt(a.readLine());
    	
    	c1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the second card row and column.");
    	r2=Integer.parseInt(a.readLine());
    	
    	c2=Integer.parseInt(a.readLine());
    	//reveal
    	for(int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
    	if((r==r1)&&(c==c1))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else if((r==r2)&&(c==c2))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else
    	{
    	System.out.print("*");
    	}
    	System.out.println();
    	}
    	//match?
        	if (cards[r1][c1]==cards[r2][c2])
        	{
        	}
        	else
        	{
        	}
        	//this pushes the next board onto a blank screen
        	for (int b=0; b<=20; b++)
            System.out.println();
    	}	
    }
    }
    it doesn't print any errors the problem is once the user enter their desired row and column,nothing happens..the number on the row and column they type didn't appear..waht's wrong?

    Yes you cannot get any result until you enter few values. Look at the following in your cod.

    Java Code:
            System.out.print("\nPlease insert the first card row and column " +
                    "seperated by a comma.");
            r1 = Integer.parseInt(a.readLine());
            comma = a.readLine();
            c1 = Integer.parseInt(a.readLine());
    What are you doing there? First of all build a well design/logic for your application. Then try to code it.

  9. #9
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    ok..
    here is my modify code:
    Java Code:
    import java.io.*;
    public class Num2
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
    	
    	int r1, c1, r2, c2;
    	int[][]cards;
    	cards=new int[4][4];
    	for(int r=0; r<4; r++)
    	{
    		for(int c=0; c<4; c++)
    		{
    			cards[r][c]=(int) (Math.random() * 8);
    			System.out.print(cards[r][c]);
    		}
    		System.out.print("\n");
    	}
       	 for (int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
               System.out.print("*");
            }
    	System.out.print("\n");
    	}
    	System.out.print("\nPlease insert the first card row.");
    	r1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the first card column.");
    	c1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the second card row.");
    	r2=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the first card column.");
    	c2=Integer.parseInt(a.readLine());
    	//reveal
    	for(int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
    	if((r==r1)&&(c==c1))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else if((r==r2)&&(c==c2))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else
    	{
    	System.out.print("*");
    	}
    	System.out.println();
    	}
    	//match?
        	if (cards[r1][c1]==cards[r2][c2])
        	{
        	}
        	else
        	{
        	}
        	//this pushes the next board onto a blank screen
        	for (int b=0; b<=20; b++)
            System.out.println();
    	}	
    }
    }
    the output the program is this:
    Java Code:
    1555
    0672
    2322
    6011
    ****
    ****
    ****
    ****
    
    Please insert the first card row.1
    
    Please insert the first card column.2
    
    Please insert the second card row.1
    
    Please insert the second card column.2
    *
    *
    *
    *
    
    
    
    
    
    
    
    *
    *
    7
    *
    
    
    
    
    
    
    
    *
    *
    *
    *
    
    
    
    
    
    
    
    *
    *
    *
    *
    it prints the exact place wherein the seven can be found,
    but why it print in that way?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #11
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    i want to print the value in this way?
    ****
    **7*
    ****
    ****
    when i try to erase the System.out.print("\n");
    the output is this:
    ****







    **7*







    ****







    ****

    pls help

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  13. #13
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    im not quite sure,
    here is my new code:
    Java Code:
    import java.io.*;
    public class Num2
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
    	
    	int r1, c1, r2, c2;
    	int[][]cards;
    	cards=new int[4][4];
    	for(int r=0; r<4; r++)
    	{
    		for(int c=0; c<4; c++)
    		{
    			cards[r][c]=(int) (Math.random() * 8);
    			System.out.print(cards[r][c]);
    		}
    		System.out.print("\n");
    	}
       	 for (int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
               System.out.print("*");
            }
    	System.out.print("\n");
    	}
    	System.out.print("\nPlease insert the first card row: ");
    	r1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the first card column: ");
    	c1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the second card row: ");
    	r2=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the first card column: ");
    	c2=Integer.parseInt(a.readLine());
    	//reveal
    	for(int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
    	if((r==r1)&&(c==c1))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else if((r==r2)&&(c==c2))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else
    	{
    	System.out.print("*");
    	}
    	}
    	//match?
        	if (cards[r1][c1]==cards[r2][c2])
        	{
        	}
        	else
        	{
        	}
    	}	
    }
    }
    output:
    Java Code:
    0331
    6102
    6010
    7132
    ****
    ****
    ****
    ****
    
    Please insert the first card row: 1
    
    Please insert the first card column: 2
    
    Please insert the second card row: 3
    
    Please insert the second card column: 3
    ******0********2
    what should i do?

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Can you tell me what the exact pattern you want to have, I'm not clear what you are talking about.

  15. #15
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    my problem here is i want the output to be print like this:
    example:
    Java Code:
    Please insert the first card row: 1
    Please insert the first card column: 2
    Please insert the second card row: 3
    Please insert the second card column: 3
    ****
    **0*
    ****
    ***2
    but i came up with this:
    Java Code:
    Please insert the first card row: 1
    Please insert the first card column: 2
    Please insert the second card row: 3
    Please insert the second card column: 3
    ******0********2
    what or where should i modify?

  16. #16
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    alright..
    i print now the output that i wanted..
    i have a question again..
    i want to hide the values of the cards in my output.
    here is my modified code:
    Java Code:
    import java.io.*;
    public class Num2
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
    	
    	int r1, c1, r2, c2;
    	int[][]cards;
    	cards=new int[4][4];
    	for(int r=0; r<4; r++)
    	{
    		for(int c=0; c<4; c++)
    		{
    			cards[r][c]=(int) (Math.random() * 8);
    			System.out.print(cards[r][c]);
    		}
    		System.out.print("\n");
    	}
       	 for (int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
               System.out.print("*");
            }
    	System.out.print("\n");
    	}
    	System.out.print("\nPlease insert the first card row: ");
    	r1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the first card column: ");
    	c1=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the second card row: ");
    	r2=Integer.parseInt(a.readLine());
    	System.out.print("\nPlease insert the first card column: ");
    	c2=Integer.parseInt(a.readLine());
    	//reveal
    	for(int r=0; r<4; r++)
        	{
            for (int c=0; c<4; c++)
            {
    	if((r==r1)&&(c==c1))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else if((r==r2)&&(c==c2))
    	{
    		System.out.print(cards[r][c]);
    	}
    	else
    	{
    	System.out.print("*");
    	}
    	}
    	//match?
        	if (cards[r1][c1]==cards[r2][c2])
        	{
        	}
        	else
        	{
    	System.out.println();
        	}
    	}	
    }
    }
    the output is:
    5264
    7021
    1741
    1556
    ****
    ****
    ****
    ****
    Please insert the first card row: 1
    Please insert the first card column: 2
    Please insert the second card row: 3
    Please insert the second card column: 3
    ****
    **2*
    ****
    ***6
    [/code]
    the values in my output are printed also..
    before the 4x4 *;
    what should i modify?

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    On the array it cause to print values.

    Java Code:
    	if((r==r1)&&(c==c1))
    	{
    		System.out.print(cards[r][c]);
    	}
    Rather printing the elements of the array, print the hiding character.

  18. #18
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    alright..
    i hide now the values but why did the program stop after inputting some values?
    i wanted that the program should continue until they match those hidden numbers..
    i am working to have a memory matching game..
    wherein if the value that they input is not match;
    it prints the 4x4 * again;
    and input again until the game completes,
    is there any idea how to do it?

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  20. #20
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

Page 1 of 2 12 LastLast

Posting Permissions

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