Printing numbers from a for loop horizontally instead of vertically
<br>
tag creates a new line, just replace it with a single space " "
:
<script>
for (counter=1; counter <=200; counter++) {
document.write(counter + " ")
}
</script>
Tables are displayed horizontally instead of vertical
If you want your display to be vertical then you have to put each piece of information into a table row <tr><td>...</td></tr>
while ($ausgabespiele = mysql_fetch_array($spiele)) {
if ( $ausgabespiele['sieger'] == $ausgabespiele['team1'] ) {
echo "<tr><td><b>". strtoupper($ausgabespiele['sieger']) . "</b></td></tr>";
}
else {
echo "<tr><td>". strtoupper($ausgabespiele['sieger']) . "</td></tr>";
}
}
python: printing horizontally rather than current default printing
In Python2:
data = [3, 4]
for x in data:
print x, # notice the comma at the end of the line
or in Python3:
for x in data:
print(x, end=' ')
prints
3 4
Display a second output horizontally but first output vertically
You need to compute the dimensions first so you can format the columns properly: maxwidths
. Noting the maximum number of rows helps for controlling the printing part.
int maxrow = 0;
List<Integer> maxwidths = new ArrayList<>();
for (int i=0; i<=storage.length-1; i++){
int maxwidth = 0;
for (String[] inner : storage[i]){
int width = String.join( " ", inner ).length();
if( width > maxwidth ) maxwidth = width;
}
if( storage[i].length > maxrow ) maxrow = storage[i].length;
maxwidths.add( maxwidth );
}
for (int i=0; i<=storage.length-1; i++){
System.out.printf( "%-" + maxwidths.get(i) + "d ", i );
}
System.out.println();
for( int row = 0; row < maxrow; ++row ){
for (int i=0; i<=storage.length-1; i++){
String normal;
if( row < storage[i].length ){
normal = String.join( " ", storage[i][row] );
} else {
normal = "";
}
System.out.printf( "%-" + maxwidths.get(i) + "s ", normal );
}
System.out.println();
}
Print loop output horizontally?
You are using System.out.println("X");
This automatically appends a newline character to the end of the string.
Instead use System.out.print("X");
to print the X's next to each other.
Restricting JTextField input to Integers
Do not use a KeyListener for this as you'll miss much including pasting of text. Also a KeyListener is a very low-level construct and as such, should be avoided in Swing applications.
The solution has been described many times on SO: Use a DocumentFilter. There are several examples of this on this site, some written by me.
For example: using-documentfilter-filterbypass
Also for tutorial help, please look at: Implementing a DocumentFilter.
Edit
For instance:
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
public class DocFilter {
public static void main(String[] args) {
JTextField textField = new JTextField(10);
JPanel panel = new JPanel();
panel.add(textField);
PlainDocument doc = (PlainDocument) textField.getDocument();
doc.setDocumentFilter(new MyIntFilter());
JOptionPane.showMessageDialog(null, panel);
}
}
class MyIntFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
} else {
// warn the user and don't allow the insert
}
}
private boolean test(String text) {
try {
Integer.parseInt(text);
return true;
} catch (NumberFormatException e) {
return false;
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
} else {
// warn the user and don't allow the insert
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
if (test(sb.toString())) {
super.remove(fb, offset, length);
} else {
// warn the user and don't allow the insert
}
}
}
Why is this important?
- What if the user uses copy and paste to insert data into the text component? A KeyListener can miss this?
- You appear to be desiring to check that the data can represent an int. What if they enter numeric data that doesn't fit?
- What if you want to allow the user to later enter double data? In scientific notation?
Related Topics
How to Check If an Integer Is in a Given Range
How to Sort by Two Fields in Java and Specify Sort Direction
How to Stop the Items Duplication in Recyclerview Android
Reading Data from Nested Json Object Using Java
Error: Could Not Find or Load Main Class in Intellij Ide
How to Use a Regex to Search Backwards Effectively
Multiple Queries Executed in Java in Single Statement
How to Return a Custom Object from a Spring Data JPA Group by Query
Replacing Double Backslashes With Single Backslash
Cannot Find Element Using Selenium Webdriver
Handling the Null Value from a Resultset
Dynamic Placeholder Substitution in Properties in Java
Opening Intent Crashing the App Android Studio
How to Query Dynamodb Based on Partition Key and Sort Key [Java]