File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I'm trying  to run my code through console but the option is not available Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Watch "I New topic
Author

I'm trying to run my code through console but the option is not available

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

I'm trying to make a student that has some project and quizz scores but I currently can't launch the code through the console.

here is the code:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

What do you mean you "can't launch the code through the console"? Why not? What did you try? Did you get an error, and if so, what did it say?

You have to make it possible for us to help you.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

One thing that's missing from that code, though: to run a programme you need a class with a main method. Maybe you have one, but if you don't that's where you need to start.

Having a quick look at your code, though, I don't think you understand how classes work. A class is a template for an object, so given a class you can have many objects of that class. For instance, if you've got a Student class, you can have lots of Student objects. Each one will have a different name, ID, test score, etc.

Which means it doesn't make sense to have instance variables called Kevin and Nash. Surely they should be something like firstName and lastName? And you shouldn't have things like the ID and the score hard-coded into the class. Instead those are values that will be passed into the class from outside.
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

I meant that when I go to 'Run As' my only option is 'Run Configuration'.
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54
Matthew Brown wrote:One thing that's missing from that code, though: to run a programme you need a class with a main method. Maybe you have one, but if you don't that's where you need to start.

Having a quick look at your code, though, I don't think you understand how classes work. A class is a template for an object, so given a class you can have many objects of that class. For instance, if you've got a Student class, you can have lots of Student objects. Each one will have a different name, ID, test score, etc.

Which means it doesn't make sense to have instance variables called Kevin and Nash. Surely they should be something like firstName and lastName? And you shouldn't have things like the ID and the score hard-coded into the class. Instead those are values that will be passed into the class from outside.


So, how can I call up the students name and id?

something like this?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
There is so much wrong with that code, I am afraid.
You have some peculiar names for your fields; Kevin and Nash aren’t proper field names. And they ought to start with small letters.
Then you are giving those fields values, which you promptly replace with different values. You never use the projects and quizzes constructor parameters. You have a little loop in the constructor which doesn’t do anything. And a very similar loop in the get project index method, which appears to return -1 regardless.
Then you have some weird methods which have setXXX names and do nothing but return false.

I have a suspicion that it is easier to start from scratch by working out what you want your Student object to do.
Fields are the data in your objects. The constructor sets up the initial values in your fields. Look at this Java Tutorials section. The methods each do something. One thing and one only, preferably.
I recommend you work out how to write a toString() method for your Student object, then you can print it out. Details of the toString method here and in the sample chapter from Effective Java by Bloch.
Then put a main method in a student demo class, like thisStart like that.

Start small. Try a Student class with name only in the first instance. The less code you write before you compile and run it, the better. Then build up the class in tiny pieces, a few lines at a time.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

kenny gill wrote:I meant that when I go to 'Run As' my only option is 'Run Configuration'.

I don't even know what this means.

so...let's back up.

to run something from the console, you first need to open a console window. Go to Start->run. You should get a little pop-up where you can type "cmd" (without the quotes). then click OK. You should get a (probably) black window that says something like:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

I:\>

Mine defaults to my I: drive, so I would first change to my C drive. I would type "c:" Then I would change to the directory where my java file is. I have a directory called "slop" where I play with java code for fun. So, I type "cd slop".

If i then type "dir", i should see my java file listed. I can then compile it by typeing "javac MyClass.java".

I should end up with a file called "MyClass.class" - or I would get error messages telling my I need to fix my code.

If all that works, I can then type "java MyClass" to actually run my program.

based on what you have said, I'm guessing this won't go smoothly. You will probably get errors along the way. If so, please give a detailed description of what you are doing, and the full text of what you get when things go wrong.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

What Kenny is describing is not running the code from the command line but through eclipse.

Kenny, the reason you cannot run your class the way you want is because you haven't defined a main method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: I'm trying to run my code through console but the option is not available
 
Similar Threads
Printing out of an data stored in and array and SringBuffer array
need help: I'm just not getting how this whole "private" / "public" class definition thing works.
Converting Infix to Postfix Expressions
Could someone explain what is wrong with my set methods?
Need help with while-loop in constructor.