Java Manipulating a String: Solving the 6G2N42B Puzzle

  • Thread starter Thread starter NiaSphinx
  • Start date Start date
  • Tags Tags
    Puzzle String
AI Thread Summary
The discussion focuses on how to manipulate a user-input string in Java to extract integers and characters separately. A user seeks guidance on splitting a string like "6G2N42B" into its numeric and alphabetic components. The solution involves using the Scanner class for basic string parsing and regular expressions for more complex extraction. The provided code examples demonstrate how to use the Pattern and Matcher classes to identify and print groups of digits and non-digit characters from the input string. The output confirms the successful separation of the integers '6', '2', '42' and the characters 'G', 'N', 'B'. This approach allows for efficient string manipulation without needing to format the input with spaces.
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 :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
10
Views
3K
Replies
15
Views
5K
Replies
1
Views
3K
Replies
3
Views
3K
Replies
37
Views
4K
Replies
3
Views
1K
Replies
1
Views
1K
Back
Top