How to manually set an array in Java? - java

How do you manually assign variables to an array? I have a code fragment below.
I don't want to manually put shortStraight[0] = "211100", shortStraight[1] = "021110", and so on. Any help?
private String [] shortStraight;
public Sample () {
shorty = new String [12];
shorty = {211100, 021110, 002111, 121100, 112100, 111200, 012110, 011210, 011120, 001211, 001121, 001112 } //this line doesn't work.
Any help?

String[] shorty = {"211100", "021110", "002111", "121100", "112100", "111200", "012110", "011210", "011120", "001211", "001121", "001112"} ;

String[] shorty = {"211100", "021110", "002111", "121100", "112100", "111200", "012110", "011210", "011120", "001211", "001121", "001112"} ;
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Related

I want to data type Conversion in a row in android

Hi.
I'm making an app that receives data from bluetooth by using stringbuilder
And makes it slice for using another activity.
The image shows what i want to make.
Q1. What should i use c->d, d->e ?
Q2. There will be a lot of data, I want to know the way to simplify this sequence
******************** edited ********************
I have practiced by adding value to Arraylist.
But in String Array, there is no .get(), so i couldn't access to element's length.
public static ArrayList<String> randomValue = new ArrayList<>();
public static int iDistance=0, xIAngle=0, yIAngle=0, zIAngle=0;
public static String distance, xAngle, yAngle, zAngle;
randomValue.add("12345090080070");
randomValue.add("15640080085071");
randomValue.add("16542070084074");
randomValue.add("12645080087078");
randomValue.add("21345084081060");
randomValue.add("14785078075065");
randomValue.add("13155079077077");
randomValue.add("14623080078078");
randomValue.add("14918086080078");
randomValue.add("15684085082080");
for (int i=0; i<randomValue.size(); i++){
String a = randomValue.get(i);
String distance = a.substring(0,5);
String xAngle = a.substring(5,8);
String yAngle = a.substring(8,11);
String zAngle = a.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}
It seems like you are just stuck on finding the equivalent of get for a string array. To access an element in an array, the syntax is array[I], so if you were using a string array, this line:
String a = randomValue.get(i);
would have been:
String a = randomValue[i];
The code for your sequence of transformations can be shortened with Streams:
// this is the sequence of transformation starting with the sting builder "a"
List<String> randomValueWithLength14 =
Arrays.stream(a.toString().split(";")).filter(x -> x.length() == 14)
.collect(Collectors.toList());
// this is the for loop shown in your code
for (int i=0; i<randomValueWithLength14.size(); i++){
String s = randomValueWithLength14.get(i);
String distance = a.substring(0,5);
String xAngle = s.substring(5,8);
String yAngle = s.substring(8,11);
String zAngle = s.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}

How to print an item from list

I have a list. I want print list item horizontally with delimiter.
I got output like this:
OderHistory: Item1-1$
OderHistory: Item2-1$
OderHistory: Item3-1$
But i need like this:
OderHistory: Item1-1$Item2-1$Item3-1
Code here:
for(int i=0;i<itemList.size();i++){
String name = itemList.get(i).ItemName;
String quanty = itemList.get(i).Quantity;
Log.d("OderHistory",name+"-"+quanty+"$");
}
Anyone help to me!
Thanks in advance...
Try this,
String result="";
for(int i=0;i<itemList.size();i++){
String name = itemList.get(i).ItemName;
String quanty = itemList.get(i).Quantity;
result=result.concat(name+"-"+quanty+"$");
}
Log.d("OderHistory:",result);

how get a specific value from a string?

i have have a string looks :
mystring = "<EQHO state="degraded"...> at /NE[1]/EQHO[2]/#state to <EQHO state="working"...> at /NE[1]/EQHO[1]/#state"
and i want to get this value :
value="NE[1]/EQHO[1]"
how can i achieve that ?
thanks
Try this:
mystring.substring(mystring.lastIndexOf("at /")+4, mystring.lastIndexOf("/#"))
but you probably should use a more generic solution. To extract all the section that have this format you can use something like this:
String mystring = "<EQHO state=\"degraded\"...> at /NE[1]/EQHO[2]/#state to <EQHO state=\"working\"...> at /NE[1]/EQHO[1]/#state";
ArrayList<String> values = new ArrayList<String>();
while(mystring.indexOf("at /") < mystring.indexOf("/#")){
String val = mystring.substring(mystring.indexOf("at /") + 4, mystring.indexOf("/#"));
values.add(val);
mystring = mystring.substring(mystring.indexOf("/#")+2);
}
System.out.println(values);
You can change the value of a string like this
mystring = "NE[1]/EQHO[1]";
Remember to include the semicolon!

Getting a part of a string in java

so, right now I have this String:
String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
I want to be able to get just this part of the the string but I'm at a loss as to how to do this.
Needed Output:
String[] requiredOutput;
requiredOutput[1] = 48526; // csfo_offering
requiredOutput[2] = csfo_add_ind A;
or
requiredOutput[2] = A; // csfo_add_ind
EDIT:
I have used some of your suggestions and am trying out subString but it seems like its a temp fix because if the length of the original string changes then it will throw a wrench in my calls. I will try regex next because it seems to go by pattern matching and I might be able to figure something out with that. Thanks everyone for all your help.
Suggestions are still appreciated!
Are the numbers always the same length? If so, use String.subString. If not use String.indexOf("csfo_add") to find the locations of the "csfo_add" parts and then find the relative locations of the required information.
Hi there you can also use split if you always have the same pattern for your string.
for example
String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
System.out.println(csfo.split("csfo_add_ind ")[1].split("\\]\\)")[0]);
Would get the requiredOutput[2] = A; // csfo_add_ind
and this would get the first one
String[] requiredOutput = new String[2];
String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
requiredOutput[0] = "csfo_add_ind " + csfo.split("csfo_add_ind ")[1].split("\\]\\)")[0];
requiredOutput[1] = csfo.split("\\]\\[csfo_add_ind ")[0].split("csfo_offering = ")[1];
//System.out.println(requiredOutput[0] + " et " + requiredOutput[1] );

Turn Declared String Into An Array

In my program the user declares a string of numbers that I am trying to figure out to turn into an array.
Example:
WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, "27, 26,
28");
Im trying to figure out how to add that string into my class instance variable.
This is what I have:
private String sampleID;
private String weekOfTest;
private int engineerID;
private String[] strengths = new String[20];
private static int count;
public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String strengths)
{
this.sampleID = sampleID;
this.weekOfTest = weekOfTest;
this.engineerID = engineerID;
this.strengths = strengths;
count++;
}
My compile error message says incompatible types, required: String[], found: String
It is because you have declared String[] strengths which is an array.
declare your constructor like this :
public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String[] strengths)
{
this.sampleID = sampleID;
this.weekOfTest = weekOfTest;
this.engineerID = engineerID;
this.strengths = strengths;
count++;
}
Make a call like :
WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, new String[] {"27","26", "28"});
You need to parse that String of numbers to multiple Strings. For example,
this.strengths = strengths.split(",");
You can't say this.strengths = strengths because the strengths argument is of type String and not String[]. That is where your error is coming from.
Pass it like this:
WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32,
new String[] { "27", "26", "28" });

Categories

Resources