Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So I have this particular code:
import java.util.*;
class insertWordInMiddle {
public static void main(String[] args) {
String s = "Python 3.0";
String s_split[] = s.split(" ");
String a = new String(s_split[0]);
String b = new String(s_split[1]);
String c = a + " Tutorial " + b;
System.out.println(c);
}
}
I was practising and I wanted to insert a word between two words (in the form of a string). It works, however I have yet other way of doing this but cannot get why it doesn't work:
import java.util.*;
class insertWordInMiddle {
public static void main(String[] args) {
String s = "Python 3.0";
String s_split[] = s.split(" ");
String final = s_split[0]+" Tutorial "+s_split[1];
System.out.println(final);
}
}
Error:
/home/reeshabh/Desktop/java practice/insertWordInMiddle.java:14:
error: not a statement
String final = s_split[0]+" Tutorial"+s_split[1];
The simple answer is that final can't be the name of a variable. I can go into more detail if needed, but that's pretty much it. Try something else for your variable name.
final is a reserved keyword in Java. You can't name a variable final. Try renaming it to e.g. finalString.
Wikipedia has a list of all the reserved keywords.
final is a keyword. You can't use it as a variable name.
As per the building of strings: you could use StringBuilder, but for generating one string it's not really necessary. However, concatenating strings using the + operator will create copies of all strings involved and make baby cry when used within a loop.
When to use StringBuilder in Java
You was just a bit unlucky - you used word that is reserved for other purposes in Java language. Take a look at this list, final is present here, so you can't name variables with words listed there:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need a java program which reverse a string
for e.g.:-
input:-"hello world"
output:- "world hello"
Split the string via split() method and space delimiter. It will return the array of the words in the string, then reverse the array and concatenate elements via space character. That's all.
Use split(" ") to split the String into words (i.e. breaking on spaces). Then iterate over the resulting array of words and build up the reverse by prepending each word to a StringBuilder.
public static void main(String[] args) {
System.out.println(reverseOrderOfWords("hello world"));
}
public static String reverseOrderOfWords(String s) {
StringBuilder sb = new StringBuilder();
for (String word : s.split(" ")) {
sb.insert(0, word + " ");
}
return sb.toString().trim();
}
Produces:
world hello
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got this string:
"type":"image","originX":"center","originY":"center","left":135,"top":259,"width":270,"height":519,"fill":"rgb(0,0,0)","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"src":"file:///C:/Users/Alvin%20Combrink/Dropbox/Entrepren%C3%B6rskap/Design/Hemsidan/Backgrunder/Labyrint.jpg","filters":[]},
each part is seperated by a comma, i want to be able to extract a few of the numbers into doubles. The ones i want are left, top, scaleX, scaleY and angle. How shall i approch this?
thanks
If you don't want to rely on using JSON parsers (you should, though, if you are using JSON a lot), you could use the split-method on the entire string and split according to , (comma), find the chunks of data that you want, split those according to : and read the data directly from the 2nd slot in the resulting array.
You may need to substring the last " to be able to parse the numbers directly, though.
But like I said, you really do want to use a JSON parser of some kind if you are using JSON more than a few times in your program.
Code example:
String abc = "ABC:123,DEF:456,GHI:789";
String[] chucks = abc.split(",");
String[] oneToThree = chunks[0].split(":");
String nums = oneToThree[1];
System.out.println(nums);
//This will print 123
I know that someone already replied, but I've been doing this, hope that help too:
public class HelloWorld{
public static void main(String []args){
String text ="\"type\":\"image\",\"originX\":\"center\",\"originY\":\"center\",\"left\":135,\"top\":259,\"width\":270,\"height\":519,\"fill\":\"rgb(0,0,0)\",\"overlayFill\":null,\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"src\":\"file:///C:/Users/Alvin%20Combrink/Dropbox/Entrepren%C3%B6rskap/Design/Hemsidan/Backgrunder/Labyrint.jpg\"";
//Just left and scaleX for example
String left = readValue(text, "left");
String scaleX = readValue(text, "scaleX");
System.out.println("left:" + left);
System.out.println("scaleX:" + scaleX);
}
public static String readValue(String text, String key)
{
//search for the init of the value
int start = text.indexOf("\"" + key + "\"");
//search for the end of the value
int end = text.indexOf(",", start + key.length() + 3);
//return the value. these + 3 , is for quotes and ":"
return text.substring(start + key.length() + 3,end);
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string containing this:
D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo.
I want to get just this part:
wt\lifecycle\StateRB
How can I do that?
You can simply spilt whole path to parts and then get the parts you want.
String path = "D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo";
String[] parts = path.split("\\");
parts = Arrays.copyOfRange(parts, parts.length-3, parts.length);
Or you can get throught string using loop (this seems to be better)
int index = 0, i = 0;
Stack<String> al = new Stack<String>();
while((index = path.lastIndexOf()))!=-1 && i < 3) {
al.push((path = path.substring(index)));
i++;
}
String[] parts = (String[])al.toArray(); //if you don't have array elements
// in correct order, you can use
// Collections.reverse with Arrays.asList
// applied on array
You can use string tokeniezer with \ delimiter and fetch only last three string tokens. i hope that above path going to be constant always.
http://www.tutorialspoint.com/java/java_string_substring.htm
Check the above link
example :
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.substring(10) );
System.out.print("Return Value :" );
System.out.println(Str.substring(10, 15) );
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some string type data in my database table like as D-101 and D-102.
I want to get automatically next data in front end (a JSF web application) to send to database which is D-103.
For this I want to get only int data from string.
How can I do this?
final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
String input = D101;
Matcher matcher = lastIntPattern.matcher(input);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
int lastNumberInt = Integer.parseInt(someNumberStr);
System.out.println("Test int output - " + lastNumberInt);
inputList.add(lastNumberInt);
}
then compare max value
int currentBranchCode = 0;
for (Integer CCS : companyArrayList) {
if (currentBranchCode <= CCS) {
currentBranchCode = CCS;
}
}
and then
currentBranchCode =currentBranchCode +1;
String codegenerate="D"+currentBranchCode;
you must try it.
In your managed bean you can use split() function.
String s = "D-101";
String[] arr = s.split("[^\\d]+");
System.out.println(arr[1]); //prints 101
OR
In the xhtml page you can write an EL like this. Note that myBean is the name of your bean and getColumnValue() method returns a value of a column(i.e. "D-101").
#{myBean.columnValue.split('[^\\d]+')[1]}
This can be done in just a couple of lines:
int i = Intger.parseInt(input.replaceAll(".*(?<!\\d)(\\d+)", "$1");
String next = input.replaceAll("(.*)(?<!\\d)\\d+", "$1"+ ++i);