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

Using Command-Line Java


  • Please log in to reply

#1
Joeturf

Joeturf

    Member

  • Member
  • PipPipPip
  • 158 posts
Hi G2G.

Until now I've just been programming with java at school, but I also want to try it from home. I know I can compile, document, and run java programs that I have made using the Command Line Prompt because I can do that at school using keywords such as javac, javadoc, and java. For instance, if I made a .java text document called practice.java I can go to my command line prompt and type in javac practice.java to compile it.

How can I utilize this command line prompt at home? When I tried it at home it did not recognize javac, javadoc, or java as a valid command.

Thanks!
  • 0

Advertisements


#2
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
download and install this, (let it save to the default directory) then:1. Click Start > Control Panel > System (assuming Windows XP)
2. Click Advanced > Environment Variables.
3. Click on PATH, choose Edit.
4. After the last entry, add a semicolon ";" and type "C:\Program Files\Java\jdk1.6.0_01\bin"
5. Click Ok
6. Click Ok
7. Click Ok
Now those commands will work in the command line.

Edited by stettybet0, 12 April 2007 - 01:51 PM.

  • 0

#3
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Wow! All that computer jargon actually worked!

Thanks very much, this will make working at home a lot easier =)
  • 0

#4
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
No problem. :whistling:

A quick suggestion:

As you may have figured out, teachers are not always right. This is especially true when it comes to programming. Teachers tend to stick to the way they learned it, so if something new and better comes out, they won't teach it.

I assume your teacher has you programming in notepad, then compiling via command prompt.

You should try using NetBeans to program and compile. Not only is this easier, but your programs will be able to run on any platform! (as opposed to only Windows using your current method) Also, Sun Microsystems recommends using NetBean to make your programs. It also has lots of neat features to improve your programming experience and knowledge.

Edited by stettybet0, 12 April 2007 - 04:53 PM.

  • 0

#5
IO-error

IO-error

    Member

  • Member
  • PipPipPip
  • 276 posts
Quick question to stettybet,

Does NetBeans support VBS?
Like, does it colorcode the vbs script?

I heard some programs had problems with single-line scripting, using " : " (a space, :, and another space.).
This enables scripts like this:

a = "hello" : b = msgbox(a) : c = 2 : d = 2.3 : e = c*d : msgbox(e)
Instead of:
a = "hello"
b = msgbox(a)
c = 2
d = 2.3
e = c*d 
msgbox(e)

  • 0

#6
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
No, NetBeans is only for developing java programs. Since VBS is a scripting language (not a programming language), there is no real IDE for VBS.
  • 0

#7
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Ah, great suggestion. I downloaded NetBeans and I was surprised that a free software could be so convenient and "professional". Yup, that's exactly what my teacher does but he also told us specifically NOT to use NetBeans right now and that we'd be utilizing it later. I'm not sure why... maybe because he wants us to have good habits in making headers and such before having NetBeans make pre-made headers for us, since I see that NetBeans already pre-makes some Java code right when you open it up.

But thanks again =).

Edited by Joeturf, 13 April 2007 - 01:14 PM.

  • 0

#8
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
you're welcome. :whistling:

just thought I'd share with you a great learning resource for java:

Java tutorials
  • 0

#9
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Many thanks Stettybet0 :blink: I'll remember to read up on that!

However, I hope you don't mind if I ask you another question I've just encountered...

I'm a beginner to Java programming so I don't know a lot of fundamentals of the language, but how do you read in a character from the keyboard? For instance, I'm trying to make a program that will calculate the cost of a pizza. First, my program will ask the user for the size of the pizza (S, M, or L). If a user inputted L for large, the program would calculate the cost of a large pizza. Is there anyway I can do that?

The only methods I know that will read in something are those that read in integers.

Thanks :whistling:
  • 0

#10
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
this should give you pretty much what you need:

import java.io.*;

public class pizza {

   public static void main (String[] args) {

	  System.out.print("Pizza Size (S, M, or L): ");

	  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	  // Ask for user input... in this case pizza size

	  String pizzaSize = null;
	  String pizzaCost = null;
	  String pizzaType = null;
	  Boolean noPizza = null;

	  try {
		 pizzaSize = br.readLine();
	  } catch (IOException ioe) {
		 System.out.println("Error!");
		 System.exit(1);
	  }

	  // Take user input, and store it in a string. You have to handle the exception!

	  if (pizzaSize.equals("S") || pizzaSize.equals("s")) {
		 pizzaType = "small";
		 pizzaCost = "6.99";
		 noPizza = false;
	  } else if (pizzaSize.equals("M") || pizzaSize.equals("m")) {
		 pizzaType = "medium";
		 pizzaCost = "8.99";
		 noPizza = false;
	  } else if (pizzaSize.equals("L") || pizzaSize.equals("l")) {
		 pizzaType = "large";
		 pizzaCost = "10.99";
		 noPizza = false;
	  } else {
		 noPizza = true;
	  }

	  // Use an If-Then-Else statement to determine what action to take based on the user input.
	  // Remember the user input is stored in string "pizzaSize".

	  if (noPizza == false) {
		 System.out.println("A " + pizzaType + " pizza will cost $" + pizzaCost + ".");
	  } else if (noPizza == true) {
		 System.out.println("Sorry, we don't have that pizza size.");
	  }

	  // Output the cost of pizza based on the type of pizza inputed.
	  // If they input anything other than S, M, or L, it will tell them that you don't have that pizza size.

   }
}

-stettybet0
  • 0

Advertisements


#11
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Hi again!

Is it possible to call a class? I know how to call methods, but I don't know how to call a class.

Thanks!
  • 0

#12
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
why are you wanting to call a class? if it's just to use some variables, then you can just instance those.
  • 0

#13
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Honestly, I have no clue. We're starting a new project in class and it says I must use at least two classes. Class Main will run the whole project, then a different class will hold all the methods that are going to be used in the program. I was hoping that I was going to figure out why I needed two classes as I worked through the project :whistling:
  • 0

#14
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
ok, so here's a quick sample on how to store things in one class, and use them in another.

class makenumbers {
	  int a = 1;
	  int b = 2;
}
class numbers {
   public static void main (String[] args) {
	  makenumbers num = new makenumbers();
	  System.out.println(num.a);
	  System.out.println(num.b);
   }
}

Hopefully that's pretty straight-forward. If you need explanation on anything, let me know and I'll try to get back to you tomorrow.

Edited by stettybet0, 02 May 2007 - 08:39 PM.

  • 0

#15
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Hi again. Hope you had a nice mother's day :whistling:

Something has been really annoying me for the past couple days. I am wondering how to format doubles, in other words make it so only 2 decimal places are shown. Wether rounded or truncated I don't really care, I just don't want a number like 203.2358792347 showing up! The reason it annoys me is that I know there HAS to be some easier way to do it than the unorthodox suggestions I've seen that involve lines and lines of code. If there isn't an easier way, that.. really is a bummer.

I know in C++ you can do it very easily with just a %3d or %4.2f in the printf's argument. I also know that you can format decimals easily with VB! Please let there be a way to do it in java!

Thanks :blink:

P.S.: I know I've posted a lot of questions in this same thread, it's just I get stuck a lot and I don't want to be "flooding" this forum with seperate topics for each question.
  • 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