Results 1 to 14 of 14
  1. #1
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Need help with JOptionPane and multiplication tables

    I've been stuck at this for hours and I'm starting to lose my mind... I'm writing a program that's supposed to let the user enter an integer, and then the program will calculate the number * numbers(1-10) and output it in a dialog box. While this kinda works, it outputs every line in a seperate box... How do I get all the multiplications in one single dialog box??

    Java Code:
    package uppg1;
    
    import javax.swing.JOptionPane;
    
    public class asd {
    
    	public static void main(String[] args) {
    		String in,out;
    		in = JOptionPane.showInputDialog("Enter an integer ");
    		int number = Integer.parseInt(in);
    		out = " ";
    		
    		int i=1;
    		for (i=1 ; i<11 ; i++){
    			
    			
    			out =  (i + " * " + number + " = " + i*number);
    			JOptionPane.showMessageDialog(null, out);
    		}
    		
    
    	}
    
    }

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

    Default Re: Need help with JOptionPane and multiplication tables

    Build the string in the loop, then outside the loop open the dialog:
    Java Code:
    Loop ten times
        append a new line to the string
    Show dialog with complete string
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Need help with JOptionPane and multiplication tables

    How do I get all the multiplications in one single dialog box??
    Build a String with all the text to be displayed and call the showMessage with that String when done building it.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need help with JOptionPane and multiplication tables

    Thanks, it's working now! However, what if I want to use bufferedreader input but output in with JOptionPane? Nothing is happening after the user is asked to enter an integer.

    Java Code:
    package uppg1;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import javax.swing.JOptionPane;
    
    public class test {
    
    	public static void main(String[] args) throws IOException {
    		String out;
    		BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
    		System.out.println("Enter an integer: ");
    		String answer = input.readLine();
    		
    		int number = Integer.parseInt(answer);
    		out = " ";
    		
    		
    		int i=1;
    		for (i=1 ; i<11 ; i++){
    		    out += (i + " * " + number + " = " + i*number+"\n");
    		}
    		JOptionPane.showMessageDialog(null, out);
    			
    			
    		
    
    	}
    
    }

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with JOptionPane and multiplication tables

    Deja vu! :)
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need help with JOptionPane and multiplication tables

    Jim what do you mean? I can't see what's wrong with that code, but for some reason the program stops executing after I enter an integer now... All I tried to do was change the input method but something is messing with me

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with JOptionPane and multiplication tables

    Quote Originally Posted by iwtljava View Post
    Jim what do you mean?
    Sorry. Inside joke. Norm responded with pretty much the same text just 1 minute after Toll's response. Hang around long enough and you
    will continue to see similar banter.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need help with JOptionPane and multiplication tables

    Ooooh got ya lol. Anyway, would you be so kind to tell me why the code above is not working as intended?

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

    Default Re: Need help with JOptionPane and multiplication tables

    The code in post#4 works for me on a Win10 system.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need help with JOptionPane and multiplication tables

    Really? I'm running it in Eclipse Neon and it's just at a standstill after I get told to input an integer... that's weird

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

    Default Re: Need help with JOptionPane and multiplication tables

    I tested it in a command prompt window.
    I didn't use an IDE.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need help with JOptionPane and multiplication tables

    So that means that my code isn't the one to blame here... Wonder what could cause this

  13. #13
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need help with JOptionPane and multiplication tables

    Oh well I solved it with this

    Java Code:
    final JDialog dialog = new JDialog();
    		dialog.setAlwaysOnTop(true);    
    		JOptionPane.showMessageDialog(dialog, out);
    For some reason, it seems like the parameter null "hides" the box or something, which is usually not the case but I guess it has something to do with the fact that I took the input from the console

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with JOptionPane and multiplication tables

    The windowing system of Windows 10 is keeping Eclipse on top of your message dialog. So the dialog is opening, you just won't see it unless
    you minimize Eclipse.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Multiplication tables applet...
    By New in forum Advanced Java
    Replies: 5
    Last Post: 09-14-2014, 05:07 PM
  2. multiplication of two tables using java and jdbc
    By abhi140291 in forum New To Java
    Replies: 3
    Last Post: 11-25-2013, 03:14 PM
  3. Multiplication Tables game
    By beansnet in forum New To Java
    Replies: 6
    Last Post: 01-04-2013, 05:51 PM
  4. need help with multiplication
    By dakid2 in forum New To Java
    Replies: 10
    Last Post: 03-08-2011, 04:41 AM
  5. displaying tables in joptionpane
    By kaemonsaionji in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 07:53 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
  •