I have this array that shows the user the indexes of the position on a board of a game I'm making, the board is hexagonal and the notations bellow aren't finished yet because I'm to lazy to finish them right now :), but I'm wondering how can I print it on the console while keeping the format.
Thanks in advance
public static final String[] NOTATION = {
" 0/4 0/6 0/8 0/10 0/12 ",
" 1/3 1/5 1/7 1/9 1/11 1/13 ",
" 2/2 2/4 2/6 2/8 2/10 2/12 2/14 ",
" F + + + + + + + + ",
" E + + + + + + + + + ",
" D + + + + + + + + 9",
" C + + + + + + + 8 ",
" B + + + + + + 7 ",
" A + + + + + 6 ",
" 1 2 3 4 5 "
};
I recommend making a method you can call that prints it so you can print it easily many times, for example printBoard:
public static void printBoard() {
for (String str : NOTATION) {
System.out.println(str);
}
}
This utilizes an enhanced for loop to iterate through the array, and print each String moving to the next line with println.
Use it with:
public static void main(String[] args) {
printBoard();
}
Output:
0/4 0/6 0/8 0/10 0/12
1/3 1/5 1/7 1/9 1/11 1/13
2/2 2/4 2/6 2/8 2/10 2/12 2/14
F + + + + + + + +
E + + + + + + + + +
D + + + + + + + + 9
C + + + + + + + 8
B + + + + + + 7
A + + + + + 6
1 2 3 4 5
Note: I recommend reworking this into not using static and instead using OOP in something like a Board class.
Related
I am using the PDFBox to extract the character coordinates from the read PDF. However, I can't identify the unit of measurement of the value returned by the getXDirAdj () and getYDirAdj () methods?
#Override
protected void processTextPosition(TextPosition text) {
String tChar = text.getCharacter();
System.out.println("String[" + text.getXDirAdj() + ","
+ text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale="
+ text.getXScale() + " height=" + text.getHeightDir() + " space="
+ text.getWidthOfSpace() + " width="
+ text.getWidthDirAdj() + "]" + text.getCharacter());
}
1 unit = 1/72 inch
"how to obtain the rotation of the character read": from the ExtractText.java tool:
static int getAngle(TextPosition text)
{
Matrix m = text.getTextMatrix().clone();
m.concatenate(text.getFont().getFontMatrix());
return (int) Math.round(Math.toDegrees(Math.atan2(m.getShearY(), m.getScaleY())));
}
how to use NOT operator for integers in JAVA
when i put NOT operator (!) it shows an error
package com.learnJava.first;
public class LogicalOpTable {
public static void main(String[] args) {
int p,q;
System.out.println("P\t Q\t AND\t OR\t XOR\t NOT\n");
p = 1;
q = 1;
System.out.println(p+ "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p );
p = 1;
q = 0;
System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p);
p = 0;
q = 1;
System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p);
p = 0;
q = 0;
System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p);
}
}
You would need to use the bitwise complement operator, ~, not the logical complement operator, !.
However, you seem to have a bit of a mismatch in your code: your class is called LogicalOpTable, but you are otherwise using bitwise operators, not logical operators.
If you really want to do logical operations, using boolean values instead of ints.
If you really want to do bitwise operations, name your class so it's not as confusing ;)
one year later, but hey, maybe it's still relevant.
I had the same issue with this exercise, but I went a different way. If I'm not mistaken, p and q were initially of the boolean type and the task was to modify the table to show 1s and 0s instead of true and false.
What I did was convert each expression as a whole from boolean to int (e.g. int a = (!p) ? 1 : 0;) and replace them with the variable for the rest of the code (e.g System.out.println(..... + a); )
I am making my program to throw a die (as in dices) for a school assignment in Java SE. The user can place a character as standard input, so the character the user picks will represent the eyes of the die. Sometimes when I print the result, it shows a completely different character.
package ThrowADie;
import java.util.Scanner;
public class ThrowADie {
public static void main(String[] args) {
//Ask user for the char in which the dices eyes should be printed in.
System.out.print("Which character should I use for the eye: ");
//Allow user to place input in the eye variable
Scanner input = new Scanner(System.in); //Make the stdinput object
char eye = input.next().charAt(0);
//Time to throw the die. Place result in dieResult
int dieResult = throwDie();
//Reveal of the result
printDieResult(dieResult, eye);
}
/*
* Method name: throwDie()
* Purpose: Picks a number from 1 to 6 randomly, like a die does
* Parameters: N/A
* Returns: Integer number from 1 to 6
*/
public static int throwDie(){
int range = (6 - 1) + 1;
return (int)(Math.random() * range) + 1;
}
/*
* Method name: printDieResult()
* Purpose: Generate result of the die throw in ASCII art
* Parameters: numberOfEyes, typeOfEyes
* Returns: N/A
*/
public static void printDieResult(int numberOfEyes, char typeOfEyes){
if (numberOfEyes == 1){
//Print art
System.out.println(
" " + " " + " \n"
+ " " + typeOfEyes + " \n"
+ " " + " " + " ");
} else if (numberOfEyes == 2){
//Print art
System.out.println(
typeOfEyes + " " + " \n"
+ " " + " " + " \n"
+ " " + " " + typeOfEyes);
} else if (numberOfEyes == 3){
//Print art
System.out.println(
typeOfEyes + " " + " \n"
+ " " + typeOfEyes + " \n"
+ " " + " " + typeOfEyes);
} else if (numberOfEyes == 4){
//Print art
System.out.println(
typeOfEyes + " " + typeOfEyes + "\n"
+ " " + " " + " \n"
+ typeOfEyes + " " + typeOfEyes);
} else if (numberOfEyes == 5){
//Print art
System.out.println(
typeOfEyes + " " + typeOfEyes + "\n"
+ " " + typeOfEyes + " \n"
+ typeOfEyes + " " + typeOfEyes);
} else {
//Print art
//Accidentally written down 9 eye representation
System.out.println(
typeOfEyes + typeOfEyes + typeOfEyes + "\n"
+ typeOfEyes + typeOfEyes + typeOfEyes + "\n"
+ typeOfEyes + typeOfEyes + typeOfEyes);
}
}
}
Output
This program will generate proper results. But occasionally the char that has been input, that represent the eye of the die, transforms in to a number.
In the case below, the program should print 9 '#' characters. Instead it prints 192 on the first row. (I know dices have 6 eyes but I bumped into this strange output while accidentally printing 9 eyes)
run:
Which character should I use for the eyes: #
192
###
###
I can not find the cause of this, can anyone see what I have done wrong here?
Character arithmetic!
Consult this table. # is character 64
typeOfEyes + typeOfEyes + typeOfEyes + "\n"
This first line is actually adding up the values of the characters (64 + 64 + 64) = 192, then addending that with a newline, so we get 192\n.
The compiler is choosing to add those up rather than create a String of characters. The easy way to solve this is to preface that with an empty string in front: "" + typeOfEyes...
Basically, the compiler is "dumb." When we add integers to Strings, "foo" + 123 the compiler can interpret that as foo123 just fine because it recognizes the first element as a String. However, we've defined a char which is a numeric type representing a character. So the compiler does math with it. Even though we shouldn't be. Adding the String literal tells it we actually want text.
int test = (int) (typeOfEyes + typeOfEyes + typeOfEyes);
System.out.println("\n" + test + "\n"
+ typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
+ typeOfEyes + "" + typeOfEyes + "" + typeOfEyes + "\n"
+ typeOfEyes + "" + typeOfEyes + "" + typeOfEyes);
Which character should I use for the eye: #
192
###
###
###
My assignment calls for the line number to be display with the output. The professor suggested I do it with a counter and as seeing Java doesn't have an easy way to print out the current line number, I just created a counter as suggested. The below code is as follows:
//Count Increment
for (count = 1; count<= 5; count++)
{
}
//Display information
System.out.println(count + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
System.out.println(count + "." + " " + "Total Rooms:"+ " " + numofRooms);
System.out.println(count + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
System.out.println(count + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
System.out.println(count + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyvalue);
However, the output starts the line counter at six as demonstrated here:
6. Street: park avenue #44
6. Total Rooms: 5
6. Total Area: 2500.0 sq.ft
6. The price per Sq. Ft is $120.4
6. The estimated property value is $301000.0
Removing the brackets doesn't help either. How can I get the line count to correctly state 1,2,3,4,5?
Please ask for clarification if needed!! Thanks.
Your prints are outside of the for loop. Your for loop ends when the counter is "6" which is when it exits the for loop. This variable doesn't change so the current value is "6",that is why it always prints "6" below on your code. If you want to print the line number for each instruction you could do something like this:
count = 0;
System.out.println(++count + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
"++count", you increment the variable the moment you write a line, in the first case it should print 1 then 2 etc. Hope this helped :)
The loop is not required cause you are only counting the lines one time each. If you put those lines in a loop that goes from 0 to 5 you will be counting each line 5 times. Since you only need to count each line ONE time you dont need the loop and just the simple increment I previously mentioned. Hope this clears out why the loop is not required
I assume that you have somewhere above this a line defining count:
int count;
So after the for loop, you've incremented count to 6 and then started printing with count left at the last incremented value from the for loop.
So, remove the for loop and just pre-increment the count variable for each line of ouput.
int count = 0;
//Display information
System.out.println( (++count) + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
...
class Print{
static int lineno = 0;
private int static getLineNo(){
lineno = lineno + 1;
return lineno;
}
}
//Display information
System.out.println(Print.getLineNo() + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
System.out.println(Print.getLineNo() + "." + " " + "Total Rooms:"+ " " + numofRooms);
System.out.println(Print.getLineNo() + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
System.out.println(Print.getLineNo() + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
System.out.println(Print.getLineNo() + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyv
I'm looking for a Java built-in data structure that would be the best at handling adjacent rooms.
I have a grid/floor divided into randomly generated rooms like so:
+ + + + + + + + + + + + + + + + + + + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + + + + + + + + + + + + + + + + + + +
and i'm looking for a data structure in which it would be fastest/easiest to store this grid and map out what rooms neighbour what rooms.
Does anyone have a suggestion?
thanks
You just need to store:
the opposite corners of each room
the adjacency graph/matrix of the graph formed by rooms as nodes and adjacency as the edge.
You can use a graph to represent rooms as nodes and neighboring relationship as edges.
You can represent graphs in many different ways. In this case, since the relationship is sparse, it's better to use adjacency list instead of adjacency matrix.
In Java, the graph can be represented with Map<Room,List<Room>>. Basically, it is what it says: it's a map from a Room to a list of its neighboring Rooms.
Alternatively, if you prefer to work with basic integers and arrays, you can use an adjacency matrix representation boolean[][] adj, where adj[i][j] == true if and only if room i and room j are neighbors.