• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to View Array In MessageDialog

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i view Array in JOptionPane.showMessageDialog(array)

Example :



in Main Method :



When i run the program it appears like this [Ljava.lang.String;@379619aa

How can i print Subject 1 then Enter Subject 2 in a new line ? and how can i solve this problem ?

Thanks
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
omar tawfeek
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:



Thank you it's working now like this : Student Subjects = [null, string,string,string,string,string ]

but how can i make it appear like this also without null :

Subject 1 = String
Subject 2 = String
Subject 3 = String
Subject 4 = String
Subject 5 = String

 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write your own method that takes a String[] as an argument and returns a formatted String. Use StringBuilder to append the strings together.
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, array indexes start with zero. Your loop needs to be
 
omar tawfeek
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Write your own method that takes a String[] as an argument and returns a formatted String. Use StringBuilder to append the strings together.



i am trying to do it like that but it always gives me an error in compile .

Carey Brown wrote:In Java, array indexes start with zero. Your loop needs to be



Thank you , but do you know how can i make them appear like this in MessageDialog

Subject 1 = string
Subject 2 = string
Subject 3 = string
Subject 4 = string
Subject 5 = string
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

omar tawfeek wrote:i am trying to do it like that but it always gives me an error in compile .

Post what you wrote and copy and paste error message into the post.
 
omar tawfeek
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

omar tawfeek wrote:i am trying to do it like that but it always gives me an error in compile .

Post what you wrote and copy and paste error message into the post.





In Main Method :



Getting an error in for loop

while run getting this error :

incompatible types: java.lang.String cannot be converted to java.lang.StringBuffer
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Write your own method that takes a String[] as an argument and returns a formatted String. Use StringBuilder to append the strings together.


You aren't reading my suggestion correctly
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you do with the String[] in your class? Why?
 
omar tawfeek
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:Write your own method that takes a String[] as an argument and returns a formatted String. Use StringBuilder to append the strings together.


You aren't reading my suggestion correctly



i know what you mean , but the first issue it doesn't appear my Subject[1,2,3,4,5] in InputDialog
second issue that it still doesn't make new line in MessageDialog :

is it possible to make it with for loop :

String 1 = string
String 2 = string
String 3 = string
String 4 = string
String 5 = string

i am trying to make it with for loop but it doesn't work for me
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

omar tawfeek wrote:is it possible to make it with for loop :

String 1 = string
String 2 = string
String 3 = string
String 4 = string
String 5 = string

i am trying to make it with for loop but it doesn't work for me


1) you will need a loop ----- read the pseudo code I gave you.
2) "it doesn't work" tells me nothing ---- you have to post the code and any error messages.
3) it won't have a new-line unless you put it there.
 
omar tawfeek
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

omar tawfeek wrote:is it possible to make it with for loop :

String 1 = string
String 2 = string
String 3 = string
String 4 = string
String 5 = string

i am trying to make it with for loop but it doesn't work for me


1) you will need a loop ----- read the pseudo code I gave you.
2) "it doesn't work" tells me nothing ---- you have to post the code and any error messages.
3) it won't have a new-line unless you put it there.



i want to make it with my code i just miss something with the new line because for each is doing loop then print Subject 1 after that it shows input then print message subject 2 etc. , i may be doing wrong i have done this :

 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The loop needs to be in the subjectToString() method. If you don't like my name for this method make up one of your own, but DO NOT call it getSubject(). The 'get' prefix is reserved for 'getters', and this is not a getter.


Maybe we should back up a bit. Can you describe in English what you are trying to do?
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  • Variable names must begin with a lower case character.
  • You do not have a StringBuilder object you have an array of Strings.
  • It should be named in a way that you don't have to wonder what kind of data it holds. "subjects" in your original post was correct (except it should be plural).
  •  
    omar tawfeek
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:The loop needs to be in the subjectToString() method. If you don't like my name for this method make up one of your own, but DO NOT call it getSubject(). The 'get' prefix is reserved for 'getters', and this is not a getter.


    Maybe we should back up a bit. Can you describe in English what you are trying to do?



    It's working but it doesn't appear in new line like :

    Subject 1 = string
    Subject 2 = string
    Subject 3 = string
    Subject 4 = string
    Subject 5 = string

    but it appear like this Subject 5 = [ string , string , string , string , string ]


    i am trying to teach myself arrays because it is hard for me to do stuff with array in MessageDialog .
     
    Carey Brown
    Saloon Keeper
    Posts: 8588
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    omar tawfeek wrote:i am trying to teach myself arrays because it is hard for me to do stuff with array in MessageDialog .


    Yes. That is why I'm telling you that you need to create a method to convert an array of Strings into a single formatted String. If you want to check for null, if you want to add "Subject", if you want to add new-lines, you'll need the logic for all of that inside the method.
    Once you've done that you can call that method as a parameter to your call to JOptionPane.showMessageDialog().
     
    omar tawfeek
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    omar tawfeek wrote:i am trying to teach myself arrays because it is hard for me to do stuff with array in MessageDialog .


    Yes. That is why I'm telling you that you need to create a method to convert an array of Strings into a single formatted String. If you want to check for null, if you want to add "Subject", if you want to add new-lines, you'll need the logic for all of that inside the method.
    Once you've done that you can call that method as a parameter to your call to JOptionPane.showMessageDialog().



    i don't know what to write inside the method for printing ^^

    sorry i am still learning
     
    Carey Brown
    Saloon Keeper
    Posts: 8588
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    omar tawfeek wrote:i don't know what to write inside the method for printing.

    You don't "print" or "display" anything inside the method. You create a string that is the composite of your subjects and return that string.
    but turn that into a loop and have the method return 's'.
    To concatenate strings you can do this
     
    omar tawfeek
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    omar tawfeek wrote:i don't know what to write inside the method for printing.

    You don't "print" or "display" anything inside the method. You create a string that is the composite of your subjects and return that string.
    but turn that into a loop and have the method return 's'.
    To concatenate strings you can do this



    it's working but it prints Subject 1 = null etc. , how can i solve this problem
     
    Carey Brown
    Saloon Keeper
    Posts: 8588
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you don't what to print nulls then you need to add an 'if'.

    Get in the habit of always posting the code you've changed.
     
    omar tawfeek
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:If you don't what to print nulls then you need to add an 'if'.

    Get in the habit of always posting the code you've changed.



    thank you so much it's working here is the full code :



    in Main Method :



    Thank you so much Carey Brown
     
    Carey Brown
    Saloon Keeper
    Posts: 8588
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    FYI
     
    omar tawfeek
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:FYI



    Wow , thank you for this method i didn't knew i can ignore nulls with if statement ^^
     
    Carey Brown
    Saloon Keeper
    Posts: 8588
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

  • Variable names must begin with a lower case character.

  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic