Results 1 to 5 of 5
  1. #1
    JavaLucky is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Simple coding help (integers)

    So I'm having some trouble with a bit of simple code I'm trying to write to print a set of 4 numbers input by the user then calculate the average of those numbers. For some reason I keep getting an error when try to enter the numbers. The program also has to print two words before the numbers in the correct format. Here's what I've done

    public static void main(String[] args)
    {Scanner scan = new Scanner (System.in);
    String heading1 = "Word1";
    String heading2 = "Word2";
    String heading3 = "Num1";
    String heading4 = "Num2";
    String heading5 = "Num3";
    String heading6 = "Num4";
    String heading7 = "Total";

    double Total;
    scan.useDelimiter("[,]");

    String Word1= scan.next();
    String Word2= scan.next();

    int Num1=scan.nextInt();
    int Num2=scan.nextInt();
    int Num3=scan.nextInt();
    int Num4=scan.nextInt();

    Total=(Num1*0.1)+(Num2*0.4)+(Num3*0.2)+(Num4*0.3);
    System.out.printf( "%-15s %10s %12s %10s %10s %10s %10s %n", heading1, heading2, heading3, heading4, heading5, heading6, heading7);
    System.out.printf("%-15s %14s %5s %10s %10s %10s %10s %n", Word1, Word2, Num1, Num2, Num3, Num4, Total);

    The error occurs as soon as the program reaches the number integers (it has no problem with the words). Any help you can provide would be greatly appreciated because I just can't figure out what's going on here.

  2. #2
    JavaLucky is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Simple coding help (integers)

    Sorry, to correct myself, it isn't an average, it's the sum listed next to Total (Num1*0.1)+(Num2*0.4)+(Num3*0.2)+(Num4*0.3)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Simple coding help (integers)

    Please go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    You might also get better responses by learning How to ask questions the smart way

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple coding help (integers)

    Don't use that delimeter and type your entire input on one line separated by spaces and see ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Simple coding help (integers)

    Another recommendation: Code Conventions for the Java Programming Language: Contents

    Variable names should begin with a lowercase letter. Also learn how to indent code correctly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Simple Application Coding
    By Syed Rizvi in forum New To Java
    Replies: 3
    Last Post: 07-29-2013, 10:33 PM
  2. [NEED HELP]coding a simple calculator
    By techy in forum New To Java
    Replies: 1
    Last Post: 05-24-2013, 12:19 PM
  3. Simple Chat Server socket coding issues
    By TheGadgetCat in forum Networking
    Replies: 14
    Last Post: 12-17-2010, 12:54 PM
  4. Replies: 4
    Last Post: 10-25-2010, 03:49 PM
  5. Help with really simple coding
    By tigertomas in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:47 AM

Posting Permissions

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