Hi I'm petty new to java and I have a question,
I'm trying to replace the String " 22(S)" with " 22(I)" but for some reason the replace first doesn't replace the String.
Here is my code:
public class tes {
public static void main(String[] args) {
int y = 22;
String x = " 22(S)";
x = x.replaceFirst(" " + y + "(S)", " " + y + "(I)");
System.out.println(x);
}
}
While I know that I can do x.replaceFirst("S","I") , I want to understand why this is still producing 22(S) with my current code. Thanks.
Because ( and ) are grouping operators in regular expression. You need to escape them in the match term. Like,
x = x.replaceFirst(" " + y + "\\(S\\)", " " + y + "(I)");
And I get
22(I)
with no other changes.
public class Replace {
public static void main(String[] args) {
int y = 22;
String x = " 22(S)";
x = x.replaceAll(" " + y + "\\(S\\)", " " + y + "(I)");
System.out.println(x);
}
}
By using replaceAll also we can replace all the S letters to I
Related
So for an assignment I have to create an application whose main() method holds two variables. After declaring the variables and assigning an integer to each of them, I have to run both through the same 3 methods. I was thinking that I have to create a class for the variables, but honestly have no idea where to begin. So far, I have figured out how to run one of the integers through the methods, but I can't get both to pass through the same methods.
Here is my work so far:
public class ArithmeticMethods{
public class integer
{
int firstInteger = 10;
int secondInteger = 20;
}
public static void main(String[] args) {
displayNumberPlus10();
displayNumberPlus100();
displayNumberPlus1000();
System.out.println(firstInteger + " +" + " 10" + " is " + displayNumberPlus10());
System.out.println(firstInteger + " +" + " 100" + " is " + displayNumberPlus100());
System.out.println(firstInteger + " +" + " 1000" + " is " + displayNumberPlus1000());
}
public static int displayNumberPlus10() {
int numberPlus10;
numberPlus10 = (firstInteger + 10);
return numberPlus10;
}
public static int displayNumberPlus100() {
int numberPlus100;
numberPlus100 = (firstInteger + 100);
return numberPlus100;
}
public static int displayNumberPlus1000() {
int numberPlus1000;
numberPlus1000 = (firstInteger + 1000);
return numberPlus1000;
}
}
Right now the methods are set to only run the first variable and with my ATTEMPT at creating a class, the program doesn't work at all. Any advice would be greatly appreciated.
Also I apologize if the code looks ugly. I am very new to this.
You need to add parameters to your methods. The result should look something like this:
public static int displayNumberPlus10(int input) {
return (input + 10);
}
...
And can be called like this:
int first = 10;
int second = 20;
displayNumberPlus10(first);
displayNumberPlus10(second);
I would like to concatenate a few variables into a string variable but I am unable to get it to work. When I compile it says "not a statement" and "; expected."
float a = 1;
float b = 2;
String resW;
My purpose is to concatenate "a" and "b" and assign it to resW.
resW = a " + " b;
My ultimate goal is to use resW as such...
System.out.println(resW);
bufferedWriter.write(resW);
It should save to a file in the format of "1 + 2". I don't understand how to do this properly or if this is even possible.
String resW = a + " + " + b;
try this..
resW = a + " + " + b;
Use a plus sign to concatenate Strings.
It should allow an autoconversion from float to String, but if it doesn't, you can change the floats to Floats, and do:
resW = a.toString() + " + " + b.toString();
Instead of using resW, you could try this:
public class QuickTester {
public static void main(String[] args) {
float a = 1;
float b = 2;
System.out.println(String.format("%.0f + %.0f", a, b));
System.out.println(String.format("%.2f + %.2f", a, b));
System.out.println(String.format("%.5f + %.5f", a, b));
}
}
Output:
1 + 2
1.00 + 2.00
1.00000 + 2.00000
Note:
If you insist, you could do something like String resW = String.format(...);
String#format can help you 'beautify' your resulting string, allowing you to specify the number of decimal places, alignment, etc
Unfortunately, I'm having some difficulty replacing new lines.
public class Example {
static String s = "73167176531330624919225119674426574742355349194934\n" +
"96983520312774506326239578318016984801869478851843\n" +
"85861560789112949495459501737958331952853208805511\n" +
"12540698747158523863050715693290963295227443043557\n" +
"66896648950445244523161731856403098711121722383113\n" +
"62229893423380308135336276614282806444486645238749\n" +
"30358907296290491560440772390713810515859307960866\n" +
"70172427121883998797908792274921901699720888093776\n" +
"65727333001053367881220235421809751254540594752243\n" +
"52584907711670556013604839586446706324415722155397\n" +
"53697817977846174064955149290862569321978468622482\n" +
"83972241375657056057490261407972968652414535100474\n" +
"82166370484403199890008895243450658541227588666881\n" +
"16427171479924442928230863465674813919123162824586\n" +
"17866458359124566529476545682848912883142607690042\n" +
"24219022671055626321111109370544217506941658960408\n" +
"07198403850962455444362981230987879927244284909188\n" +
"84580156166097919133875499200524063689912560717606\n" +
"05886116467109405077541002256983155200055935729725\n" +
"71636269561882670428252483600823257530420752963450".replaceAll("\\n", "");
public static void main(String[] args) {
System.out.println(s);
}
}
I would like to remove all occurrences of \n from String s. I tried looking up the answer on StackOverflow, but every post seems to have a different answer (and they are all complicated and hard to remember).
Is there a simple way of doing this?
I have also tried
replaceAll("\n", "")
replaceAll("\\\\n", "")
replaceAll(System.getProperty("line.separator"), "")
replaceAll("(\\r|\\n)", "")
"hello" + "world".replace("o","")
is same as
"hello" + ("world".replaceAll("o",""))
So replaceAll will affect only last part of your string, which in your case is
"71636269561882670428252483600823257530420752963450".replaceAll("\\n", "").
To solve this problem use parenthesis to first concatenate all string parts, and then call replace on resulting string.
static String s = (
"73167176531330624919225119674426574742355349194934\n" +
"96983520312774506326239578318016984801869478851843\n" +
"85861560789112949495459501737958331952853208805511\n" +
"12540698747158523863050715693290963295227443043557\n" +
"66896648950445244523161731856403098711121722383113\n" +
"62229893423380308135336276614282806444486645238749\n" +
"30358907296290491560440772390713810515859307960866\n" +
"70172427121883998797908792274921901699720888093776\n" +
"65727333001053367881220235421809751254540594752243\n" +
"52584907711670556013604839586446706324415722155397\n" +
"53697817977846174064955149290862569321978468622482\n" +
"83972241375657056057490261407972968652414535100474\n" +
"82166370484403199890008895243450658541227588666881\n" +
"16427171479924442928230863465674813919123162824586\n" +
"17866458359124566529476545682848912883142607690042\n" +
"24219022671055626321111109370544217506941658960408\n" +
"07198403850962455444362981230987879927244284909188\n" +
"84580156166097919133875499200524063689912560717606\n" +
"05886116467109405077541002256983155200055935729725\n" +
"71636269561882670428252483600823257530420752963450"
).replaceAll("\\n", "");
But in situation where we are not dealing with initializing field, I would prefer splitting this "one-liner" into more steps which will be more readable and you will avoid errors like one from your question:
//concatenation
String s = "....."
+"....."
:
+".....";
//modification
s = s.replaceAll("\\n","");
First of all, when you are doing
static String s = "73167176531330624919225119674426574742355349194934\n" +
"96983520312774506326239578318016984801869478851843\n" +
"85861560789112949495459501737958331952853208805511\n" +
"12540698747158523863050715693290963295227443043557\n" +
"66896648950445244523161731856403098711121722383113\n" +
"62229893423380308135336276614282806444486645238749\n" +
"30358907296290491560440772390713810515859307960866\n" +
"70172427121883998797908792274921901699720888093776\n" +
"65727333001053367881220235421809751254540594752243\n" +
"52584907711670556013604839586446706324415722155397\n" +
"53697817977846174064955149290862569321978468622482\n" +
"83972241375657056057490261407972968652414535100474\n" +
"82166370484403199890008895243450658541227588666881\n" +
"16427171479924442928230863465674813919123162824586\n" +
"17866458359124566529476545682848912883142607690042\n" +
"24219022671055626321111109370544217506941658960408\n" +
"07198403850962455444362981230987879927244284909188\n" +
"84580156166097919133875499200524063689912560717606\n" +
"05886116467109405077541002256983155200055935729725\n" +
"71636269561882670428252483600823257530420752963450".replaceAll("\\n", "");
you are only replacing the last concatenation of that string (Note: which has no "\n").
what you need to do is get rid of that replaceAll in your string declaration, and in your main method do s = s.replaceAll("\n", ""); like the following:
public static void main(String[] args) {
s = s.replaceAll("\n", "");
System.out.println(s);
}
Tested and works :D
Hope this was helpful :D
You could split and join the string before printing it like this:
public static void main(String[] args) {
System.out.println(String.join("", s.split("\n")));
}
// This application displays some math facts
public class DebugThree2 {
public static void main(String args[]) {
add();
subtract();
System.out.println("Java");
int a = 2, b = 5, c = 10;
add("a" + "b");
add("b" + "c");
subtract("c" - "a");
}
public static void add() {
System.out.println("The sum of " + "a" + "and" + "b" + "is" + "a" + "b");
}
public static void subtract() {
System.out.println("The difference between " + "a" + "and" + "b" + "is" + "a" - "b");
}
}
I keep getting the errors method add in class DebugThree2 cannot be applied to given types, bad operand types for binary operator '-'
public static void main(String args[])
{
add();
subtract();
System.out.println("Java");
int a = 2, b = 5, c = 10;
add(a,b);
add(b,c);
subtract(c,a);
}
public static void add(int a, int b)
{
System.out.println("The sum of " + a +
" and " + b + " is " + (a+b));
}
public static void subtract(int a, int b)
{
System.out.println("The difference between " +
a + " and " + b + " is " + (a-b));
}
I think something like that may be more what you are looking for. You are trying to subtract (string)b from (string)a.
"a" - "b"
You must use their actual value, by not including quotation marks.
The output of my program needs to be the value of an int printed inside square brackets but i can't work out how to type this so that it will compile!?
If I am understanding the question.
Say you have an int
int number = 0;
and you have a print statement.
System.out.println("This is the number : [" + number + "]");
That is how you do it.
just append the "[" + number + "]"
public static void main(String[] args) {
int a = 10;
System.out.println("[" + a + "]");
}
Output
[10]
As said:
System.out.println("[" + a + "]");
The + signs means "and". print string AND int AND string.