5 Replies - 656 Views - Last Post: 04 February 2009 - 08:38 PM Rate Topic: -----

#1 kuletboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-February 09

converting strings to chars

Posted 04 February 2009 - 02:04 PM

I started learning java a couple of weeks ago and we have a project due.

this is the gameboard i created. it creates a 2 dimensional array with the first row as numbers and the first columns are Letters then everything else as tildas (~)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package battleship;
import java.util.*;
/**
 *
 * @author aschg3
 */
public class Test {


	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here
		String enter;
		Object[][] newgame = new Object[12][12];//[rows][columns]

		newgame[0][0] = "  ";
		for(int i = 1; i < 11; i++)
		{
			for(int j = 1; j < 11; j++)
			{
				newgame[i][j] = "~ ";
			}
		}
		for(int i = 0;i<11; i++)
		{
			newgame[0][i+1] = i+" ";
		}
		for(int i = 0;i<11; i++)
		{		
			newgame[i+1][0] = (char)(65+i) + " ";
		}


		System.out.println("Let's play Battle Ship!\n");
		
		for(int i = 0; i<11; i++)
		{
			for(int j = 0; j<11; j++)
			{
				System.out.print(newgame[i][j]);
			}
			System.out.println("");
		}
		System.out.println("Pick a coordinate");

		Scanner keyboard = new Scanner(System.in);
		enter = keyboard.nextLine();

	}

}



now im making a new class file that takes the input and interpret it as numbers i can plug in into the gameboard for a proper coordinate.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package battleship;

/**
 *
 * @author aschg3
 */
public class Coordinates {

	private static String row;
	private static String column;

   /* public static void enteredCoord(String input)
	{
	   row = input.substring(0,1);
	   column = input.substring(1,2);
	   //return column;
	}*/
	public static int columnCoord(String coord)
	{
		column = coord.substring(1,2);
	   
	}
	

	/*public static void main(String[] args)
	{
		String str = "A1";*/

	 

	}
}




what im having trouble with is:
the user input would be a Caps letter and a number i.e. (A1). I thought i could take A1.substring(0,1) to take the A and set it as some variable. Then use that variable and convert it to an ANSI character then subtract 65 from it to get a numerical value that i can use for the coordinate system.

Problem: im having trouble converting strings to chars

please help . . . some of the things i said might not make sense because i just started java. sorry i hope u understand what im asking for

Is This A Good Question/Topic? 0
  • +

Replies To: converting strings to chars

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: converting strings to chars

Posted 04 February 2009 - 02:09 PM

use the charAt() method

EDIT: forget my previous post, didnt read your post carefully

This post has been edited by mostyfriedman: 04 February 2009 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: converting strings to chars

Posted 04 February 2009 - 02:11 PM

To get a character out of a string, use the charAt(int index) function

eg.
String s = "A1";
char c = s.charAt(0);


Was This Post Helpful? 1
  • +
  • -

#4 kuletboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 04-February 09

Re: converting strings to chars

Posted 04 February 2009 - 02:14 PM

View PostDogstopper, on 4 Feb, 2009 - 01:11 PM, said:

To get a character out of a string, use the charAt(int index) function

eg.
String s = "A1";
char c = s.charAt(0);



oh god thats right . . . i remember the professor talking about that. . . thanks a lot and sorry bout the double post of this thread. internet is slow and i kinda refreshed it and posted again
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: converting strings to chars

Posted 04 February 2009 - 02:22 PM

Modified title to be more descriptive of the problem.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: converting strings to chars

Posted 04 February 2009 - 08:38 PM

or to do it only once... and not to call the method 100 times
the char[] digit = String.toCharArray() is the most appropriate method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1