How to extract integer value in sentence in java [closed] - java

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 to extract integer values in string.
For example
this is the java 1234 and 7899/45767 program
in that I need to extract only integer like
1234
7899
45767

I think a regex could fit your need:
import java.util.regex.*;
String aParser="123 bla bla 1234 bla bla 0324";
Pattern p=Pattern.compile("(\d+)");
Matcher m=p.matcher(aParser);
while(m.find())
{
//your code here
}

try this
Matcher m = Pattern.compile("-?\\d+").matcher(str);
while(m.find()) {
System.out.println(m.group());
}

You can try Scanner.nextInt() method
Scanner sc = new Scanner("this is the java 1234 and 7899/45767 program, in that i need to extract only integer like 1234 7899 45767");
while(sc.hasNext()) {
boolean scanned = false;
if(sc.hasNextInt()) {
System.out.println(sc.nextInt());
scanned = true;
}
if(!scanned)
sc.next();
}

Related

How to increase the value by +1 in the string(java) and the string is mentioned below [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
"Sales Docket successfully saved and sent for approval. Please note your document number. JBHL/39/16-17"
i want only the number 39 in the string should be increased by +1 when we run the method
Use regular expression to find the number, then build new string:
private static String increment(String input) {
Matcher m = Pattern.compile("/(\\d+)/").matcher(input);
if (! m.find())
throw new IllegalArgumentException("Invalid document number: " + input);
int newNumber = Integer.parseInt(m.group(1)) + 1;
return input.substring(0, m.start(1)) + newNumber + input.substring(m.end(1));
}
Test
System.out.println(increment("JBHL/39/16-17"));
Output
JBHL/40/16-17

How can I set a loop to pick words in a sentence? [closed]

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
Objective: To create a function which takes a single String argument. The string is then evaluated and words in the String are reversed.
The output desired:
Ex: Hello World!
-- olleH !dlroW
*Question:* is how do I set up a loop so that I can separate the words from the sentence?
I am using a scanner to get the input from the user in the form of a String object.
Separating words is easy: see http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
String[] words = sentence.split(" ");
would result in words containing each word delimited by the spaces.
String source = "Hello World";
for (String part : source.split(" ")) {
System.out.print(new StringBuilder(part).reverse().toString());
System.out.print(" ");
}
Output:
olleH dlroW
Notes: Commenters have correctly pointed out a few things that I thought I should mention here. This example will append an extra space to the end of the result. It also assumes your words are separated by a single space each and your sentence contains no punctuation.
Credits: #William Brendel
String str = new Scanner(System.in).nextLine();
Stack<String> stack = new Stack<String>();
for (String s : str.split(" ")) {
stack.push(new StringBuilder(s).toString());
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
This specifically pushes it onto the stack and then pops it to reverse it.
I took some of your code E. Doroskevic, sorry!

Is there something like a while switch? [closed]

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 was wondering if there is something that takes in a user input and tests it into preset 'cases' (like a switch) and if there is no 'cases' matching the user input the switch-thing resets (like a while statement). Then it prompts the user for an input and then tests if that matches and if it doesn't it keeps doing this until the input from the user matches one of the cases. I realize that you can do this with a while/if/else combo and am simply wandering if there is a way to do this with a while statement.
Edit:
What I ended up doing is...
String aString = scanner.next();
boolean switchOff = false;
while ( switchOff = false )
{
switch (aString)
{
case "example" : //What I want to happen
switchOff=true;
break;
default: aString = scanner.next();
break;
}
}
Would this work?
You can combine them with
OUTER: while(true) switch(tested) {
case GOOD:
// something
break;
case ALSO_GOOD:
// something
break;
default:
break OUTER;
}
do{ input = askInput(); } while( !match(input) );

Parse a String to obtain id numbers [closed]

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
My input String is as below,
{"string name"=hi;"id"=44401234;"string name"=hey;"id"=87695432.....}
I want only the ids as output like, {44401234 87695432}
for(int i=0;i<input.length();i++)
{
int index=input.indexOf("id");
hh=input.substring(index+4,index+12);
System.out.println(hh);
}
The below code uses Regex to get the value associated with id
try
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String input = "{\"string name\"=hi;\"id\"=44401234;\"string name\"=hey;\"id\"=87695432.....}";
Pattern pattern = Pattern.compile("\"id\"=[0-9]{8}");
Matcher m = pattern.matcher(input);
while (m.find()) {
String str = m.group();
str = str.replace("\"id\"=", "");
System.out.println(str);
}
}
}

Put each character in a string into its own string - java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
I have a string and I want to have that string get split up so that each individual character is in its own string. The string will vary in length as it is user inputted. Thanks in advance
If you actually want an array of strings from a string you can try this
String[] chars = myString.split("");
String str = /*Your String here*/;
char[] charArray = str.toCharArray();
String[] strArray = new String[charArray.length];
String strChars = "";
for (Character c : charArray){
int i=0;
strChars = c.toString();
strArray[i] = strChars;
System.out.println(strChars);
i++;
}
System.out.println(strArray.length);

Categories

Resources