Java Help for a Beginner

Discussion in 'Java' started by Booter, Oct 23, 2009.

  1. Booter

    Booter Guest

    Hello all,

    I am working on a program that can takes numbers in as grades with a
    delimiter of a ",". I have most of the code working but for some
    reason I can never get out of the while loop in the code. If anyone
    can help it would be greatly appreciated.

    *****************CODE**************

    import java.util.*;// need scanner

    public class Grades
    {
    public static void main (String[] args)
    {
    Scanner classGrades = new Scanner(System.in);
    Scanner delimiter = new Scanner(System.in);

    System.out.print("Welcome to the Grade Calculator Program \n\n"
    + "Please enter the student scores as integers between 0 and
    100.\n"
    + "Separate the scores with a comma.\n"
    + "Follow the last student score with a negative number.\n"
    + "Enter the grades here: ");

    //declairations for vars needed
    int grade = 0;
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int f = 0;
    int total = 0;
    int low = 0;
    int high = 0;
    double avg = 0;
    double sum = 0;

    //set delimiter to a ","
    delimiter.useDelimiter(",");

    //while loop to go though the input grades
    while (delimiter.hasNextInt())
    {
    System.out.println(grade);

    grade = delimiter.nextInt();

    if (grade >= 0)
    {
    System.out.println("test 2");

    //assign low and high
    if (grade > high)
    high = grade;
    else if (grade < low)
    low = grade;

    // assign grades
    if (grade >= 90)
    {
    a++;
    sum = sum + grade;
    }
    else if (grade >= 80 && grade <= 89)
    {
    b++;
    sum = sum + grade;
    }
    if (grade >= 70 && grade <= 79)
    {
    c++;
    sum = sum + grade;
    }
    if (grade >= 60 && grade <= 69)
    {
    d++;
    sum = sum + grade;
    }
    if (grade < 60)
    {
    f++;
    sum = sum + grade;
    }

    System.out.println(grade);
    total++;

    }
    else
    break;
    }

    avg = sum / total;

    System.out.print("\n\n");
    System.out.println("Grade Calculations:");
    System.out.println("Total number of grades = " + total);
    System.out.println("Low score = " + low);
    System.out.println("High score = " + high);
    System.out.println("Average score = " + avg);
    System.out.println("Number of A's = " + a);
    System.out.println("Number of B's = " + b);
    System.out.println("Number of C's = " + c);
    System.out.println("Number of D's = " + d);
    System.out.println("Number of F's = " + f);

    }
    }
    Booter, Oct 23, 2009
    #1
    1. Advertising

  2. Booter

    Lew Guest

    Booter wrote:
    > I am working on a program that can takes numbers in as grades with a
    > delimiter of a ",". I have most of the code working but for some
    > reason I can never get out of the while loop in the code. If anyone
    > can help it would be greatly appreciated.
    >
    > *****************CODE**************
    >
    > import java.util.*;// need scanner
    >
    > public class Grades
    > {
    > public static void main (String[] args)


    Do not embed TAB characters in Usenet posts. Use spaces, a maximum of four
    per indent level.

    People have to be able to read your code to help you with it.

    > {
    > Scanner classGrades = new Scanner(System.in);
    > Scanner delimiter = new Scanner(System.in);


    This is probably your problem. You've declared two scanners for System.in and
    you don't seem ever to use the first one. Why do you need two? Why don't you
    use the first one?

    > System.out.print("Welcome to the Grade Calculator Program \n\n"
    > + "Please enter the student scores as integers between 0 and
    > 100.\n"


    Those darn TAB characters!

    > + "Separate the scores with a comma.\n"
    > + "Follow the last student score with a negative number.\n"
    > + "Enter the grades here: ");
    >
    > //declairations [sic] for vars needed
    > int grade = 0;
    > int a = 0;


    It is better in languages like Java to name a variable with a whole word that
    indicates its meaning in the logic of the problem. Readers have to ask, "What
    is an 'a'?" A better name would be "aCount" or "countOfA" or "numberOfAs".

    > int b = 0;
    > etc.


    I took a guess, but for this kind of question you should also report
    (copied-and-pasted) what your test inputs and resulting outputs were.

    --
    Lew
    Lew, Oct 23, 2009
    #2
    1. Advertising

  3. Booter

    Booter Guest

    On Oct 22, 5:54 pm, Lew <> wrote:
    > Booter wrote:
    > > I am working on a program that can takes numbers in as grades with a
    > > delimiter of a ",".  I have most of the code working but for some
    > > reason I can never get out of the while loop in the code.  If anyone
    > > can help it would be greatly appreciated.

    >
    > > *****************CODE**************

    >
    > > import java.util.*;// need scanner

    >
    > > public class Grades
    > > {
    > >    public static void main (String[] args)

    >
    > Do not embed TAB characters in Usenet posts.  Use spaces, a maximum of four
    > per indent level.
    >
    > People have to be able to read your code to help you with it.
    >
    > >    {
    > >            Scanner classGrades = new Scanner(System.in);
    > >            Scanner delimiter = new Scanner(System.in);

    >
    > This is probably your problem.  You've declared two scanners for System..in and
    > you don't seem ever to use the first one.  Why do you need two?  Why don't you
    > use the first one?
    >
    > >            System.out.print("Welcome to the Grade Calculator Program \n\n"
    > >                            + "Please enter the student scores as integers between 0 and
    > > 100.\n"

    >
    > Those darn TAB characters!
    >
    > >                            + "Separate the scores with a comma.\n"
    > >                            + "Follow the last student score with a negative number.\n"
    > >                            + "Enter the grades here: ");

    >
    > >            //declairations [sic] for vars needed
    > >            int grade = 0;
    > >            int a = 0;

    >
    > It is better in languages like Java to name a variable with a whole word that
    > indicates its meaning in the logic of the problem.  Readers have to ask, "What
    > is an 'a'?"  A better name would be "aCount" or "countOfA" or "numberOfAs".
    >
    > >            int b = 0;
    > > etc.

    >
    > I took a guess, but for this kind of question you should also report
    > (copied-and-pasted) what your test inputs and resulting outputs were.
    >
    > --
    > Lew


    I was going through debug mode in Eclipse and noticed that when I got
    to a negative number that my line:

    grade = delimiter.nextInt();

    was not preforming as expected. Does this method get negative
    numbers?
    Booter, Oct 23, 2009
    #3
  4. Booter

    Lew Guest

    Booter wrote:
    > I was going through debug mode in Eclipse and noticed that when I got
    > to a negative number that my line:
    >
    > grade = delimiter.nextInt();
    >
    > was not preforming as expected. Does this method get negative
    > numbers?


    How did you expect it to perform?

    What input did you give it (I ask again)?

    How did it perform?

    Is its action not consistent with
    <http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#nextInt()>
    ?

    --
    Lew
    Don't quote sigs.
    Like this one.
    Do answer the questions people ask when trying to help you.
    Lew, Oct 23, 2009
    #4
  5. Booter

    markspace Guest

    Normally, I'd say "do your own homework." It's important to ask your
    instructor or TA so they know where you are having trouble. However,
    this may be a subtle error.

    Booter wrote:

    > //set delimiter to a ","
    > delimiter.useDelimiter(",");


    You might try ",\\s*" instead. Normally there's white space in a file
    like this, and I'm not really sure what the response of the scanner will
    be if it tries to convert an int with white space. Note that white
    space includes newlines, so I don't see how this will progress to a new
    line as you have it now. If you have white space before the comma,
    you'll need something like "\\s*,\\s*".

    I could be all wrong, and I didn't test it, but it's something to look at.
    markspace, Oct 23, 2009
    #5
  6. Booter

    Booter Guest

    On Oct 22, 6:27 pm, markspace <> wrote:
    > Normally, I'd say "do your own homework."  It's important to ask your
    > instructor or TA so they know where you are having trouble.  However,
    > this may be a subtle error.
    >
    > Booter wrote:
    > >            //set delimiter to a ","
    > >            delimiter.useDelimiter(",");

    >
    > You might try ",\\s*" instead.  Normally there's white space in a file
    > like this, and I'm not really sure what the response of the scanner will
    > be if it tries to convert an int with white space.  Note that white
    > space includes newlines, so I don't see how this will progress to a new
    > line as you have it now.  If you have white space before the comma,
    > you'll need something like "\\s*,\\s*".
    >
    > I could be all wrong, and I didn't test it, but it's something to look at..


    So I tested it with the the following input:

    1,2,3,-1

    When it goes through the while loop it works until I get to the part
    where

    grade = delimiter.nextInt();

    tries to get the -1. Once it reaches that line it just stalls and
    doesn't progress any further.
    Booter, Oct 23, 2009
    #6
  7. Booter

    Roedy Green Guest

    On Thu, 22 Oct 2009 17:19:52 -0700 (PDT), Booter
    <> wrote, quoted or indirectly quoted someone who
    said :

    > grade = delimiter.nextInt();
    >
    >was not preforming as expected. Does this method get negative
    >numbers?


    Javadoc says:

    Scans the next token of the input as an int. This method will throw
    InputMismatchException if the next token cannot be translated into a
    valid int value as described below. If the translation is successful,
    the scanner advances past the input that matched.

    If the next token matches the Integer regular expression defined above
    then the token is converted into an int value as if by removing all
    locale specific prefixes, group separators, and locale specific
    suffixes, then mapping non-ASCII digits into ASCII digits via
    Character.digit, prepending a negative sign (-) if the locale specific
    negative prefixes and suffixes were present, and passing the resulting
    string to Integer.parseInt with the specified radix.

    That means it wants simple ints, no spaces, no commas, but a single
    lead - is ok.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Nothing is so good as it seems beforehand.
    ~ George Eliot (born: 1819-11-22 died: 1880-12-22 at age: 61) (Mary Ann Evans)
    Roedy Green, Oct 23, 2009
    #7
  8. Booter

    Booter Guest

    On Oct 22, 7:13 pm, Roedy Green <>
    wrote:
    > On Thu, 22 Oct 2009 17:19:52 -0700 (PDT), Booter
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    > >   grade = delimiter.nextInt();

    >
    > >was not preforming as expected.  Does this method get negative
    > >numbers?

    >
    > Javadoc says:
    >
    > Scans the next token of the input as an int. This method will throw
    > InputMismatchException if the next token cannot be translated into a
    > valid int value as described below. If the translation is successful,
    > the scanner advances past the input that matched.
    >
    > If the next token matches the Integer regular expression defined above
    > then the token is converted into an int value as if by removing all
    > locale specific prefixes, group separators, and locale specific
    > suffixes, then mapping non-ASCII digits into ASCII digits via
    > Character.digit, prepending a negative sign (-) if the locale specific
    > negative prefixes and suffixes were present, and passing the resulting
    > string to Integer.parseInt with the specified radix.
    >
    > That means it wants simple ints, no spaces, no commas, but a single
    > lead - is ok.
    > --
    > Roedy Green Canadian Mind Productshttp://mindprod.com
    >
    > Nothing is so good as it seems beforehand.
    > ~ George Eliot (born: 1819-11-22 died: 1880-12-22 at age: 61) (Mary Ann Evans)


    Ok well that is all I have is a single - in front of the last number
    but it just stalls once it reaches that point previously stated.

    If it helps when the input is 1,2,3,-1,5 the loop will go through and
    not get hung up on the -1 but will not do anything for the -1 or 5.
    Booter, Oct 23, 2009
    #8
  9. Booter

    markspace Guest

    Booter wrote:

    > 1,2,3,-1


    ----------^

    Here it is.

    Delimiters in Scanner go on both sides of the token. So it's waiting
    for a , after the -1 (or more digits), and stalls, waiting forever. I'd
    add a newline to the delimiter, or maybe just whitespace.

    Here's what fixed this problem for me:

    //set delimiter to a "," or newline
    delimiter.useDelimiter("(\\s*,\\s*)|\n");


    This could probably use some improvement still.

    >
    > When it goes through the while loop it works until I get to the part
    > where
    >
    > grade = delimiter.nextInt();
    >
    > tries to get the -1. Once it reaches that line it just stalls and
    > doesn't progress any further.



    'cause it's waiting for more input.

    Really, you should have talked to your instructor or TA. This kind of
    subtle use of regex is quite beyond a beginning student.

    Please bring this to your instructor's immediate attention. They should
    advise the beginner of the proper regex to use in cases like this.
    markspace, Oct 23, 2009
    #9
  10. Booter

    Booter Guest

    On Oct 22, 7:45 pm, markspace <> wrote:
    > Booter wrote:
    > > 1,2,3,-1

    >
    > ----------^
    >
    > Here it is.
    >
    > Delimiters in Scanner go on both sides of the token.  So it's waiting
    > for a , after the -1 (or more digits), and stalls, waiting forever.  I'd
    > add a newline to the delimiter, or maybe just whitespace.
    >
    > Here's what fixed this problem for me:
    >
    >    //set delimiter to a "," or newline
    >    delimiter.useDelimiter("(\\s*,\\s*)|\n");
    >
    > This could probably use some improvement still.
    >
    >
    >
    > > When it goes through the while loop it works until I get to the part
    > > where

    >
    > > grade = delimiter.nextInt();

    >
    > > tries to get the -1.  Once it reaches that line it just stalls and
    > > doesn't progress any further.

    >
    > 'cause it's waiting for more input.
    >
    > Really, you should have talked to your instructor or TA.  This kind of
    > subtle use of regex is quite beyond a beginning student.
    >
    > Please bring this to your instructor's immediate attention.  They should
    > advise the beginner of the proper regex to use in cases like this.


    Thank you every one for all of your help. I could not have gotten
    this done without you.
    Booter, Oct 23, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    562
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Kristin

    Beginner Java...need help!

    Kristin, Mar 21, 2006, in forum: Java
    Replies:
    5
    Views:
    486
    Bonney Armstrong
    Mar 22, 2006
  3. Rensjuh
    Replies:
    7
    Views:
    953
    Mabden
    Sep 2, 2004
  4. Replies:
    2
    Views:
    337
    TechBookReport
    Sep 5, 2006
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    202
    7stud --
    Apr 12, 2011
Loading...

Share This Page