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 2 of 2

Thread: Newbie Needs Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie Needs Help

    Hi guys, I entered into a computer science course and I might need this forum for any upcoming projects. With that being said, so far, I learned up from putting integers, doubles, strings, bufferedreader, output to user (the System.out.println), input from user to program (for example num1= Integer.parseInt(myInput.readLine()); ), basic operations of math(adding,subtracting etc.), Math.floor/ceil, for loops, do...while loops, changing words into number of letters being a number, specific decimal places, while loops, if and if else statements.
    Now, my teacher just taught us how to do switch and case.
    and I'm having a problem involving loops.
    The program that we are doing right now involves us looping something if the user does not input anything.
    For example:

    switch (num1)
    {case 1:
    System.out.println ("You have selected Mathematics");
    do
    {System.out.println ("Please enter your average for mathematics.");
    num2= Integer.parseInt(myInput.readLine());}
    while (num2==blank);
    //where String blank= "";

    Can anybody help me on this? I do not know how to do any Boolean even though it is requested in the program when I get the error. Thanks in advance


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

    Default Re: Newbie Needs Help

    Quote Originally Posted by DJMS View Post
    ...so far, I learned up from putting integers, doubles, strings, bufferedreader, output to user (the System.out.println), input from user to program yaada, yaada, yaada...
    OK. You have learned a lot. We get it.


    Quote Originally Posted by DJMS View Post
    The program that we are doing right now involves us looping something if the user does not input anything.
    Now, you have to define "anything."

    I mean, if the program is executing some kind of readLine() function from the System.in stream, and I, the user, don't do "anything," it waits for me to do "something." Now, in this case "something" is defined as pressing zero or more character keys in sequence. The sequence is terminated when I, the user, press the 'Enter' key. Until I, the user, press 'Enter' nothing is presented to the program, even though it can't really be said that I am "not doing anything." I mean, I'm pushing keys like crazy. I just haven't hit 'Enter' yet.


    Now as for your bit of code:

    Assuming myInput is a BufferedReader object or some such thing (as I infer from your preamble about all of the things you have learned), the readLine() method waits for the user to press 'Enter' and then returns the String consisting of a concatenation of the sequence of characters from the keys that the user pressed before hitting the 'Enter' key.

    If the user just pressed 'Enter' and nothing else, the readLine() method returns an "empty" String, and that's what (I'm guessing) you may be looking for, when you say that the user doesn't do "anything."

    "Does not input anything" in your problem statement is defined as pressing 'Enter' and nothing else, right?

    So: Is you assignment to make a loop that waits for the user to enter some "stuff" and keep looping if the user just presses 'Enter' and nothing else?

    Then, starting with what I think I am seeing in your codelet) it could go something like this:
            BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
    .
    .
    .
            String line;
            String blank = "";
            do {
                System.out.print("Enter an integer: ");    
                line = myInput.readLine();
                // Test to see if the line is equal to the String that you have defined as blank.
                if (/* The line is not a blank line */) { // Use the String.equals() method to test for equality
     
                   /* Do something useful with the non-blank line */
     
                }
            } while (/* The line is a blank line */); // If it's a blank line, loop back for more, otherwise terminate the loop


    Cheers!

    Z
    Last edited by Zaphod_b; October 30th, 2012 at 04:11 PM.

Similar Threads

  1. I'm a newbie
    By chanukya4528 in forum Member Introductions
    Replies: 0
    Last Post: July 5th, 2012, 10:04 AM
  2. Yo :D Newbie here =)
    By Sjogern in forum Member Introductions
    Replies: 4
    Last Post: February 21st, 2012, 11:15 AM
  3. Hi, newbie here!!
    By niecah in forum Member Introductions
    Replies: 15
    Last Post: April 28th, 2010, 04:21 AM
  4. Help to a newbie!
    By painthygrave in forum AWT / Java Swing
    Replies: 0
    Last Post: April 7th, 2010, 10:29 PM
  5. I'm a newbie
    By r12ki in forum Member Introductions
    Replies: 2
    Last Post: June 1st, 2009, 06:38 AM