Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Newb java problem

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Newb java problem

    Hello guys,

    I just started my first Java course and am finding it interesting, but am having some small issues. The following is an example provided by my course that I copied into Bluej:

    importjava.io*;
    public class VoiceExample {
    public static void main(String[] args) {
    // first comment
    // Get the number of seconds from the user.
    // Create a ConsoleReader object to simplify input.
    ConsoleReader console = new ConsoleReader(System.in);
    System.out.print("Please enter a number of seconds: ");
    double sec = console.readDouble();

    // second comment
    // Calculate number of kilobytes.
    final int KBYTE = 1024;
    // 1 kb = 1024 bytes
    final int VOICE_BYTES_PER_SECOND = 8000;
    final int VOICE_KB_PER_SECOND = VOICE_BYTES_PER_SECOND / KBYTE;
    double voiceKB = sec / VOICE_KB_PER_SECOND;

    // third comment
    // Print the result on the screen
    System.out.println();
    }

    } // end of class VoiceExample

    I copied it as follows:

    importjava.io*;
    public class VoiceExample
    {
    public static void main( String[] args) {
    ConsoleReader console = new ConsoleReader(System.in);
    System.out.print("Please enter the number of seconds: ");
    double sec = console.readDouble();
    final int KBYTE = 1024;
    final int VOICE_BYTES_PER_SECOND = 8000;
    final int VOICE_KB_PER_SECOND = VOICE_BYTES_PER_SECOND / KBYTE;
    double voiceKB = sec / VOICE_KB_PER_SECOND;
    System.out.println();
    }
    }

    I get a "class, interface, or enum expected" error at the importjava.io*; line, but I'm sure there are other problems as well. Any experienced people with advice? I'd really appreciate it. Thanks alot.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Newb java problem

    Quote Originally Posted by Tedstriker View Post
    I just started my first Java course and am finding it interesting, but am having some small issues. The following is an example provided by my course that I copied into Bluej:
    importjava.io*;
     
    public class VoiceExample {
      // ....
    }

    I get a "class, interface, or enum expected" error at the importjava.io*; line, but I'm sure there are other problems as well. Any experienced people with advice?
    The error is telling you that this line is wrong, and if you look closely at it, you'll see why.

    Notice the difference between your line, and mine?
    import java.io*;
     
    public class VoiceExample {
      // ....
    }

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Newb java problem

    Thanks for the help. I'm now getting a ";" expected error message on the import java.io*; line.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Newb java problem

    Look at the source from which you originally copied the code.

    Look again.

    Still don't see any difference between its import line and yours?

    Then say it out loud phonetically and pay attention to spacing and punctuation:

    import(space)java(dot)io(dot)*(semicolon)

    import java.io.*;

    ELTC (Every Little Thing Counts!)

    Even very experienced people make mistakes (yes! really). After you have seen error messages like yours a few million times (as I have), you will gain the ability to spot "minor" discrepancies like a missing dot, even though the message itself might not be specifically enlightening.

    Some day your Java learning experience may actually reach a point where that particular syntax is explained. It may not make sense right now (or even for some time after it is initially introduced to you), so treat it as an idiom. Just do it.


    Cheers!

    Z
    Last edited by Zaphod_b; October 7th, 2012 at 11:41 AM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    curmudgeon (October 7th, 2012)

  6. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Newb java problem

    I second all that Mr. Beeblebrox has to say. Also, if you're not using an IDE, and I agree with you're not using one when just starting out, be sure to compile early and often, perhaps after each new line or two of code that you've added. Then if you find a compilation error, be sure to fix it before trying to add any more new code. I usually start out with a code "skeleton", mainly the class name and declaration, then compile, then add variables and constants, then compile, then add method skeletons (the method declarations without their bodies), then compile, then flesh out the methods one at a time,...

  7. #6
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Newb java problem

    Thanks again, frustrating that the extra dot wasn't in the text. Oh well.

    I know this is a complete newb thread and I appreciate your guys help. The next one is: "can't find symbol - class ConsoleReader"

    Thanks for your patience, I look forward to hearing from you.

  8. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Newb java problem

    Quote Originally Posted by Tedstriker View Post
    I know this is a complete newb thread and I appreciate your guys help. The next one is: "can't find symbol - class ConsoleReader"
    The next big skill to master -- checking out the Java API. Do so and you'll see that ConsoleReader doesn't exist. Did you perhaps get this class from your instructor?

  9. #8
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Newb java problem

    Thanks for the help guys,

    Rather than keep posting error after error, I'm going to go back to the drawing board and try to figure this out, and review the course materials again. If I have any other issues I'll post back again. BTW this forum is awesome and I really find this stuff interesting. Thanks again.

  10. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Newb java problem

    Regarding ConsoleReader:

    Being kind of new to Java, I have wondered why non-standard elementary classes like ConsoleReader keep showing up on programming help forums like this.

    So, I did a little research, and here's what I conclude:

    Before Java 5 was released, eight or nine years ago, there was no java.util.Scanner class, and beginners were saddled with learning about BufferedReaders and InputStreams and stuff like that. There were a lot of things floating around to make it a little easier to read numbers from user console input, and some of them involved a class named ConsoleReader. For example ConsoleReader.java (From a syllabus of a course with a date of 2001.) There are a lot of different (non-standard) classes with the same or similar names, so you were required to get yours from your own instructor.


    Anyhow...

    The Java source files for things like ConsoleReader were part of class "handouts." Maybe even a whole package of I/O was given out, and students were required to use whatever the instructor said for them to use. That wasn't very unreasonable for the times, I guess.

    However...

    Nowadays, I don't see why they don't just teach about java.util.Scanner on the first day of beginners' classes (well, maybe the second or third day) and be done with it. I know that for some institutions, teaching materials and methods (and instructors) haven't been updated for more than eight or nine years, so I guess that's why we still see non-standard classes like ConsoleReader being given to somewhat-bewildered students who already have enough stuff being pushed into one ear that it just runs out the other. Maybe a little will actually stay inside, but...

    Bottom line: If you aren't actually required to use ConsoleReader, then the first part of the hokey example can use something that is standard and just as easy to use and doesn't require you to compile a separate class. It can look something like this:
    import java.util.*; // Note that Scanner is in java.util, not java.io
     
    public class Whatever {
        public static void main(String[] args) {
            // first comment
            // Get the number of seconds from the user.
            // Create a Scanner object to get user input.
     
            Scanner console = new Scanner(System.in);
            System.out.print("Please enter a number of seconds: ");
     
            double sec = console.nextDouble();
     
            // From Zaphod_b:
            //   Everything else can be the same as the original
            //   (and will give the same hokey wrong results).
            //
    .
    .
    .
    Then, helpers on this forum, being more tuned into currently standard Java classes, might be more willing and able to offer specific, detailed assistance.




    Cheers,

    Z
    Last edited by Zaphod_b; October 7th, 2012 at 03:53 PM.

  11. The Following User Says Thank You to Zaphod_b For This Useful Post:

    curmudgeon (October 7th, 2012)

Similar Threads

  1. [SOLVED] I am a java newb and I am having a problem with my while loops.
    By Deaththekid in forum Loops & Control Statements
    Replies: 22
    Last Post: July 10th, 2011, 07:26 AM
  2. [SOLVED] newb help..
    By Hallowed in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 05:15 PM
  3. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. help a newb
    By WahLao in forum Java Theory & Questions
    Replies: 1
    Last Post: January 4th, 2010, 02:13 PM