H
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
H
When posting code, please use the highlight tag to preserve formatting. Also, code should be in the form of an SSCCE. For example, I'm not sure what's giving you trouble- does the algorithm work? Does the applet show up? Start over and post only the code that's giving you trouble, hard-coding the rest (if your problem is displaying a String on a gui, write code that displays a hard-coded String on a gui first).
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Well, this is the section of code we really care about:
Notice how you're appending the String so it contains the counts, separated by commas.
You say you want that String to contain more information. Why don't you just append that information the same way you append the comma?
Also, System.out.println() is your best friend. Don't be afraid to use it to check the value of the String as you go through the loop!
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I understand what you mean by this but I am not sure on how to lay it out so it appears how i need it too? .
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I just realized that you're using AWT instead of Swing. I'm not sure how AWT deals with escape characters and html. Is there a reason you aren't using Swing?
Also, I was wrong about escape characters working in components, at least for JLabels. Use html instead. Here's an example:
import javax.swing.JFrame; import javax.swing.JLabel; public class Test{ public static void main(String[] args) { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("<html>one<br/>two</html>"); frame.add(label); frame.setSize(100, 100); frame.setVisible(true); } }
Alternatively, you could split this up into separate JLabels, one for each line.
As for setting your JLabel's size, do you mean the font size (if so, use html or the setFont() method if you aren't using html) or the size of the colored box around your text (if so, use a layout)?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
For the number at the start, you're appending the value of the array at index i. I'm not sure how your algorithm works, but you need to make sure you're filling the array correctly. For the number at the end, you shouldn't just use a hard-coded "1" in your String. Again, you're looking at the value of the array at index i. It's up to you how that value and that index map to your output.
As for the multiple lines, I've given you several suggestions. Either use html (not sure if it works in AWT) or split the lines up into multiple JLabels.
--- Update ---
This thread has been cross posted here:http://www.java-forums.org/awt-swing/85701-help-my-java-applet-displaying-output-text.html
Although cross posting is allowed, for everyone's benefit, please read:
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Why were all the posts deleted?
Here's some of it:
Yeah sorry about that this is my first post. The algorithm works fine i just need to display the output as
There are:
2 words of length 1
1 word of length 2
1 word of length 3
whereas mine just outputs the numbers 2,1,1
I am not sure how to change this. I think I have to use a variable from the array and display it but i am not too sure. The array code isThe result button is currently displaying the 2,1,1 output.String array[]=text.split(" "); int counter=0; for(int i=0;i<array.length;i++) if(counter<array[i].length()) counter=array[i].length(); int intArray[]=new int[counter]; for(int i=0;i<intArray.length;i++){ intArray[i]=0; } for(int i=0;i<array.length;i++){ intArray[array[i].length()-1]++; } String mystring=""; for(int i=0;i<intArray.length;i++){ if(intArray[i]>0) { mystring+=String.valueOf(intArray[i]); mystring+=", "; } } result.setText(mystring); // set label here to display text
Thanks
If you don't understand my answer, don't ignore it, ask a question.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!