Results 1 to 5 of 5
  1. #1
    egrizzly is offline Member
    Join Date
    Jun 2016
    Posts
    10
    Rep Power
    0

    Default Got stuck on Java Assignment

    I'm sure this is something simple but I cannot seem to figure it out. After I run this program and it displays the total summary for the customers electric charges, well the last customer number always seems to show up as -999....hmmm, any ideas ?

    The link to the java file and the complete instructions for the homework assignment are below. The code is included here in blue as well.


    \\ Link to full assignment instructions \\
    https://www.dropbox.com/s/z7t3isb1wn...tions.pdf?dl=0.

    \\Link to java file (hw2y.java) \\
    https://www.dropbox.com/s/ssd0h8larv...hw2y.java?dl=0

    \\\ My java code I have so far color coded in blue \\\


    import java.util.Scanner;

    import java.text.*;
    import javax.swing.*;


    public class hw2y
    {

    public static double findCharges ( int kwh )
    {
    double total, first300, next300, next400, over1000;

    first300 = kwh * .09;
    next300 = ( kwh - 300 ) * .08 + 27;
    next400 = ( kwh - 400 ) * .06 + 51;
    over1000 = ( kwh - 1000 ) * .05 + 75;

    if ( kwh <= 300 )
    return first300 ;

    if ( kwh <= 600 )
    return next300;

    if ( kwh <= 1000 )
    return next400;

    if ( kwh > 1000 )
    return over1000;

    return .1;
    }

    public static void main( String[] arg )
    {

    Scanner input = new Scanner(System.in);

    int cust, kwh, count;
    double findCharge, total;
    kwh = 0;
    final int SENT = -999;

    String message;

    cust = 0;
    count = 0;
    total = 0;
    message = "****************\n";

    System.out.println ("Please enter a customer number or " + SENT + "to quit:");
    cust = input.nextInt();

    while (cust != SENT)

    {
    count = count + 1;

    System.out.println ("Please enter the usage in KWH");

    kwh = input.nextInt();

    System.out.println ("Please enter a customer number or " + SENT + "to quit:");

    if (cust != SENT)

    cust = input.nextInt();

    message = message + " " + cust + " " + kwh + " " + findCharges(kwh) + "\n";

    total = total + kwh;

    }

    System.out.printf("%s", message);


    System.exit( 0 );

    }


    }

    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Got stuck on Java Assignment

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Be sure the code has proper indentations so it is easier to read.

    the last customer number always seems to show up as -999
    What happens to the -999 that the user enters? Does it get accepted as data?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Got stuck on Java Assignment

    Java Code:
    if (cust != SENT) {
        cust = input.nextInt();
    }
    message = message + " " + cust + " " + kwh + " " + findCharges(kwh) + "\n";
    total = total + kwh;
    Is this doing what you want it to?

    I've added brackets to highlight what that 'if' encompasses...what happens if the user inputs -999 in there?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    egrizzly is offline Member
    Join Date
    Jun 2016
    Posts
    10
    Rep Power
    0

    Default Re: Got stuck on Java Assignment

    Quote Originally Posted by Tolls View Post
    Java Code:
    if (cust != SENT) {
        cust = input.nextInt();
    }
    message = message + " " + cust + " " + kwh + " " + findCharges(kwh) + "\n";
    total = total + kwh;
    Is this doing what you want it to?

    I've added brackets to highlight what that 'if' encompasses...what happens if the user inputs -999 in there?
    if the user enters -999, that's the sentinel value and the code will not execute.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Got stuck on Java Assignment

    the code will not execute.
    What is "the code" that won't execute? Only the statement on line 2 is controlled by an if test. lines 4 and 5 will execute.
    For example if cust contains 123 then
    123 is not equal to -999
    and a new value is read into cust
    if cust is -999, its value is not changed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 04:16 AM
  2. Java Help please xD SO stuck
    By Ethan in forum New To Java
    Replies: 1
    Last Post: 03-19-2011, 12:54 PM
  3. New to java and stuck
    By xpd259 in forum New To Java
    Replies: 2
    Last Post: 12-12-2010, 09:34 AM
  4. First Assignment and already stuck....Newbie Question
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 11:44 PM
  5. stuck on an assignment
    By starchildren3317 in forum New To Java
    Replies: 11
    Last Post: 11-19-2008, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •