Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Error message in jGrasp


  • Please log in to reply

#1
El Kabong

El Kabong

    New Member

  • Member
  • Pip
  • 2 posts
hey guys, I'm new to the board, thanks for having such a good one! I'm currently in an intro to Java class, and we have a program due tonight. The program is supposed to print our name, birthday, hobbies, favorite book, and favorite movie, all on separate lines. Now I know I could just say "System.out.println('<name>')", and repeat that for each one, but I want to do it a different way. I'd like to store each thing as a variable, and then print each thing without quoting the exact thing. This is what I have so far:

public class PeronalInfo
{
public static void main (String args[])
{
string Name;
int Bday;
int Hobbies;
int Favbook;
int Favmovie;
Name = "I'm not telling you my name!";
Bday = "November 20, 1986";
Hobbies = "Computers, guitar, car repair";
Favbook = "The Harry Potter series";
Favmovie = "Wedding Crashers";

System.out.println(Name);
System.out.println(Bday);
System.out.println(Hobbies);
System.out.println(Favbook);
System.out.println(Favmovie);
}
}

I'm pretty sure that "int" means integer, but I don't know what to put when I want to initialize a string. How do I do that? Thanks ahead of time for any help!
  • 0

Advertisements


#2
Logic_Overflow

Logic_Overflow

    Member

  • Member
  • PipPip
  • 63 posts
Yes, int does mean integer. int are only for numbers only, until you are in higher level of Java and know how to manipulate it a bit more. In this case by looking at the code, you can't use int when you want to store variables that aren't numbers. In this case, all of those would be string.

A fast way of declaring new string variables is as follows using your code:

string Name, BDay, Hobbies, FavBook, FavMovie;

Don't mind if I use different capitalization as we all develop our own ways of programming.

So that will create all the variables you need. You can leave this part alone as it is:

Name = "I'm not telling you my name!";
BDay = "November 20, 1986";
Hobbies = "Computers, guitar, car repair";
FavBook = "The Harry Potter series";
FavMovie = "Wedding Crashers";

As for the System.out.println(variablenamehere); You can leave that how it is. Another way of doing it could be this:

System.out.println(Name"+\n+"BDay"+\n+"Hobbies"+\n+"FavBook"+\n+"FavMovie);

That would put it all in one line. \n basically means new line, so it will jump down to a new line.

Hope this helps :-)

Oh yeah! With strings, all you have to do is string variablename; and you are set. Worry about initialization when you are dealing with the other primitive types (int, long, double, float, boolean, short, char)

Edited by LDG, 28 January 2006 - 10:39 AM.

  • 0

#3
El Kabong

El Kabong

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
Hey man, thanks a lot! I ended up taking the easy way out, and just telling it to print exactly what I wanted rather than using variables. But all I really wanted was to know HOW to do it, I didn't really care about the assignment. Anyways, your reply helped a lot, I really appreciate it!
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP