Manipulating a String: Solving the 6G2N42B Puzzle

  • Context: Java 
  • Thread starter Thread starter NiaSphinx
  • Start date Start date
  • Tags Tags
    Puzzle String
Click For Summary

Discussion Overview

The discussion revolves around manipulating a string input from the user, specifically focusing on extracting integers and characters from a mixed string format, exemplified by the string "6G2N42B". The scope includes programming techniques and methods for string parsing.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant seeks guidance on how to separate integers and characters from a string input.
  • Another participant suggests using the Scanner class in Java for parsing strings, providing a code example that demonstrates its use.
  • A different approach is presented using regular expressions to identify and extract groups of digits and non-digits from the string, with a code snippet illustrating this method.
  • The output of the regex method is shared, showing the successful extraction of integers '6', '2', '42' and characters 'G', 'N', 'B'.

Areas of Agreement / Disagreement

Participants appear to agree on the need for methods to extract integers and characters from the string, but there are multiple approaches suggested without a consensus on which is superior.

Contextual Notes

Some methods rely on specific programming constructs and may depend on the input format. The effectiveness of each approach may vary based on the context of use.

Who May Find This Useful

Individuals interested in string manipulation in programming, particularly in Java, may find the discussion relevant.

NiaSphinx
Messages
4
Reaction score
0
Hi guys,

I was wondering if anyone could explain to me/point me in the right direction on how to manipulate a String read in from the user in a particular way. I'm trying to read in a String, and then I want to sort of split the string up into ints and chars and associate them to variables.
For instance:
If I have a string: 6G2N42B, is there a way to access the ints '6', '2', '42' and a way to access the chars 'G', 'N' and 'B' ?

Thanks
 
Technology news on Phys.org
You can use the Scanner class:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
For example, the output of
import java.util.Scanner;
public class Hello
{
public static void main(String[] argv)
{
String s = "a 123 b";
Scanner scan = new Scanner(s);
System.out.println(scan.next());
System.out.println(scan.next());
System.out.println(scan.next());
}
}
is:
a
123
b
 
This will work without having to put empty spaces in your input string:

String inputString = "6G2N42B";
String expression = "(\\d*+)"; // Searches for all groups of numbers
Pattern myPattern = Pattern.compile(expression);
Matcher matcher = myPattern.matcher(inputString);
while(matcher.find()){
if(!matcher.group().equalsIgnoreCase("")){
System.out.println("matcher.group(): " + matcher.group());
}
}

expression = "(\\D*+)"; // Searches for all groups of non-numbers
myPattern = Pattern.compile(expression);
matcher = myPattern.matcher(inputString);

while (matcher.find()) {
if(!matcher.group().equalsIgnoreCase("")){
System.out.println("matcher.group(): " + matcher.group());
}
}

Output:


matcher.group(): 6
matcher.group(): 2
matcher.group(): 42
matcher.group(): G
matcher.group(): N
matcher.group(): B
 
Thanks, I'll give that a try :)
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
5K
  • · Replies 15 ·
Replies
15
Views
5K
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K