Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Using Command-Line Java


  • Please log in to reply

#16
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
I did have a nice mother's day thanks, I hope you had a nice one as well.

And I have no problem with you posting lots of questions in this topic.

As for your question, here's the easiest way I know how to do it:

import java.text.*;

class decimalrounding {

   public static void main (String[] args) {

	  double a = 2;
	  double b = 3;
	  DecimalFormat decimalround = new DecimalFormat("#0.00");
	  String c = decimalround.format(a/b);
	  System.out.println(c);
   }
}

This will take 2/3, which would normally be displayed as 0.6666666666666666, and display it as 0.67.

If you have any questions, let me know.
  • 0

Advertisements


#17
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Ah, yes. Much shorter than the confusing and very large algorithm I was using ...

Also, I'm trying to make a class that records any pet's information. I don't really get something - I am supposed to write the program so that it executes differently depending on how much information the user types in when they first type in "java sampleprogram" from the command line. For example, if the program had three variables called "pet", "name", and "color" and the user typed in "java sampleprogram dog rover" the program would instantly store "dog" into the variable pet and "rover" into the variable name, and keep the variable color unchanged (maybe i could assign the color variable to a default color such as brown). Similarly, if the user typed in "java sampleprogram cat, the program would store "cat" into the variable pet, and leave the other two variables unchanged.

I know I am supposed to have three constructor methods and I get how each method would set the appropriate variables but I don't know how to let the program know when to use the one-variable, two-variable, or three-variable constructor.

It's hard to explain, so sorry if that was a shabby explanation!
  • 0

#18
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
Alright, as you may have figured out by now, I like to teach by demonstration. So...

class sampleprogram {

   public static void main (String[] args) {

	  if (args.length > 3 || args.length < 1) {
		 System.out.println("Enter only 1, 2, or 3 arguments!");
		 System.exit(0);
	  }
	  if (args.length > 0) {
		 String pet = args[0];
		 System.out.println(pet);
	  }
	  if (args.length > 1) {
		 String name = args[1];
		 System.out.println(name);
	  }
	  if (args.length > 2) {
		 String color = args[2];
		 System.out.println(color);
	  }
   }
}

The code is hopefully pretty self-explanatory, but if you have questions please ask. The strings are printed for demonstration purposes, but you can remove that of course.

Edited by stettybet0, 16 May 2007 - 02:21 PM.

  • 0

#19
Joeturf

Joeturf

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 158 posts
Thanks for all your help.

Starting only a couple days ago, when I try to run any .jar files from Netbeans, the command prompt comes up with the "Exception in thread "main" java.lang.NoClassDefFoundError"" I don't even know what that means and why it's coming up so many times. It's really annoying because I can't really make any projects without being able to test them ...

EDIT : I solved the problem by putting a ".;" at the start of my CLASSPATH in Environment Variables and then adding a constructor for my main method.

Edited by Joeturf, 28 May 2007 - 11:45 AM.

  • 0

#20
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
grats on solving your own problem. :whistling:
  • 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