Results 1 to 12 of 12
Thread: Integer troubles
- 08-12-2009, 07:59 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
Integer troubles
I am constructing a program in which you enter eight numbers. At the end it will display the sum of these numbers. But when I run the code, it simply displays the numbers side by side each other. My IDE is eclipse.
Here is the code:
Java Code:import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String a; a = JOptionPane.showInputDialog ("Welcome to the Averaging Machine Basic! Enter your first number."); String b; b = JOptionPane.showInputDialog ("Enter your second number."); String c; c = JOptionPane.showInputDialog ("Enter your third number."); String d; d = JOptionPane.showInputDialog ("Enter your fourth number."); String e; e = JOptionPane.showInputDialog ("Enter your fifth number."); String f; f = JOptionPane.showInputDialog ("Enter your sixth number."); String g; g = JOptionPane.showInputDialog ("Enter your seventh number."); String h; h = JOptionPane.showInputDialog ("Enter your eighth number."); int a1 = Integer.parseInt(a); int b1 = Integer.parseInt(b); int c1 = Integer.parseInt(c); int d1 = Integer.parseInt(d); int e1 = Integer.parseInt(e); int f1 = Integer.parseInt(f); int g1 = Integer.parseInt(g); int h1 = Integer.parseInt(h); JOptionPane.showMessageDialog(null,"The average of " + a + "," + b + "," + c + "," + d + "," + e + "," + f + "," + g + " and " + h + " is " + a + b + c + d + e + f + g,"Output",JOptionPane.INFORMATION_MESSAGE); } }Last edited by Tb0h; 08-12-2009 at 08:00 PM. Reason: Code brackets AGAIN
- 08-12-2009, 08:29 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
when you stick in + signs where a string is expected, it concatenates the strings with one another. you probably already knew that, though.
- 08-12-2009, 08:56 PM #3
Member
- Join Date
- Aug 2009
- Location
- ...
- Posts
- 12
- Rep Power
- 0
Also, you said average where it should say 'sum'. Just saying. make another variable called sum (an integer) and make that equal what you said at the end.
- 08-12-2009, 09:26 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
Yes; I originally intended this to be an averaging program. But before I do that, I want to figure out how to add integers. Also, yes I knew, emceenugget.
- 08-12-2009, 09:27 PM #5
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
So how DO you add multiple integers?
- 08-12-2009, 09:48 PM #6
Member
- Join Date
- Aug 2009
- Location
- ...
- Posts
- 12
- Rep Power
- 0
sum = int1+int2+int3;
EDIT:
Here, I wrote up a little tut for you:
Java Code:import java.util.Scanner; public class test { public static void main(String[] args) { Scanner s = new Scanner(System.in); //get input from user System.out.print("Please enter the integers to average seperated by a space: "); //ask for integers String[] all = (s.nextLine().split(" ")); //get the individual numbers in string format int total = 0; //initiate our variable for the total for (int i=0; i<all.length; ++i) { //loop through the array's values total += Integer.parseInt(all[i]); //parse the string into an integer and add that to total } total /= all.length-1; //divide by amount of integers in array System.out.println("Average: "+total); } }Last edited by coffee; 08-12-2009 at 10:04 PM. Reason: added commented tutorial
- 08-12-2009, 10:06 PM #7
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
This is great, thanks :)
But still, I want to learn how to fix what I've done wrong with my code.
- 08-12-2009, 10:25 PM #8
Member
- Join Date
- Aug 2009
- Location
- ...
- Posts
- 12
- Rep Power
- 0
Simply enough, instead of adding the integers, you added the string equivalents of the integers. Just create a variable for the total, and add them there. Then, add the total to the string printed on the screen, instead of all those variables. :)
- 08-13-2009, 01:22 PM #9
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
u add all the values in a newly cerate variable int
and display that variable:) javadeveloper
- 08-13-2009, 02:56 PM #10
Member
- Join Date
- Aug 2009
- Location
- ...
- Posts
- 12
- Rep Power
- 0
Yup. Thats how you do it.
- 08-13-2009, 04:31 PM #11
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
Ah, knew it was something like that. Thank you, coffee and britto bicsjohn.
- 08-13-2009, 04:56 PM #12
Member
- Join Date
- Aug 2009
- Location
- ...
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Gif decoding/LZW troubles
By hellochar in forum Advanced JavaReplies: 2Last Post: 07-14-2009, 11:26 PM -
Image troubles
By Theodoreb in forum New To JavaReplies: 24Last Post: 07-14-2009, 12:41 AM -
subclass troubles
By xf021209 in forum New To JavaReplies: 12Last Post: 04-20-2009, 11:46 PM -
[SOLVED] Array troubles, yes I searched...
By Reiyn in forum New To JavaReplies: 11Last Post: 04-16-2009, 11:28 PM -
StreamCorruptedException and Casting troubles
By Wassa in forum NetworkingReplies: 2Last Post: 02-18-2009, 03:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks