A very basic question about Main.java

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1232 Views - Last Post: 15 June 2010 - 09:58 AM Rate Topic: -----

#1 Solar D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 10

A very basic question about Main.java

Posted 15 June 2010 - 07:24 AM

Alright, forgive me if I've messed up in what I've attempted to write. I am trying to learn the very basics of Java and tried to do a very basic math... subroutine(if that is the right term).

Quote

public class main {
public static void calculator (String[] args) {
int y = 5;
int x = 2;
int z;

z = y*x;

System.out.println ("z");
}

} //basic math test


I save the file as Main.java to compile it like Netbeans 6.8 yells at me too.

When i attempt to run it i get:

"Main class wasn't found in JavaApplication1 project.

I have a feeling I may not have opened the right type of file to just mess around in.

If i want to start with a blank file with nothing to do the basics myself I should be going to New>Category>Java>Empty Java File? :whistling:

If that is not the problem, then what in my noobish ways have I done wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: A very basic question about Main.java

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: A very basic question about Main.java

Posted 15 June 2010 - 07:32 AM

Close, you may want to look at this tutorial to help you with the basics.

//name of the class
public class calculator  { 
	//main method always has to be like this
	public static void main (String[] args) {
		int y = 5;
		int x = 2;
		int z;

		z = y*x;
               //print value of z, not the string "z".
		System.out.println (z);
	}

} //basic math test


Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,029
  • Joined: 11-December 07

Re: A very basic question about Main.java

Posted 15 June 2010 - 07:39 AM

A main class in java is a class which has the main method. You can call the class whatever you want but the program always starts with the main method. This is probably what you want:

Save it in a file called Calculator.java

public class Calculator{
public static void main(String[] args) {
int y = 5;
int x = 2;
int z;

z = y*x;

System.out.println ("z");
}

} //basic math test


Beaten to it! :D

This post has been edited by cfoley: 15 June 2010 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: A very basic question about Main.java

Posted 15 June 2010 - 07:42 AM

@cfoley

I believe he wants to print the value of z not System.out.println ("z"); :).
Was This Post Helpful? 0
  • +
  • -

#5 Solar D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 10

Re: A very basic question about Main.java

Posted 15 June 2010 - 07:43 AM

Yes, as soon as I posted I realized I should switch the main and calculator labels.

And i see the error of my ways. I realized when i chose to "save as" calculator I didn't add the .java extension to the end(thought it would do that automatically). Also when i choose to "save as", does caps matter? I'm assuming it does? Just a habit i have from writing in MS Word.

Ex) Test.java = File name

public class test


Other than that it all makes sense now thanks!
Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: A very basic question about Main.java

Posted 15 June 2010 - 07:51 AM

Yes it does matter and it will not run, it will give you a java.lang.NoClassDefFoundError. Therefore the class name's case has to match the case you save it in. It is best practice to save your class with the first character starting with an uppercase.
Was This Post Helpful? 0
  • +
  • -

#7 Solar D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 10

Re: A very basic question about Main.java

Posted 15 June 2010 - 07:54 AM

Alright thanks, good to know :bigsmile:

As well, if i had:
int x = 6;
int y = 5;
int z;
z = x*y;


how would i write

system.out.println() to show the "y*z"?

So what i want is the sout to show

Quote

Z=6*5
Z=30

This post has been edited by Solar D: 15 June 2010 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: A very basic question about Main.java

Posted 15 June 2010 - 08:03 AM

Quote

how would i write

system.out.println() to show the "y*z"?


print out the results of the multiplication.
System.out.println (y*z);

prints out the String.
System.out.println ("y*z");
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,029
  • Joined: 11-December 07

Re: A very basic question about Main.java

Posted 15 June 2010 - 08:57 AM

View Postm-e-g-a-z, on 15 June 2010 - 02:42 PM, said:

@cfoley

I believe he wants to print the value of z not System.out.println ("z"); :).


Quite right. I hadn't looked past the main method error he had explained. :)
Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: A very basic question about Main.java

Posted 15 June 2010 - 09:08 AM

Awww thanks for recommending my tutorial! :wub:

Also, I think it would be:

System.out.println("Z = " + x + "*" + y);
System.out.println(Z = " + z);



You would do this because you want to print a literal string saying z = but you want the value held by x and y, not the literal string "x" and "y", so you close the quote and concatinate the value to the end with a +. Hope that helps! ^__^
Was This Post Helpful? 1
  • +
  • -

#11 Solar D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 10

Re: A very basic question about Main.java

Posted 15 June 2010 - 09:21 AM

Thanks! That was exactly what I was trying to do! Oh and PS I've already read that, very helpful.

As well one last question is there a way i can type larger sentences that take up more than one line in System.out.println ?

Such as how you can write a multi-line comment using /* */
Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: A very basic question about Main.java

Posted 15 June 2010 - 09:39 AM

Awww thanks. :wub:

System.out.print(); You should know that from my tutorial. ;) Just use a \n character for forced new lines.
Was This Post Helpful? 0
  • +
  • -

#13 Solar D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 10

Re: A very basic question about Main.java

Posted 15 June 2010 - 09:44 AM

Ahhh okay, I must have forgotten or missed the /n part. Thanks, the makes things much less messy. And okay, I've been reading another book on Java as well.

So System.out.print() is for a general screen display where as System.out.println() can be used to specify more information separately, but will display it on the same line as System.out.print() is this correct? Sorry for all of the extremely basic questions.
Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: A very basic question about Main.java

Posted 15 June 2010 - 09:49 AM

println just means that it automatically puts the carriage return in there for ya, as opposed to print which with multiple print statements would just run on and on on the same line until you type \n or \r. ^__^
Was This Post Helpful? 0
  • +
  • -

#15 Solar D  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-June 10

Re: A very basic question about Main.java

Posted 15 June 2010 - 09:51 AM

Oooh! Okay, makes sense. Thanks to everyone for steering me in the right direction.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2