Is there a way to adjust the spacing between new lines when outputting to the console through System.out.println? I'm attempting to print out a square with a basic nested for-loop, but I keep getting a rectangle despite having the right number of characters. This is because the spacing between the characters is different than the spacing between lines. Any ideas?
The standard output stream is just a stream of data; how that data is displayed is up to the application displaying it (e.g. a terminal or your IDE). You'll either have to settle for a rectangle, or find a different method of output than standard out.
Without your current code, it's hard to say exactly whats going on. My first thought is, make sure you're using a character that is as high as it is wide, e.g. *.
Second thought is (and again, I have no code to reference), see if you get the same result using one of the algorithms here: Printing a Square with loops, perhaps there is an issue with the square-printing logic that you overlooked.
Related
So I have been programming a scientific calculator using Java for a school project, and I have become stuck on two things here.
Adding functions such as sin, cos, tan, root(base,n) etc. With this, I am stuck on a way to separate arguments. For example, root(base,n). I can figure out how to read the equation on my own, I am using the Shunting Yard algorithm to convert the equation to RPN. I have been following this pseudo-code algorithm to implement it. I just cant figure out how to allow the user to add both arguments using only the onscreen GUI.
Implementing an advanced display. So by this I mean a display that doesn't just have one line of text, but multiple lines such as degrees/radians mode, the previous equation and answer, and the current equation/answer. If my explanation is bad, this might help illustrate my point:
Currently my display uses a simple JTextField, and I am aware of its limitations for what I want to do, and that I will definitely need to change it. The JTextField was kind of a temporary way to display the output until I got to making the display properly.
My calculator GUI currently looks like this:
It basically has two strings, one is the one to be evaluated, and the other is what is displayed on the screen, with unicodes etc that can't be evaluated in an equation (hence the power two symbol and the x instead of asterisk)
For the first issue, I think the left arrow and right arrows should move between arg1, arg2 etc. But I don't know how I would achieve this.
Any help with either of these issues would be greatly appreciated. I have been trying to solve this for a few hours now...
Thanks.
Given that root(base, n) = base ^ (1 / n), the input logic could be similar. I would suggest that when the user clicks on the n-root button, a left bracket opens and the display (or status bar) indicates they must enter the base. When they close with a right bracket, they then enter n.
For the advanced display I'm not so sure, maybe a JFormattedTextField?
I've been working on a GUI to handle DNA sequences. Most of the molecules will be plasmids, which are circular forms of DNA. I can get a sequence as a string and display it in a JTextPane, but I'm not sure how to handle cases where the user might want to select a section of the sequence that crosses from the end to the beginning of the sequence. One thing I've considered is displaying the sequence twice, so you can select the last part of the first section and the first part of the last section, then overwriting some function so that copying the text will put the correct sequence on the clipboard instead of the string that was actually selected. (I'll have to do that anyway, I'm displaying the forward and reverse strands of the DNA, then a blank line, so that 1 "line" of actual sequence becomes 3 lines of text.
Is there some trick to circular strings that I just don't know about?
Do you know how other programs handle the selection of pieces of circular DNA? You might be able to get some inspiration from other software, like alignment viewers (supporting circular DNA) in this list on Wikipedia: en.wikipedia.org/wiki/List_of_alignment_visualization_software.
I think it would be wise to decide on how you want the GUI to work first, and then start working on your code. Otherwise you risk wasting a lot of time on implementing ideas that you end up not using.
You could even consider offering both a circular and a linear view on the same sequencing data, as this screenshot from the CLC Sequence Viewer shows:
So in JTextArea there is a getLineCount() is there something similar for JTextPane, because I could find anything. Perhaps there is a different way of obtaining that? I want to get the number of currently existing lines.
There is (as you noted) no built-in way to find the number of lines in a JTextPane- this may be (although I see no official reference to support this) because of JTextPane's ability to hold many different text styles in a single area, which would lead to some complications when finding the total number of lines.
You may be able to calculate the number of lines on your own, by keeping track of the text (and its style) that is presented inside of your JTextPane- you can then use other methods (perhaps LineBreakMeasurer (documentation here)) to calculate the number of lines for each piece of text, and then sum up the total.
In my app I have line numbers to the left of an EditText - everything is great so far, the line numbers are perfectly aligned with the lines of the EditText.
The problem is, if the user changes the text size of the EditText the line numbers aren't aligned properly. So I've added code so when the text size of the EditText is changed, the line numbers text size is changed too; but that causes another problem: if the user picks a big text size, the line numbers hog all the space, so there's barely any room to write in the EditText.
The only solution would be to set line spacing. So I eventually came across the setLineSpacing() function, and have tried using it based off tutorials but I don't understand how it works and can't get it aligned with the EditText's lines.
Can someone help me understand how to use it, or suggest a different way of getting each line in the line numbers TextView aligned with the EditText's lines?
Sorry if this is confusing, I'm not very good at explaining things well sigh.
Thanks,
Alex.
I faced a similar situation in the past, in which the EditText's size was limited. It was decided that the max font size that can be chosen should be limited to a value in which the visual appearance does not look ugly. I feel it is useless to plug cases that can occur but don't make sense. It is better to block such cases. Perhaps you can think along these lines.
HTH,
Akshay
Okay, thus may seen kind of odd, but I wanted to get some suggestions from everyone here. I am a beginning Java developer (after 2 years of ASP.NET web development) and I have recently began working on my first Java project - a calculator. I realize that their are tons of calculators out there, but I thought it would be a good beginner project.
Anyway, here is what I need help with. Currently, I am using a Scrolling JTextArea for display (instead of a simple JTextField) that is approximately 5 rows tall. I want the user to be able to scroll through the list to see previous entries and such. The format of the box will be equation on one line and the program will generate the answer on the next and so on.
My real question is, how is the best way to implement this? My fist idea was to read through the JTextArea when equals is pressed, down to the last line and try to search that line for the operator (+, -, etc.) and the operands. Is this the best way to go about this? Although, this would work would work, I think it could get cumbersome and sounds very inefficient. I am open to any suggestions, even possibly replacing the JTextArea is some other component would work better.
Thanks!
There's no need to read through the JTextArea contents - use JTextArea.append() to add to the end. Here are some examples of JTextArea content manipulation:
JTextArea ta = new JTextArea("Initial Text");
// Insert some text at the beginning
int pos = 0;
ta.insert("some text", pos);
// Insert some text after the 5th character
pos = 5;
ta.insert("some text", pos);
// Append some text
ta.append("some text");
// Replace the first 3 characters with some text
int start = 0;
int end = 3;
ta.replaceRange("new text", start, end);
// Delete the first 5 characters
start = 0;
end = 5;
ta.replaceRange(null, start, end);
If you are open to different interfaces, you might want to try something like a JTextField at the top of your view, from which you can receive as input your 'new' inputted equation, and then below it with the same width a JList that would scroll to have all of the previous equations and their results. That would make parsing of the current formula much easier, and you would also have an easy time of keeping your previous formula and their results in a scrollable list, with the easy option of keeping the most recent on top.
your idea is interesting. so you would have a line such as.
2+2
then when pressing calculate would add the line
4
and so on then you could type in another equation.
it could work but as you said it wouldn't be the most efficient implementation... but that's just a tradeoff of getting the desired functionality.
If i were going to implement it the way you discribed (with a JTextArea) I'd use scanner, and scan the value string a line at a time.
if the line has +/- in it then do the calculation and add both the original line and the answer to a string.
the new string is the new value of the text field.
this method would get pretty cumbersom as you would be continually recalculating the users old entries more were added.
I guess if you continually stored the last line of the document, when you run out of lines, calculate the last stored and append the answer, then it wouldn't be so bad.
Here's what I would do:
use a JTextField to enter in the calculations, and a JList to display the old ones and their answers.
You could treat each line as a single operation. That way you could use the String array returned directly by:
String [] operations = textArea.getText().split("\n");
And then you'll know that exactly each one of them as a complete operation ( may be invalid, but that' another story )
Is this what you asked or do I totally misread you?
I think a simpler solution would actually use two components. A TextArea to hold the "history" of what's happened so far, and a textfield where the user inputs new entries.
Thanks to everyone who replied. You all gave me some ideas to think about. I think right now, I am going to go with my original idea of using a single JTextArea and try to find ways to optimize the process. If that gets too difficult (which is very possible), I will follow the majority's advice and use two separate fields. Thanks for replying everyone!