I was starting to learn Java. I followed the tutorial on how to install it.
I also checked by typing "javac"(without the quotation marks) in cmd if it works. And yes it gives a whole list of text which means its supposed to work, right?
This is my java code:
class apples{
public static void main(String args[]) {
System.out.printIn("hello youtube");
}
}
I saved it in a folder called 'test' in my c drive.
This is what I typed in cmd:
cd \
dir
now it lists everything in my c drive and one of them is test
cd test
dir
Now it lists everything in test and one of them is 'youtube.java'(the file I named), so I type
javac youtube.java
This doesn't work
This is what it gives me:
youtube.java:3: error: cannot find symbol
System.out.printIn("hello youtube");
symbol: method printIn(string)
location: variable out of type PrintStream
1 error
Can someone help me out with this?
You have a typo in your call. Change
System.out.printIn("hello youtube"); // capital 'I'
to
System.out.println("hello youtube"); // lowercase 'l'
And as has been mentioned already, in Java, the public class in a file must match the filename.
The name of the class must match the filename.
You should use
public class Youtube{
in your code(as class names are capitalized) and call the file Youtube.java.
Also you used In instead of ln.
Use:
System.out.println(
which means "print line to System.out".
Related
I´d wanted to compile a simple Java "Hello World" program like it was repesented on the GeeksforGeeks Hello World Tutorial, by using gcj in Linux Ubuntu. This is the source code:
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, World");
}
}
But gcj threw two errors:
(.text+0x18): undefined reference to main
collect2: error: ld returned 1 exit status
Original output from the terminal:
gcj -o helloworld HelloWorld.java
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
I´d take attention on the requirement, that the .java file should be named after the class which holds main:
Important Points :
The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class and there is at most one public class which contain main() method.
By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.
What am I doing wrong?
You are missing the --main= option, from the documentation, this option is used when linking to specify the name of the class whose main method should be invoked when the resulting executable is run.
gcj -o helloworld --main=HelloWorld HelloWorld.java
I am trying to write a simple program below.
public class HelloWorld {
public static void main (String[] args){
System.out.printIn("Hello, World");
}
}
I saved the file as "HelloWorld.java" and I navigated to the directory and typed in this command in my terminal javac HelloWorld.java. This created a HelloWorld.class file. I then typed in java HelloWorld in my terminal to run the file and I get this error
"Error: Main method not found in class HelloWorld, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
So the error message is telling me I don't have a main method inside the class, I have tried a capital M and lowercase m and I would still get the same error. Does anyone know why this is happening?
You needed a lowercase "L" not an upcase "I". If it helps to remember "println" stands for "print line".
// Copy and paste this to correct your error.
System.out.println("Hello, World");
There is a wrong spelling
//System.out.printIn("Hello, World");
System.out.println("Hello, World");
you wrote a capital "I" instead of small "L"
I'm using the terminal in Mac to try and output some strings using javac. However there are some symbols that don't seem to work, for instance the dollar sign and asterisk:
public class BirdDisplay{
public static void main(String... args){
System.out.println(args[1]);
}
}
and then:
javac BirdDisplay.java
java BirdDisplay sparrow $someBird
I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at BirdDisplay.main(BirdDisplay.java:3)
As far as I know $ is accepted in class names and is a valid identifier, what is the cause of this exception?
You're using it from a shell, where $ is doing environment/shell variable substitution. This has nothing to do with Java - it's how the shell is invoking the process.
Just put it in single quotes:
java BirdDisplay sparrow '$someBird'
Note that the use of a $ as a valid Java identifier is irrelevant, as you're not using it in any source code - the value $someBird purely being used as data in your program (or will be once you've prevented the shell from performing variable substitutions).
As Daisy pointed out, this is because you are running your program in the shell, where $someBird is interpreted as an environment variable. Because $someBird is not an environment variable, the shell replaces it with nothing and you have a command-line arguments array of length 1 instead of length 2. As such, your program has no value for args[1] and you get java.lang.ArrayIndexOutOfBoundsException. You can test this by running this code to print out the length of args:
public class BirdDisplay{
public static void main(String... args){
System.out.println(args.length);
}
}
And now when you do:
javac BirdDisplay.java
java BirdDisplay sparrow $someBird
You will see 1 instead of 2
The static method main, which receives an array of strings. The array should have two elements: the path where the files are located (at index 0), and the name of the files to process (at index 1). For example, if the name was “Walmart” then the program should use “Walmart.cmd” (from which it will read commands) and “Walmart.pro” (from which it will read/write products).
I don't want anyone to write the code for me because this is something I need to learn. However I've been reading this through and the wording is confusing. If someone could help me understand what it wants from me through pseudo-code or an algorithm it would be greatly appreciated.
Where I'm confused is how to initialize arg[0] and arg[1] and exactly
what they are being initialized to.
The main method's String array input argument consists of whatever String arguments you pass to the program's main method when you run the program. For example, here is a simple program that loops over args and prints a nice message with each argument's index and value on a separate line:
package com.example;
public class MainExample {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.printf("args[%d]=%s\n", i, args[i]);
}
}
}
Once you've compiled the program, you can run it on the command-line and pass it some arguments:
java -cp . com.example.MainExample eh? be sea 1 2 3 "multiple words"
Output:
args[0]=eh?
args[1]=be
args[2]=sea
args[3]=1
args[4]=2
args[5]=3
args[6]=multiple words
So lets explain to you
Create a class Inventory : if you don't know how to create a class google it just as is
The static method main: Every executable class in java (at least from the console) has the main method you should google java main method and propably in the same place you find it you will see the default arguments that it receives
When you learn about the default arguments of method main you will undertand about the 'args' that has to be on it
You will have t study the class String google it "java String class"
You will have to study the class File google it "java File class"
At the end everything else would be just logic and I beleave you have learned some at this point.
public class Inventory { // class inventory
public static void main(String[] args) // main method
{
if(args.length==2){ // check if args contains two elements
String filePath = args[0];
String fileName = args[1];
filePath+= System.getProperty("file.separator")+fileName;
File fileCMD = new File(filePath+".cmd");
//fileCMD.createNewFile();
File filePRO =new File(filePath+".pro");
//filePRO.createNewFile();
}
else {
//write the code to print the message Usage: java Inventory Incorrect number of parameters for a while and exit the program.
}
}
This is what I've understood. Basically you have to write a program to create two files, one called fileName.cmd and the other fileName.pro. You have to construct the path of the files using the arguments (input parameters of the main method) and system's file separator. If the arguments don't have two elements you have to print the 'invalid' message. That's it.
Where I'm confused is how to initialize arg[0] and arg[1] and exactly
what they are being initialized to.
You have to use command line to pass the arguments and launch the program , something like the following code in cmd or terminal:
java inventory thePath theFileName
That's how it get initialized.
This is part of a lab exercise for a course I'm doing, it's not assessable, just a learning exercise. Not sure why but the tut didn't go through it, so I just went through it at home but I'm stuck on the last part.
I'm trying to write a java WSDL client to access http://www.nanonull.com/TimeService/TimeService.asmx?WSDL - I should input UTC+10 to display the current time. Below is the code that I have written:
package time;
class Client {
public static void main(String args[]){
TimeService service = new TimeService();
TimeServiceSoap port= service.getTimeServiceSoap();
String result = port.GetTimeZoneTime("UTC+10");
System.out.println("Time is "+result);
}
}
When I try and compile the code I get the following error:
C:\Program Files\Java\jdk1.6.0_22\bin>javac -d . "c:\Program Files\Java\jdk1.6.0
_22\bin\time\Client.java"
c:\Program Files\Java\jdk1.6.0_22\bin\time\Client.java:13: cannot find symbol
symbol : method GetTimeZoneTimeResponse(java.lang.String)
location: interface time.TimeServiceSoap
String result = port.GetTimeZoneTime("UTC+10");
^
1 error
Any thoughts on what I'm doing wrong?
Did you mean
String result = port.getTimeZoneTime("UTC+10");
with a lowercase g? Java method names are case-sensitive, so it won't recognize the method if you get its letter casing wrong. As per both WSDL's TimeServiceSoap documentation and Java naming conventions, method names are in camel case beginning with a lowercase letter.
What does your TimeServiceSoap look like?
Perhaps you meant to use getTimeZoneTime() (starting with a lower case letter)?