Results 1 to 12 of 12
  1. #1
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Question 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

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    when you stick in + signs where a string is expected, it concatenates the strings with one another. you probably already knew that, though.

  3. #3
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Default

    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.

  4. #4
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    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.

  5. #5
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    So how DO you add multiple integers?

  6. #6
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Default

    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

  7. #7
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    This is great, thanks :)
    But still, I want to learn how to fix what I've done wrong with my code.

  8. #8
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Default

    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. :)

  9. #9
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    u add all the values in a newly cerate variable int
    and display that variable
    :) javadeveloper

  10. #10
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Default

    Yup. Thats how you do it.

  11. #11
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    Ah, knew it was something like that. Thank you, coffee and britto bicsjohn.

  12. #12
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Gif decoding/LZW troubles
    By hellochar in forum Advanced Java
    Replies: 2
    Last Post: 07-14-2009, 11:26 PM
  2. Image troubles
    By Theodoreb in forum New To Java
    Replies: 24
    Last Post: 07-14-2009, 12:41 AM
  3. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-20-2009, 11:46 PM
  4. [SOLVED] Array troubles, yes I searched...
    By Reiyn in forum New To Java
    Replies: 11
    Last Post: 04-16-2009, 11:28 PM
  5. StreamCorruptedException and Casting troubles
    By Wassa in forum Networking
    Replies: 2
    Last Post: 02-18-2009, 03:07 PM

Tags for this Thread

Posting Permissions

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