How do I Fix this error?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: How do I Fix this error?

  1. #1
    Join Date
    Feb 2004
    Posts
    25

    How do I Fix this error?

    I'm just learning java and I'm trying to get this small this to work.

    public class Lab2

    {
    String stringone;
    {
    System.out.println ("My name is Matt" + stringone.charAt(8));
    }


    It gets some error about their not being a "main" no such method. Tried adding public static voic main (??) I don't know what to put in there. Before I wasn't using the strings (1st lab) so i left the (String [] args) as it is.


    What should I do???

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    this code will not compile for at least the following reasons:

    mismatched numbers of { and }
    stringone might not have been given a value
    no method signature for the {block} enclosing the println..

    here is a basic class skeleton. copy and paste it, then fill in the details:

    Code:
    public class CLASSNAME{
    
      //class-wide variables go here
    
      public CLASSNAME(){
        //this is a constructor. constructors are used to perform
        //any operations that must be done before a class is usable
        //put any such code here :)
      }
    
    /*
      public RETURNTYPE METHODNAME(PARAMETERS) throws EXCEPTIONS{
        //this is a method. the returntype is either a java primitive
        //or some kind of object. if a method never returns anything
        //it's returntype is 'void'. pick a methodname that makes
        //sense and describes what the method does.
        //parameters are things you can supply to the method
        //to change its behaviour. not all methods have them
        //excpetions are thrown when things go wrong. not 
        //all methods throw them
        //put your method code here
      }
    */
    
      public static void main(String[] argv){
        //static means that you dont have to make an instance
        //of this object before you use it. main() MUST be static
        //- dont ask why, it will become apparent later
        //main also takes parameters, an array of strings. this
        //array is gathered from the command line. anything after
        //the class name, is put in the array, so:
        //java copyFiles c:\*.mp3 TO d:\mymp3
        //would cause the following array:
        //argv[0] = "c:\*.mp3"
        //argv[1] = "TO"
        //argv[2] = "d:\mymp3"
        //do you see how we can use this to make copyFiles work?
    
        //if you dont have a main, you cant run a program. put
        //your code in here
      }
    } //this bracket is the closing bracket for the CLASS
    now.. in your case, you wont need all that.. no class variables, no constructors.. you ahvent got to objects yet, so just make your class like this:

    Code:
    public class CLASSNAME{
      public static void main(String[] argv){
        //put your code here
      }
    }
    another note. you cant do this:

    String myString;
    System.out.println("the char at position 10 is: " + myString.charAt(10);

    why not? well, you tell me.. what's at string position 10?
    we declared the string with "String myString".. that is, we saiud to java 'im going to use a String later, and it is called myString.. but im not setting the contents at this time, just remember the name'
    So java says OK.. and when you try and get content out of the string, before you even put anything in it.. java cant do it
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    25
    Please bear with me, since i am a newbie at this whole java thing and it is confusing me horribly.

    Since I told java to just hold the string, how do I get it to initialize?

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    The following is a declaration:

    String myString;

    The following are initialtizations:

    myString = "hello";
    myString = new String("hello");
    myString = someOtherString;

    They can be combined:

    String myString = "hello";

    -
    You msut at least, declare, then initialize anything before you can use it.. You cannot initialize before declaring.

    As to how to initialize in your particular case, depends what your brief is.. where is the content of stringone supposed to come from?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    25
    3. Write a program containing a series of Java statements that implement the following tasks:
    (Print means print to the screen)

    a) Print the 9th character of the 1st string.

    b) Use compareTo( ) to compare strings 2 and 3. Print the integer generated by the comparison.

    c) Print the index (position) of the character G in the second string.

    d) Print the index of the last occurrence of the letter g in the third string.

    e) Print the length of each string with an appropriate message (i.e., String one has a length of xx characters)

    f) Convert all the characters in the first string to uppercase and print the string.

    g) Convert all the characters in the third string to lower case and print the string.

    h) Use the regionMatches method to determine if the 4th through the 9th characters of strings 2 and 3 match. The character case should be ignored.

    i) Use one other method of the string class and print your result.



    That is my lab descriptions.

  6. #6
    Join Date
    Feb 2004
    Posts
    25
    the content of string one is supposed to be

    "Albert Einstein: Education is what remains after one has forgotten everything he learned in school."

    I'm then to println the 8th character of the line.

  7. #7
    Join Date
    Feb 2004
    Posts
    25
    this is what I ahve so far:

    public class Gallisdorfer2
    {
    public static void main(String[] argv)
    {
    String stringone = System.out.println("Albert Einstein: Education is what remains after one has forgotten everything he learned in school"+ stringone.charAt(8));
    }
    }



    I get this error:

    incompatible types
    found : void
    required: java.lang.String

    what do I sub in for void?

  8. #8
    Join Date
    Feb 2004
    Posts
    808
    you dont...

    Assign the string to stringone..
    THEN print it..
    dont try to do everything in one line!
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  9. #9
    Join Date
    Feb 2004
    Posts
    808
    ps.. assign <-> initialize, are interchangeable terms.. just making you aware (variables are (re)initialized by assigning a value to them...)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  10. #10
    Join Date
    Feb 2004
    Posts
    25
    missing method body, or declare abstract
    public static void main(String[] argv);

    I'm now getting this error, why do I have to have a method body? do I need one?

  11. #11
    Join Date
    Feb 2004
    Posts
    541
    Yes you do need a method body. The body doesn't have to contain anything, but it has to be there. By body, i mean it has to have an opening and closing curly bracket block, which surrounds the scope of the method. If you don't have the method body then you have to declare it (and the whole class i believe) to be abstract. You're not ready to worry about abstract classes yet though.

    Your error was becasue you put a semi colon after the method declaration (a typo I make all the time since all statements are ended with a semi colon). If you change the line
    Code:
    public static void main(String[] argv);
    to
    Code:
    public static void main(String[] argv)
    {
    }
    the error will be fixed. Your program won't actually do anything though, since the main method is the one that is called at runtime. If it has no code in the body section your program will end as soon as it's started. You put the code that you want to run in the main method, or you call other methods that contain the code you want to run.

    It's important you get to understand what these errors that you're making mean, since they're to do with the basic building blocks of java (how to define variables, classes etc), and you won't be able to do anything until they're put right. Take the time to read whatever notes your lecturer has given you (or read a good java book).

  12. #12
    Join Date
    Feb 2004
    Posts
    25
    Thanks guys I finally got that part to work

    But I am trying to compare two strings now.





    System.out.println(stringtwo.compareTo(stringthree));


    ^^ I don't get any errors from this but Nothing is printing to the screen...


    is that what I would want to have to compare them? How to I print the Integer?

  13. #13
    Join Date
    Feb 2004
    Posts
    541
    When i use this code it prints out integers to the command screen
    Code:
    public class StringComparison
    {
        public static void main(String[] args)
        {
            String string1 = "bob";
            String string2 = "alice";
    
            System.out.println(string1.compareTo(string2));
        }
    }
    You'll probably have to show us more than just that line of code for us to determine why nothing is being printed out, as the line of code you showed us there printed fine for me.

  14. #14
    Join Date
    Feb 2004
    Posts
    25
    public class Gallisdorfer2

    {
    public static void main(String[] argv)
    {
    String stringone;
    stringone = ("Albert Einstein: Education is what remains after one has forgotten everything he learned in school.");
    System.out.println (stringone);
    System.out.println (" Step A: ");

    System.out.println ("The Character at the 9th space is: " + stringone.charAt(8));


    }
    {
    String stringtwo;
    stringtwo = ("Mark Twain: Golf is a good walk spoiled.");

    String stringthree;
    stringthree = ("Mark Twain: The man who doesn't read good books has no advantage over the man who can't read them.");

    System.out.println(stringtwo.compareTo(stringthree));



    }





    }

    This is all of my code, the first part (step a) was what I was having problems with first...

  15. #15
    Join Date
    Feb 2004
    Posts
    541
    Originally posted by Matgallis
    public class Gallisdorfer2

    {
    public static void main(String[] argv)
    {
    String stringone;
    stringone = ("Albert Einstein: Education is what remains after one has forgotten everything he learned in school.");
    System.out.println (stringone);
    System.out.println (" Step A: ");

    System.out.println ("The Character at the 9th space is: " + stringone.charAt(8));


    }//THIS BRACE HERE ENDS THE MAIN METHOD
    {//THIS BRACE HERE STARTS A NEW BLOCK OF SCOPE
    String stringtwo;
    stringtwo = ("Mark Twain: Golf is a good walk spoiled.");

    String stringthree;
    stringthree = ("Mark Twain: The man who doesn't read good books has no advantage over the man who can't read them.");

    System.out.println(stringtwo.compareTo(stringthree));



    }





    }

    This is all of my code, the first part (step a) was what I was having problems with first...
    Your problem lies with the two curly braces that i've added comments next to. As I mentioned above, the main method is the one that is called at runtime by the jvm. Once the end of the main method is reached the flow of control leaves your program, and your program is ended. The braces you added ended the main method straight after the line
    Code:
    System.out.println ("		Step A: ");
    This is the last line of code that is executed in your program, nothing after this line will ever be executed. If you take out both of the brackets that i've commented next to the program will work fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center