Results 1 to 5 of 5
Thread: Simple coding help (integers)
- 08-23-2013, 07:44 AM #1
Member
- Join Date
- Aug 2013
- Posts
- 2
- Rep Power
- 0
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.
- 08-23-2013, 07:46 AM #2
Member
- Join Date
- Aug 2013
- Posts
- 2
- Rep Power
- 0
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)
- 08-23-2013, 08:43 AM #3
Re: Simple coding help (integers)
Please go through the http://www.java-forums.org/forum-gui...w-members.html 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
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-23-2013, 08:44 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-23-2013, 08:49 AM #5
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Simple Application Coding
By Syed Rizvi in forum New To JavaReplies: 3Last Post: 07-29-2013, 11:33 PM -
[NEED HELP]coding a simple calculator
By techy in forum New To JavaReplies: 1Last Post: 05-24-2013, 01:19 PM -
Simple Chat Server socket coding issues
By TheGadgetCat in forum NetworkingReplies: 14Last Post: 12-17-2010, 01:54 PM -
does gui coding in netbeans is just the same with coding without nebeans?
By maizuddin35 in forum NetBeansReplies: 4Last Post: 10-25-2010, 04:49 PM -
Help with really simple coding
By tigertomas in forum New To JavaReplies: 10Last Post: 01-24-2009, 05:47 AM
Bookmarks