Can't Get Program to Compile - java

Pretty new to Java programming, but I'm having trouble getting this to run. I want to read in a word using the StringBuffer class, then reverse it. I am fairly unfamiliar with importing Java libraries, so I'm not sure if I did that correctly. Either way, this is what I have. I tried to compile it (I'm working in the Terminal), but got a few compiler errors. Any help would be appreciated.
import java.util;
import java.io;
public class HW1A {
public static void main (String[] args) {
printBackwards();
}
public static void printBackwards (String[] args) {
StringBuffer backwards = new StringBuffer(args);
System.out.println(backwards.reverse());
}
}
I get the following complier errors:
HW1A.java:1: cannot find symbol
symbol : class util
location: package java
import java.util;
^
HW1A.java:2: cannot find symbol
symbol : class io
location: package java
import java.io;
Thanks.

Since this is a learning exercise have a look at printBackwards, and ask yourself:
What parameters does it need and what are you passing?
Then look at this and what StringBuffer needs - do these match?

Two corrections:
import java.io.*;
To import everything in java.io.
And:
printBackwards(args);
To pass args to your printBackwards method (you declared it correctly, but didn't pass the variable).

The method printBackwards takes a String[] as an argument, but you're not passing anything when you call it.

This compiles :
public class HW1A {
public static void main (String[] args) {
printBackwards(args);
}
public static void printBackwards (String[] args) {
StringBuffer backwards = new StringBuffer(args[0]);
System.out.println(backwards.reverse());
}
}
But you have to decide if the argument to pass to create the StringBuffer is the first element of the args array.

No compilation error... but not sure this is what you want to do...
public class HW1A {
public static void main (String[] args) {
printBackwards(args);
}
public static void printBackwards (String[] args) {
StringBuffer backwards = new StringBuffer();
for (int i = 0; i < args.length; i++) {
backwards.append(args[i]);
}
System.out.println(backwards.reverse());
}
}
Parameters: Hello world!
Output: !dlrowolleH

Related

Arraylist in driver class not resolved to variable

Alright, so in my driver class, I want to call my arraylist from another class and print it out. However, when I attempt to call the method, it says that the variable cannot be resolved to a variable. Does anyone have any idea how I can get it "to be resolved".
Here's my code:
Driver:
public static void showing(){
OtherClass.initializeStrings();
System.out.println(OtherClass.showLists(mouseTypes));
}
Other Class:
static ArrayList <String> mouseTypes;
public static void initializeStrings(){ //initialize the strings outside the main
Strings = new ArrayList <String>();
Strings.add("goodbye");
Strings.add("hello");
Strings.add("hi");
}
public static void main(String[] args) {
OtherClass.initializeStrings();
Driver.showing();
}
public static void showLists(ArrayList list){
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
Any help is appreciated.
Use .showLists(OtherClass.mouseTypes).
However, that breaks both the Single Responsibility Principle, and Encapsulation. Fix it.
Also, you have done a very bad copy-paste job, you have two main methods...

Is this program recursive? If not, how do I make it recursive?

For my programming class, I was told to make a program that uses recursion. I was confused and went to see my friend who was already in the class and he showed me this program. I thought recursion had to use things like r1(x-1), etc. Is it actually recursive? If it's not, how do you make it recursive?
import java.util.*;
import java.io.*;
class ReverseFile
{
private static Scanner infile;
public static void main(String[] args) throws IOException
{
infile= new Scanner(new File("hw_1.txt"));
r1();
}
public static void r1()
{
String s;
if (infile.hasNextLine())
{
s = infile.nextLine();
r1();
System.out.println(s);
}
}
}
It is recursive as r1 calls itself. The fact that no arguments are passed to r1 doesn't matter.

Java command prompt compile error

I started learning Java lately, and trying to make my first program based on book which is the "Hello World" program.
After writing the script on notepad, i try to compile it on the command prompt and then this notice appeared.
first I type: javac javaCode.java
then there a notice said:
javaCode.java:2: error: <identifier> expected
public class static void main(string[] args) {
^
javaCode.java:6: error: reached end of file while parsing
}
^
2 errors
I don't have any ideas what going on here so please give detailed information and how to fix this thing.
public class static void main(string[] args)
Remove class
public static void main(string[] args)
class is different from method. main method syntax doesn't contain class.
Your main method needs to go inside a class: Java won't let you do things any other way.
Try:
public class HelloWorld{
public static void main (String[] args){
//your code goes here
}
}
this the basic structure for a simple program like this.
the class identifier, has to be on the declaration of the class.
public class Caculator {
public static void main(String[] args) {
// Your code here
}
}
It probably should be:
public class JavaCode {
public static void main(String[]args){
}
}
That should do it. ;)

Not able understand the error in java

I was trying to know what would happen if I have two classes with the main function.
I used the following code:
class A {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
class Hello {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
I compiled it using javac First.java (since no class is specified as public , I named the file as First.java); it got compiled without any error and i ran only the class A.Expecting the Hello class to run itself. DIDN'T HAPPEN(?),maybe the program ran out of scope.
So,
I tried compiling the following java code(I am a beginner) but i got the following error.
Code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello,World!");
}
}
class A {
public static void main(String[] args) {
System.out.println("Hello,World!");
Hello.main();
}
}
I compiled it through javac First.javaand got the following error:
method main in class Hello cannot be applied to given types;
Hello.main();
^
I wanted the program to run first the class A's main function and then class Hello's.
What's going wrong here?
Look at the declaration of Hello.main:
public static void main(String[] args)
Now you're trying to call it like this:
Hello.main();
What would you expect the value of args to be, within the method? You need to provide it with a value for args... and fortunately, you already have one, as you're within a method which uses args as a parameter, also of type String[]. So you should just be able to change your code to:
Hello.main(args);
Note that the two args parameters - one for Hello.main and one for A.main are entirely separate. We happen to use pass the value of one to the provide the initial value for the other, but we could easily have written:
Hello.main(null);
or
Hello.main(new String[] { "Some", "other", "strings" });
instead.
Do this in your second class:
public static void main(String[] args) throws IOException {
MyOtherClass.main(args);
}

Can we overload the main() method in Java?

The following code simply prints the word "hi" when run.
import java.util.*;
import java.io.*;
class poly
{
public static void main(String c)
{
System.out.println("enter a char");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(ir);
//char l= br.readLine();
System.out.println("this is "+c);
}
public static void main(String args[]) throws Exception
{
System.out.println("hi");
}
}
Is there a way to overload the main() method?
Your program only starts at one location, so that makes no sense. Furthermore, polymorphism is a totally different concept; that's called overloading, not polymorphism.
What you are trying to do is overloading the main method, not making it polymorphic. And no, you can't do it (or to be precise: overload you can, just the JVM won't call the overloaded versions). The JVM is looking for a main method with a specific signature, namely taking a String[] parameter.
Maybe if you tell us more about the actual problem you are trying to solve, we can offer alternative solutions.
The code is correct, you've overloaded the main method. But, as Peter mentioned, the main thread of an application will always start at the method with the signature
public static void main(String[] args)
and nothing else. For starting an application, JVM will ignore all other main methods. To execute the content, you'll have to call it in your code, like so:
public static void main(String args[]) {
main("me");
}
(Should print "this is me" to the console)
In brief you can overload a main function like:
class A
{
public static void main(String s) {
System.out.println(s);
}
public static void main(int s) {
System.out.println(s);
}
public static void main(String args[])
{
System.out.println("inside main entry");
main("me");
}
}
Output:
inside main entry
me
We can have any number of main method and that can be overloaded but the main thread of an application will always start at the method with the signature:
public static void main(String[] args)
and nothing else.
In Java args contains the supplied command-line arguments as an array of String objects.
You can overload a main method in Java; however, getting the classloader to start from the overloaded main method is going to be quite a trick.
The class you pass the the java executable is inspected, it's static methods are read, and control is passed off to only the one that looks like
public static void main(String[] args) { ... }
Since this is a static method, and Java does not have the concept of inheriting static methods, you have no way to overload this method. Since Java does not have the concept of inheritance of static methods, sub-classing the static bits of a class is a nonsensical idea.
Maybe you want something like this?
import java.util.*;
import java.io.*;
class poly {
public static void main(String c)
{
System.out.println("enter a char");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(ir);
//char l= br.readLine();
System.out.println("this is "+c);
}
public static void main(String args[])throws Exception
{
if (args.length == 1) {
poly.main(args[0]);
}
else {
System.out.println("hi");
}
}
}
The java runtime environment only looks for the exact match to public static void main(String[]), which is defined as the entry-point for standalone java applications.
To simulate the behaviour you want you have to call the other main yourself:
/**
* Your method called main, ignored by the java runtime since
* it does not match the signature. Called by the valid main(String[])
**/
public static void main(String c)
{
System.out.println("enter a char");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(ir);
//char l= br.readLine();
System.out.println("this is "+c);
}
/**
* Starting point of your program, called by the java runtime.
*/
public static void main(String args[])throws Exception
{
if(args.length >= 1){
main(args[0]);
}else{
System.out.println("hi");
}
}
As others point out having several methods with the same name but different parameters is called over*loading* and not polymorphism (over*writing*).
I'm not quite sure what you are asking but I don`t see any reason why you cannot overload the main method.
The "main method I think you are referring to is
public static void main(String c)
{
System.out.println("enter a char");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(ir);
//char l= br.readLine();
System.out.println("this is "+c);
}
To overload this method I would create a method such as
public static void main(){}
This must be in the same class as the other main method to make it overloaded.
The static keyword means common to the class.....(there is no need to use an object to access the method from the main(String[] args) method.
In my ind creating an overloaded method in a class is the same as creating a different method.
Different arguments, parameter list ====> different signature ======> different method.
I hope this helps.

Categories

Resources