I am making a a mini game plugin, i need to replace every blocks that not a type, so i maked this :
public static void replaceBlock(String x, String y, String z, String x2, String y2, String z2, String block, String secondBlockType, String mapDict) {
String[] BlocksToReplace = mapDict.split("\\*");
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + x + " " + y + " " + z + " " + x2 + " " + y2 + " " + z2);
ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
String pos1 = "/pos1 " + x + "," + y + "," + z;
String pos2 = "/pos2 " + x2 + "," + y2 + "," + z2;
Bukkit.dispatchCommand(console, pos1);
Bukkit.dispatchCommand(console, pos2);
tasak = Bukkit.getServer().getScheduler().runTaskTimer(Main.plugin, new Runnable() {
private int count = 2;
#Override
public void run() {
if(count == 0) {
for(String b : BlocksToReplace) {
if(b == block) {
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " fdbshjfbdjshq ");
}else {
String fillcmd = "/replace " + b + " " + secondBlockType;
System.out.println(ConsoleColorUtils.PURPLE + "[" + Main.pName +"]" + ConsoleColorUtils.RESET + " " + fillcmd + " " + block);
Bukkit.dispatchCommand(console, fillcmd);
}
}
tasak.cancel();
} else {
count--;
}
}
}, 20, 20);
}
Map dict is => gold_block*wool:15*stained_hardened_clay:4*wool:4*stained_hardened_clay:3*stained_hardened_clay:11*wool:9*wool:11*stained_hardened_clay:9*wool:10*wool:2*stained_hardened_clay:10*stained_hardened_clay:2*stained_hardened_clay:6*diamond_block*prismarine:2*prismarine:1*melon_block*wool:5*slime*emerald_block*quartz_block*stained_hardened_clay*sandstone:2*nether_brick*wool:14*wool:13
block var is : stained_hardened_clay:14
When i call the function, it replace every blocks not all exept the block i want
Any way to fix it ?
Dont use "==" for strings. use b.equals(block). Because "==" compares the hash values of the String objects. And the equals Method compares the string itself.
More Info: https://www.java67.com/2012/11/difference-between-operator-and-equals-method-in.html
The line with the problem is. This line should check if the 3rd index contains character and it should not contain the words north and america.
else if ( salida[3].matches("[a-zA-Z]+") && !salida[3].equals("North") ) { // not working correctly
if ( !salida[3].equals("America")) {
salida1 = salida1 + salida[0] + " " + salida[1] + " " + salida[2] + " " + salida[3] + ",";
The code above should run for the 4th line of the array data below
[United, States, 1,527,664, 90,978, North, America]
[Canada, 77,002, 5,782, North, America]
[Turks, and, Caicos, 12, 1, North, America]
[St., Vincent, &, Grenadines, 17, 0, North, America]
this is the string output I'm currently getting which doesn't add the 3rd index of the array to the string
United States,Canada ,Mexico ,Dominican Republic,Panama ,Honduras ,Guatemala ,Cuba ,El Salvador,Costa Rica,Jamaica ,Haiti ,Martinique ,Guadeloupe ,Bermuda ,Trinidad and,Aruba ,Bahamas ,Cayman Islands,Barbados ,Sint Maarten,Saint Martin,Nicaragua ,Antigua and,Grenada ,Belize ,Saint Lucia,St. Vincent,CuraƧao ,Dominica ,Saint Kitts,Turks and,Montserrat ,Greenland ,British Virgin,Saint Barthelemy,Caribbean Netherlands,Anguilla ,Saint Pierre,
Input country to display data:
This is my entire code
public String setCountriesList() {
String salida1 = "";
try {
Document doc = Jsoup.connect("https://www.worldometers.info/coronavirus/countries-where-coronavirus-has-spread/").get();
Elements tr = doc.select("tr");
String [] na = {"north", "america"};
for (int i = 0; i < tr.size(); i++) {
if (tr.get(i).text().contains("North America")) {
String[] salida = tr.get(i).text().split(" ");
System.out.println(salida[3].contains("North") + " and " + salida[3].contains("America") );
System.out.println(Arrays.deepToString(salida)); //split salida to country, number ,number in array
if ( salida[1].matches("[a-zA-Z]+")) {
salida1 = salida1 + salida[0] + " " + salida[1] + ",";
}
else if ( salida[2].matches("[a-zA-Z]+")) {
salida1 = salida1 + salida[0] + " " + salida[1] + " " + salida[2] + ",";
}
else if ( salida[3].matches("[a-zA-Z]+") && !salida[3].equals("North") ) { // not working correctly
if ( !salida[3].equals("America")) {
salida1 = salida1 + salida[0] + " " + salida[1] + " " + salida[2] + " " + salida[3] + ",";
}}
```
else {
salida1 = salida1 + salida[0] + " ,";
}
}
}
return salida1;
} catch (Exception ex) {
System.out.println("error");
return "error";
}
}
The issue is that the names of the countries contain a comma "," which is your separator character. You need to find a way to have the country name within the same index 0, or at least wrap the country name in quotes "", so that "North" is effectively index 3. In the examples above "North" was only index 3 for Canada.
I have a segment of code that splits a string into tokens and prints them each out on a new line. I am having a hard time writing a code that determines if a word is a reserved word or not. I have to print "Reserved word is: " if the word is a java keyword, otherwise print "Current word is: ". Here is my code so far:
package projectweek3;
/**
*
* Name -
* Email Address -
* Date -
*
*/
public class Week3Project {
final static String program = "/*\n" +
" * To change this license header, choose License Headers in Project Properties.\n" +
" * To change this template file, choose Tools | Templates\n" +
" * and open the template in the editor.\n" +
" */\n" +
"package testapplication2;\n" +
"\n" +
"import java.util.Scanner;\n" +
"\n" +
"/**\n" +
" *\n" +
" * #author james\n" +
" */\n" +
"public class TestApplication2 {\n" +
"\n" +
" /**\n" +
" * #param args the command line arguments\n" +
" */\n" +
" public static void main(String[] args) {\n" +
" Scanner input = new Scanner(System.in);\n" +
" \n" +
" System.out.println(\"Enter integer #1\");\n" +
" int num1 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #2\");\n" +
" int num2 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #3\");\n" +
" int num3 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #4\");\n" +
" int num4 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #5\");\n" +
" int num5 = input.nextInt();\n" +
" \n" +
" //determine the sum\n" +
" int sum = num1 + num2 + num3 + num4 + num5;\n" +
" \n" +
" //this is helpful to make sure your sum is correct\n" +
" System.out.println(\"The sum is: \" + sum);\n" +
" \n" +
" //why doesn't this generate the sum correctly\n" +
" double average = sum / 5;\n" +
" \n" +
" //The average, lets hope its right...\n" +
" System.out.println(\"The average of your numbers is: \" + average);\n" +
" \n" +
" }\n" +
" \n" +
"}\n" +
"";
**public static void main(String[] args)
{
String str = program;
String s = "";
for (int i = 0; i < str.length(); i++) {
s += str.charAt(i) + "";
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n' || (str.charAt(i) == ' ' && str.charAt(i) == '\n')) {
String currentWord = s.toString();
String res = "int";
if (currentWord.equals(res)) {
System.out.println("Reserved word is: [" + currentWord + "]");
}
else {
System.out.println("Current word is: [" + currentWord + "]");
}
s = "";//Clear the string to get it ready to build next token.
}
}**
I would reconsider the way you're looping through the "program."
Instead of going through character by character, use the Java String.split() function.
String program = "int num1 = input.nextInt();\n";
String[] words = program.split("[\\n\\s\\t]");
for (String word : words) {
System.out.println(word);
}
Output:
int
num1
=
input.nextInt();
EDIT:
Since you can't use String.split(), your looping solution looks good. To check if the current word is reserved, try using Set.contains().
Set<String> reserved = new HashSet<>();
reserved.add("int");
// ...
if reserved.contains(word) {
System.out.println("Reserved word is: " + word);
} else {
System.out.println("Current word is: " + word);
}
That is, assuming you're allowed to use Set.
I need the currentStockLevel for another void Method in java, is there any possibility to get it?
I think no, because of void right?
public void receive (int currentStock)
{
String outLine;
if (currentStockLevel > 0)
outLine = productCode;
{
outLine = ". Current Stock: " + currentStockLevel;
outLine += " Current Stock changed from " + currentStockLevel;
currentStockLevel += currentStock;
outLine += " to " + currentStockLevel;
int storeCost = wholeSalePrice * currentStockLevel;
System.out.println (productCode + ":" + " Received " + currentStockLevel + "." + " Store Cost " + "$" + storeCost + "." + " New stock level: " + currentStockLevel);
}
With the given code I was given the directions: Java lets us use Methods/Functions so we can store procedures that we may use more than once, so I would like you to update your code where there is common tasks it can be done inside a method/function. Any idea how to do this?
package fifthAssignment;
public class Arithmetic {
public static void main(String[] args) {
// setting up the variable firstNumber and secondNumber
int length = args.length;
if (length != 3) {
System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
return;
}
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
int addition = firstNumber + secondNumber;
int minus = firstNumber - secondNumber;
int division = firstNumber / secondNumber;
int multiply = firstNumber * secondNumber;
String arithmetic = args[2];
if (arithmetic.equals("+")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + addition);
} else if (arithmetic.equals("-")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + minus);
} else if (arithmetic.equals("/")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + division);
// I could not use "*" operator as it was looking to pull down all
// the files associated with this program instead of
// using it the way I intended to use it. So in this case I changed
// the "*" to "x" so that I can get the solution you
// were looking for.
} else if (arithmetic.equals("x")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + multiply);
}
// following prints out to the console what the length of each argument
// is.
System.out.println(args[0] + " has the length of " + args[0].length());
System.out.println(args[1] + " has the length of " + args[1].length());
if (arithmetic.equals("+")) {
int total = String.valueOf(addition).length();
System.out.println(addition + " has the length of " + total);
}else if (arithmetic.equals("-")) {
int total = String.valueOf(minus).length();
System.out.println(minus + " has the length of " + total);
}else if (arithmetic.equals("/")) {
int total = String.valueOf(division).length();
System.out.println(division + " has the length of " + total);
} else if (arithmetic.equals("x")) {
int total = String.valueOf(multiply).length();
System.out.println(multiply + " has the length of " + total);
}
}
}
I'll provide a singular example, but you should do this on your own.
You have this in your code:
System.out.println(addition + " has the length of " + total);
Instead, you could potentially create a method that would work with two ints:
public void printStatus(int check, int length) {
System.out.println(check + " has the length of " + length);
}
Which would allow you to call
printStatus(addition, total);
This is just a rough example, but you can wrap a "process" of code in a method, and pass the necessary parameters needed to execute the method to it.
package fifthAssignment;
public class Arithmetic {
public static void main(String[] args) {
// setting up the variable firstNumber and secondNumber
int length = args.length;
if (length != 3) {
System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
return;
}
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
int addition = firstNumber + secondNumber;
int minus = firstNumber - secondNumber;
int division = firstNumber / secondNumber;
int multiply = firstNumber * secondNumber;
String arithmetic = args[2];
// following prints out to the console what the length of each argument
// is.
System.out.println(args[0] + " has the length of " + args[0].length());
System.out.println(args[1] + " has the length of " + args[1].length());
performOperation(arithmetic);
}
public void performOperation(String arithmetic) {
if (arithmetic.equals("+")) {
int total = String.valueOf(addition).length();
System.out.println(addition + " has the length of " + total);
} else if (arithmetic.equals("-")) {
int total = String.valueOf(minus).length();
System.out.println(minus + " has the length of " + total);
} else if (arithmetic.equals("/")) {
int total = String.valueOf(division).length();
System.out.println(division + " has the length of " + total);
} else if (arithmetic.equals("x")) {
int total = String.valueOf(multiply).length();
System.out.println(multiply + " has the length of " + total);
}
}
}