3 Replies - 8875 Views - Last Post: 16 September 2006 - 10:07 AM Rate Topic: -----

#1 norward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 06

Variable Initialization

Posted 15 September 2006 - 06:54 PM

Hi, I am new to programing and need some basic help. I don't understand how to initialize a variable that is going to hold char data. The program I'm working on is going to sort names in ascending order. Here is what I have so far.
Thanks!!
import java.util.Scanner; //Used for the Scanner Class

//This program will sort names in assending order.

public class sortedNames

{
	char name1;	//To hold user input.
	char name2;	//To hold user input.
	char name3;	//To hold user input.

	name1 =;



	public static void main(String[] args)

	{
		//create Scanner class to accept user keyboard input.
		Scanner keyboard = new Scanner(System.in);

		//Get first name
		System.out.println("Enter the first name.");
		name = keyboard.nextLine();


		//Get second name
		System.out.println("Enter the second name.");
		name = keyboard.nextLine();

		//Get third name
		System.out.println("Enter the third name.");
		name = keyboard.nextLine();

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Variable Initialization

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Variable Initialization

Posted 15 September 2006 - 07:04 PM

First and foremost, I think you'll want to use a string object to hold a name...at the very least, a character array instead of a single char variable, which will hold only one letter.
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: Variable Initialization

Posted 15 September 2006 - 07:06 PM

Well to initialize a character you simple assign it the value to hold name1 = 'A'; or you could declare it and initialize it in one step char name1 = 'A';

But I think in this situation you want a String data type to hold a name. A character data type will only hold one character.

String name1 = "Timmy";



edit: Amadeus slipped in under my nose, damn my slow typing
Was This Post Helpful? 0
  • +
  • -

#4 norward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 06

Re: Variable Initialization

Posted 16 September 2006 - 10:07 AM

Thanks for the help, after reading the post and going back through my book I see where I went wrong. I'll have more questions in the future.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1