Results 1 to 9 of 9
  1. #1
    JoshuaBritton is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Exclamation My first Java Code [PROBLEMS]

    hey, I don`t know a lot about codes, but at uni we have to do battleships Java code.
    I have created this code so far but they are a lot errors, that I cant find out how to fix, I wound appreciate is some could give me a hand and tell were i going wrong. incomparable types: char and java.lang.String is the main error I font understand.

    the code is ...
    Java Code:
    /**
     * @(#)Battleships.java
     *
     * Battleships application
     *
     * @author
     * @version 1.00 2012/3/11
     */
    
    import java.util.Scanner;
    public class bs3 {
    
        public static void main(String[] args) {
    
        //*instructions
        {
        System.out.println ("to play enter a number from 1-10 to place your ships, and the same to fire.");
        }
    
    //*player grid
         int c[][]={
        	{1,2,3,4,5,6,7,8,9,10},
        	{1,0,0,0,0,0,0,0,0,0},
        	{2,0,0,0,0,0,0,0,0,0},
        	{3,0,0,0,0,0,0,0,0,0},
        	{4,0,0,0,0,0,0,0,0,0}};
        	for(int a=0;a<5;a++)
        	{
    
        //*keybord inputs
        Scanner fromkeyboard=new Scanner (System.in) ;
        int mynumberx=fromkeyboard.nextInt ();
        int mynumbery=fromkeyboard.nextInt ();
    
        //*ship inputs
        {
        int a=0,b=0;
        char[][] c=new char[10][4];
        if(c[a][b]=="1"||c[a][b+1]=="1");}
    
        //*3box ship
        {
        	c[a][b]="1";
        	c[a][b+1]="1";
        	c[a][b+2]="1";
        }
    
    //*output (hit or miss)
        	System.out.print("");
        	for (int b=0;b<9;b++)
    
        	{System.out.print(c[a][b]);}
        	{	if(c[mynumberx][mynumbery]==1)
        			System.out.print("HIT");
        			else
        			System.out.print("MISS");
        	}
        }
    }
    }
    thanks to anyone who may read this, I feel so silly having to do this but we got to start some place.
    Last edited by JosAH; 04-12-2012 at 09:37 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: My first Java Code [PROBLEMS]

    Character literals need single quotes, as in 'a', 'b' or 'c'; not double quotes (those are Strings) as in "a", "b" or "c".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: My first Java Code [PROBLEMS]

    The c array contains int values, the code is comparing its elements to Strings. The int 1 != to the String: "1"
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: My first Java Code [PROBLEMS]

    Quote Originally Posted by Norm View Post
    The c array contains int values, the code is comparing its elements to Strings. The int 1 != to the String: "1"
    ... and below that int array c it is redefined as a char array. The OP should really read the compiler's error messages.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: My first Java Code [PROBLEMS]

    whoops

    The second c is in {}s and hides the first one.

    Lots going on here. Good "bad coding techniques" example.
    Last edited by Norm; 04-12-2012 at 10:40 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: My first Java Code [PROBLEMS]

    Quote Originally Posted by Norm View Post
    whoops

    The second c is in {}s and hides the first one.

    Lots going on here. Good bad coding technique example.
    Yup, and the indentation is worth zilch and makes me dizzy ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    JoshuaBritton is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: My first Java Code [PROBLEMS]

    Quote Originally Posted by Norm View Post
    whoops

    The second c is in {}s and hides the first one.

    Lots going on here. Good "bad coding techniques" example.
    sorry, first time I ever coded, I have fixed a bit.
    only error now is "reached end of file while parsing"
    Java Code:
    /**
     * @(#)Battleships.java
     *
     * Battleships application
     *
     * @author
     * @version 1.00 2012/3/11
     */
    
    import java.util.Scanner;
    public class bs3 {
    
        public static void main(String[] args) {
    
        //*instructions
        {
        System.out.println ("to play enter a number from 1-10 to place your ships, and the same to fire.");
        }
    
    //*Computer grid
         int c[][]={
        	{1,2,3,4,5,6,7,8,9,10,11,12},
        	{1,0,0,0,0,0,0,0,0,0,0,0},
        	{2,0,0,0,0,0,0,0,0,0,0,0},
        	{3,0,0,0,0,0,0,0,0,0,0,0},
        	{4,0,0,0,0,0,0,0,0,0,0,0},
        	{5,0,0,0,0,0,0,0,0,0,0,0},
        	{6,0,0,0,0,0,0,0,0,0,0,0},
        	{7,0,0,0,0,0,0,0,0,0,0,0},
        	{8,0,0,0,0,0,0,0,0,0,0,0},
        	{9,0,0,0,0,0,0,0,0,0,0,0},
        	{10,0,0,0,0,0,0,0,0,0,0,0}};
        	for(int a=0;a<5;a++)
        	{
        	//*playergrid
         int d[][]={
        	{1,2,3,4,5,6,7,8,9,10,11,12},
        	{1,0,0,0,0,0,0,0,0,0,0,0},
        	{2,0,0,0,0,0,0,0,0,0,0,0},
        	{3,0,0,0,0,0,0,0,0,0,0,0},
        	{4,0,0,0,0,0,0,0,0,0,0,0},
        	{5,0,0,0,0,0,0,0,0,0,0,0},
        	{6,0,0,0,0,0,0,0,0,0,0,0},
        	{7,0,0,0,0,0,0,0,0,0,0,0},
        	{8,0,0,0,0,0,0,0,0,0,0,0},
        	{9,0,0,0,0,0,0,0,0,0,0,0},
        	{10,0,0,0,0,0,0,0,0,0,0,0}};
        	for(int a=0;a<5;a++)
        	{
    
        //*keybord inputs
        Scanner fromkeyboard=new Scanner (System.in) ;
        int mynumberx=fromkeyboard.nextInt ();
        int mynumbery=fromkeyboard.nextInt ();
    
        //*ship inputs
        {
        int a=0,b=0;
        char[][] c=new char[10][4];
        if(c[a][b]=="1"||c[a][b+1]=="1");}
    
        //*3box ship
        {
        	c[a][b]="1";
        	c[a][b+1]="1";
        	c[a][b+2]="1";
        }
    
    //*output (hit or miss)
        	System.out.print("");
        	for (int b=0;b<9;b++)
    
        	{System.out.print(c[a][b]);}
        	{	if(c[mynumberx][mynumbery]==1)
        			System.out.print("HIT");
        			else
        			System.out.print("MISS");
        	}
        }
    }
        }

  8. #8
    JoshuaBritton is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: My first Java Code [PROBLEMS]

    p.s any good books to help, I am reading Head start and java for dummy`s ?
    sorry about been pest.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: My first Java Code [PROBLEMS]

    Make sure the {}s are properly paired.

    You have extra pairs of {} that are not needed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: 06-05-2011, 09:16 PM
  2. A few problems with my code..
    By Kerrai in forum New To Java
    Replies: 1
    Last Post: 10-25-2010, 08:49 PM
  3. Problems with my source code
    By bilak09 in forum New To Java
    Replies: 10
    Last Post: 04-26-2010, 07:23 AM
  4. Problems with code
    By jforce93 in forum New To Java
    Replies: 8
    Last Post: 08-14-2009, 03:49 AM
  5. problems with java code! (very new - need help asap!)
    By sumkindafreek in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 06:00 AM

Tags for this Thread

Posting Permissions

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