I have string as below in java , when i am trying to convert getting error
HelloWorld.java:12: error: array required, but String found
c[i]=s[i];
^
1 error
public static void main(String []args){
String s="Abcde";
char c[];
System.out.print(s);
for(int i=0;i<s.length();i++)
{
//
c[i]=s[i];
}
}
}
I would suggest you to use inbuilt function instead of doing this. You can
find many methods available on internet and tutorials to convert your
string into char array in java. for example- Using **toCharArray()** Method.
`public static void main(String args[])
{
String str = "Abcde";
// Creating array and Storing the array
// returned by toCharArray()
char[] ch = str.toCharArray();
// Printing array
for (char c : ch) {
System.out.println(c);
}`
Related
I have my below code in which I'm reading a csv file and defining its schema, after that I'm converting it into BeamRecords. and then applying BeamSql to implement PTransforms.
Code:
class Clo {
public String Outlet;
public String CatLib;
public String ProdKey;
public Date Week;
public String SalesComponent;
public String DuetoValue;
public String PrimaryCausalKey;
public Float CausalValue;
public Integer ModelIteration;
public Integer Published;
}
public static void main(String[] args) {
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline p = Pipeline.create(options);
PCollection<java.lang.String> lines= p.apply(TextIO.read().from("gs://gcpbucket/input/WeeklyDueto.csv"));
PCollection<Clorox> pojos = lines.apply(ParDo.of(new ExtractObjectsFn()));
List<java.lang.String> fieldNames = Arrays.asList("Outlet", "CatLib", "ProdKey", "Week", "SalesComponent", "DuetoValue", "PrimaryCausalKey", "CausalValue", "ModelIteration", "Published");
List<java.lang.Integer> fieldTypes = Arrays.asList(Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR,Types.VARCHAR,Types.VARCHAR, Types.FLOAT, Types.INTEGER, Types.INTEGER);
BeamRecordSqlType appType = BeamRecordSqlType.create(fieldNames, fieldTypes);
PCollection<BeamRecord> apps = pojos.apply(
ParDo.of(new DoFn<Clo, BeamRecord>() {
#ProcessElement
public void processElement(ProcessContext c) {
BeamRecord br = new BeamRecord(
appType,
c.element().Outlet,
c.element().CatLib,
c.element().ProdKey,
c.element().Week,
c.element().SalesComponent,
c.element().DuetoValue,
c.element().PrimaryCausalKey,
c.element().CausalValue,
c.element().ModelIteration,
c.element().Published
);
c.output(br);
}
})).setCoder(appType, getRecordCoder());
PCollection<BeamRecord> out = apps.apply(BeamSql.query("select Outlet from PCOLLECTION"));
out.apply("WriteMyFile", TextIO.write().to("gs://gcpbucket/output/sbc.txt"));
}
My questions are:
what shall I implement in ExtractObjectsFn() so that the records gets converted into BeamRecords ?
How to write the final output to a csv file ?
I have implemented ExtractObjectsFn() as :
public void processElement(ProcessContext c) {
ArrayList<Clo> clx = new ArrayList<Clo>();
java.lang.String[] strArr = c.element().split("\n");
for(int i = 0; i < strArr.length; i++) {
Clo clo = new Clo();
java.lang.String[] temp = strArr[i].split(",");
clo.setCatLib(temp[1]);
clo.setCausalValue(temp[7]);
clo.setDuetoValue(temp[5]);
clo.setModelIteration(temp[8]);
clo.setOutlet(temp[0]);
clo.setPrimaryCausalKey(temp[6]);
clo.setProdKey(temp[2]);
clo.setPublished(temp[9]);
clo.setSalesComponent(temp[4]);
clo.setWeek(temp[3]);
c.output(clo);
clx.add(clo);
}
}
Let me know if its done correctly because while executing the code and getting error as No Coder has been manually specified; you may do so using .setCoder().
1> what shall I implement in ExtractObjectsFn() so that the records gets converted into BeamRecords ?
In the processElement() method of ExtractObjectsFn, you simply need to convert a CSV line from the input (String) to a Clorox type. Split the string by the comma delimiter (,), which returns an array. Iterate over the array to retrieve the CSV values and construct the Clorox object.
2> How to write the final output to a csv file ?
Similar process as above. You simply need to apply a new transform that will convert a BeamRecord to a CSV line (String). Members of the BeamRecord can be concatenated into a string (CSV line). After this transform is applied, the TextIO.Write transform can be applied to write the CSV line to file.
I want to use btrace to inspect the byte[] value of a method return use the #Return annotation.
The byte array is actually a normal string encoded using utf8.
The class is like below:
Class A {
byte[] method1() {
...
}
}
I have tried printArray, but it only accepts type of Objetc[], not working for type of byte[].
For print, it just outputs the internal object id like '[B#4fbc7b65'.
Is there any other way can solve the problem?
Yes, this is an omission in BTrace (https://github.com/btraceio/btrace/issues/322)
For now, use "trusted" mode where the safety checks will be turned off and you can do eg.
#BTrace(trusted = true)
public class TrustedTrace {
#OnMethod(clazz = "MyClass", method = "m", location = Location(Kind.RETURN))
public static void intercept(#Return byte[] data) {
println(Arrays.toString(data));
}
}
I new to programming and study about operator overloading. To overload "+" to add two string. But when I try to combine two string using strcpy, the second string replace the first string instead of copy with first string.
#include<string.h>
#include <iostream>
#include<conio.h>
using namespace std;
class String
{
char str[100];
public:
void operator +(String);
String()
{
strcpy(str,"");
}
String(char a[100])
{
strcpy(str,a);
}
};
void String::operator+ (String str1)
{ char temp[100];
strcpy(temp,str);
strcpy(temp,str1.str);
cout<<temp;
}
int main()
{
String s1=String("Hello");;
String s2=String("World");
s1+s2;
return 0;
}
The error in your code is that
In the operator overloading function you should use strcat - string concatenation
For more info check out : String concatenation
I think you've missed to assign the value of the two strings to a new string like this:
String nString = s1 + s2;
Hi I'm writing a test program to reverse a string. When I convert the character array to a string using the toString() method, I get the wrong output. When I try to print the array manually using a for loop without converting it to a string the answer is correct. The code I've written is shown below:
import java.util.*;
public class stringManip {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This is a string";
System.out.println("String=" +str);
//reverse(s);
char[] c = str.toCharArray();
int left = 0;
int right = str.length() - 1;
for (int i = 0; i < (str.length())/2; i++)
{
char temp = c[left];
c[left++] = c[right];
c[right--] = temp;
}
System.out.print("Reverse="+c.toString());
}
}
I should get the reverse of the string I entered, instead the output am getting is:
String=This is a string
Reverse=[C#45a1472d
Is there something am doing wrong when using the toString() method? Any help is appreciated. Thank you.
Arrays don't override the toString() method. What you're seeing is thus the output of the default Object.toString() implementation, which contains the type of the object ([C means array of chars) followed by its hashCode.
To construct a String from a char array, use
new String(c)
So I searched all over internet and in every single topic I found this solution for limitting JTextField input.
public class FixedDocument extends PlainDocument {
private int limit;
// optional uppercase conversion
private boolean toUppercase = false;
FixedDocument(int limit) {
super();
this.limit = limit;
}
FixedDocument(int limit, boolean upper) {
super();
this.limit = limit;
toUppercase = upper;
}
public void insertString (int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null){
return;
}
if ((getLength() + str.length()) <= limit) {
if (toUppercase) str = str.toUpperCase();
super.insertString(offset, str, attr);
}
}
}
but I have a problem with that code. This code line "super.insertString(offset, str, attr);" gives me error:
no suitable method found for insertString(int,java.lanf.String,javax.print.attribute.AttributeSet)
method javax.swing.text.PlainDocument.insertString(int,java.lang.String,javax.text.AttributeSet) is not applicable
(actual argument javax.printattribute.AttributeSet cannot be converted to javax.swing.text.AttributeSet by method invocation conversion)
anyone got any ideas what I am doing wrong here?
Your problem is that your importing the wrong AttributeSet class. You are importing javax.print.attribute.AttributeSet, when you should be importing javax.swing.text.AttributeSet, and the error message pretty much tells you this. Again, myself, I'd use a DocumentFilter for this as that is what it was built for.