how to fill a char array with a given string - java

Here is my code where i read a txt file and create 2 string from its content now i need to create 2 char array with these 2 strings. How can i do this?
java.io.File file=new java.io.File("deneme3.txt");
try{
Scanner input=new Scanner(file);
while(input.hasNext()){
String num= input.nextLine();
String[] parts =num.split(" ");
String part1=parts[0];
String part2=parts[1];
in the end i need to have something like;
char[] mSeqA and char[] mSeqB;

char[] mSeqA=parts[0].toCharArray();
char[] mSeqB=parts[1].toCharArray();

To convert a string to a char array you can simply write
String str = "someString";
char[] charArray = str.toCharArray();
So in your case it would be
char[] mSeqA = parts[0].toCharArray();
char[] mSeqB = parts[1].toCharArray();
See http://docs.oracle.com/javase/7/docs/api/java/lang/String.html for details

Related

How can I check if the character from a user- inputted string (first string) is present on the second string?

We have to write a program that inputs two String values. And this is the condition:
If the third character of the first string is present on the second string then do this code...
I tried using the .contains() method but there is an error. I also don't know how to apply loops because the output is being printed several times. What should I do?
The error says "The method contains (CharSequence) in the type String is not applicable for the arguments (char)"
System.out.println("Input String 1:");
Scanner sc1 = new Scanner(System.in);
String str1 = sc1.nextLine();
System.out.println("Input String 2:");
Scanner sc2 = new Scanner(System.in);
String str2 = sc2.nextLine();
char third = str1.charAt(2);
if (str2.contains(third)) {
str1 = str1.replaceAll("[AaEeIiOoUu]", "*");
str1.replaceAll("[AaEeIiOoUu]", "*");
System.out.println(str1.toUpperCase());
String first = "xyz";
String nthChar = Character.toString(first.charAt(2);
String second = "aaaaaaaz";
if(second.indexOf(nthChar) != -1){
// nth character from 'first' exists in 'second'
// do whatever
}

Java- toCharArray() method

Scanner in= new Scanner(System.in);
int len=in.nextInt();
String s=in.next();
char[] ch=s.toCharArray();
System.out.println(len);
System.out.println(ch.length);
I have run this code snippet by providing input
400004
//and a string of length 400004
The output was
400004
8190
I know when we use toCharArray() then it returns a char array having same length as of the String. But here it is diffirentcult.
I am not able to understand how this is possile.
Please help me out here.
You most certainly copied and pasted a string that contains a newline character or another character, which made the next method of Scanner return. Thus, only part of the string is copied to s.
I tried this code, only length = 8190 string is entered in String variable because of constraint of command prompt buffer.
You can try getting input from a text file, it works.
See me code billow for reference.
Scanner in = new Scanner(System.in);
int n=0;
String number=null;
BufferedReader br = null;
FileReader fr = null;
try {
br = new BufferedReader(new FileReader("txt file path"));
String len= br.readLine();
n=Integer.parseInt(len);
number = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}

Converting newline character in char array to string?

char[] charArray = {'\n'};
String str = String.valueOf(charArray);
System.out.println(str);
So I want to obtain a string of the newline character that is in the array.
However, with this code, the output is simply a blank space. How can I get
String str= "\n" ?
'\n' is the encoding for a newline character. Perhaps you want:
String str = "\\n"
or perhaps you want
char[] charArray = {'\\','n'};
String str = String.valueOf(charArray)

Retrieving part of a string using a delimiter

Okay So I am creating an application but I'm not sure how to get certain parts of the string. I have read In a file as such:
*tp*|21394398437984|163600
*2*|AAA|1234567894561236|STOP|20140527|Success||Automated|DSPRN1234567
*2*|AAA|1234567894561237|STOP|20140527|Success||Automated|DPSRN1234568
*3*|2
I need to read the lines beginning with 2 so I done:
s = new Scanner(new BufferedReader(new FileReader("example.dat")));
while (s.hasNext()) {
String str1 = s.nextLine ();
if(str1.startsWith("*2*")) {
System.out.print(str1);
}
}
So this will read the whole line I'm fine with that, Now my issue is I need to extract the 2nd line beginning with numbers the 4th with numbers the 5th with success and the 7th(DPSRN).
I was thinking about using a String delimiter with | as the delimiter but I'm not sure where to go after this any help would be great.
You should use String.split("|"), it will give you an array - String[]
Try following:
String test="*2*|AAA|1234567894561236|STOP|20140527|Success||Automated|DSPRN1234567";
String tok[]=test.split("\\|");
for(String s:tok){
System.out.println(s);
}
Output :
*2*
AAA
1234567894561236
STOP
20140527
Success
Automated
DSPRN1234567
What you require will be placed at tok[2], tok[4], tok[5] and tok[8].
Just split the returned line based on your search, which would return an array of String elements where you can retrieve your elements based on their index:
s = new Scanner(new BufferedReader(new FileReader("example.dat")));
String searchLine = "";
while (s.hasNext()) {
searchLine = s.nextLine();
if(searchLine.startsWith("*2*")) {
break;
}
}
String[] strs = searchLine.split("|");
String secondArgument = strs[2];
String forthArgument = strs[4];
String fifthArgument = strs[5];
String seventhArgument = strs[7];
System.out.println(secondArgument);
System.out.println(forthArgument);
System.out.println(fifthArgument);
System.out.println(seventhArgument);

String to bytes to string

The question is in comments in the code, I thought that'd be an easier way to ask...
Easy question, but I can't seem to find an answer. I want to convert a String to it's byte[] (easy, String.getBytes()). Then I want to convert a String of bytes (101011010101001 for example) to a byte[] and get the String value of that (that's easy too: new String(byte[]))
Here's what I've got so far:
Scanner scan = new Scanner(System.in);
String string = scan.nextLine();
String byteString = "";
for (byte b : string.getBytes()) {
byteString += b;
}
System.out.println(byteString);
//This isn't exactly how it works, these two parts in separate methods, but you get the idea...
String byteString = scan.nextLine();
byte[] bytes = byteString.literalToBytes() //<== or something like that...
//The line above is pretty much all I need...
String string = new String(bytes);
System.out.println(string);
This won't work. The problem is that when you convert your bytes to a string you are going to get a string like
2532611134
So analyzing this string, is the first byte 2, or 25, or 253?
The only way to make this work would be to use a DecimalFormat and make sure every byte is 3 characters long in your string
EDIT
Please see this answer for a solution.
With this you can:
String string = scan.nextLine();
String convertByte = convertByte(string.getBytes());
System.out.println(convertByte);
String byteString = scan.nextLine();
System.out.println(new String(convertStr(byteString)));
Alright, because the commenter who pointed me to this question (which lead me to this answer) isn't going to answer, I'll just post the solution here:
Scanner scan = new Scanner(System.in);
String pass = scan.nextLine();
StringBuilder byteString = new StringBuilder();
for (byte b : pass.getBytes()) {
b = (byte) (b);
byteString.append(b).append(","); //appending that comma is what does the trick.
}
System.out.println(byteString);
//
String[] split = byteString.toString().split(","); //splitting by that comma is what does the trick... too...
byte[] bytes = new byte[split.length];
for (int i = 0; i < split.length; i++) {
bytes[i] = (byte) (Byte.valueOf(split[i]).byteValue());
}
System.out.println(new String(bytes));
I guess what you want is this
// to get back the string from byte array
StringBuilder byteString = new StringBuilder();
for (byte b : string.getBytes()) {
byteString.append((char)b);
}
System.out.println(byteString.toString());
// to get the binary representation from string
StringBuilder byteString = new StringBuilder();
for (byte b : string.getBytes()) {
System.out.print(Integer.toBinaryString((int)b));
}
System.out.println(byteString.toString());

Categories