Calling a variable from other method [duplicate] - java

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 6 years ago.
I'm new to Java and this may be a basic question. It takes me long time to understand and hopefully someone will it to me. Why can't I put priceMessage at putExtra(Intent.EXTRA_TEXT ,....) ? Is there any possible way?
public String submitOrder (String name, int price, boolean addWhippedCream, boolean addChoc) {
String priceMessage = getString(R.string.price_symbol) + (calculatePrice(addWhippedCream, addChoc));
priceMessage += "\n" + getString(R.string.name_order) + name;
priceMessage += "\n" + getString(R.string.add_whipped_cream) + " (" + addWhippedCream +")";
priceMessage += "\n" + getString(R.string.add_chocolate) + " (" + addChoc + ")";
priceMessage += "\n" + getString(R.string.cup_of_coffee) + " : " + quantity + " " + getString(R.string.cup_of_coffee);
priceMessage += "\n" + getString(R.string.thank_you);
displayMessage(priceMessage);
return priceMessage;
}
/**
* Send Button Order for Intent Action
*/
public void sendOrder (View view){
EditText nameField = (EditText)findViewById(R.id. name_field);
String name = nameField.getText().toString();
Intent sendOrder = new Intent(Intent.ACTION_SENDTO);
sendOrder.setData(Uri.parse("mailto:"));
sendOrder.putExtra(Intent.EXTRA_SUBJECT,"Tempahan Kopi : " + name);
sendOrder.putExtra(Intent.EXTRA_TEXT,priceMessage);
startActivity(Intent.createChooser(sendOrder,"Hantar Tempahan"));

In Java (and many other Object-Oriented programming languages), variables defined in a method are only available inside that method. If you want to share variables between two different methods, you can pass them as method parameters, or create them as either static or instance class members.
I'd recommend checking out the official Java Trails Tutorials to help you get started with the language. It'll explain what the different types of variables are, and when you might want to use them.

Related

cannot find symbol method append (String)

I don't get it why it says it cannot find symbol append.
do i need to use Stringbuffer? i got this code on a tutorial for receipts from youtube, and the uploader disabled comments so I can't ask him directly. please help me. Im still an amateur at java.
Tell me if I need to post my whole code or what code would you want to see to see errors. thanks in adv.
Calendar timer = Calendar.getInstance();
timer.getTime();
SimpleDateFormat tTime = new SimpleDateFormat("HH:mm:ss");
tTime.format(timer.getTime());
SimpleDateFormat Tdate = new SimpleDateFormat("dd-MMM-yyyy");
Tdate.format(timer.getTime());
jtxtReceipt.append("\ Water Station Receipt:\n" +
"Reference:\t\t\t" + refs +
"\n=========================================\n" +
"Mineral:\t\t\t" + jtxtMineral.getText() + "\n\n" +
"Purified:\t\t\t" + jtxtPurified.getText() + "\n\n" +
"Travel:\t\t\t" + jtxtTravel.getText() + "\n\n" +
"VAT:\t\t\t" + jtxtVat.getText() + "\n"+
"\n========================================\n" + "\n" +
"Tax:\t\t\t" + jtxtTax2.getText() + "\n" +
"Subtotal:\t\t\t" + jtxtSubTotal.getText() + "\n" +
"Total:\t\t\t" + jtxtTotal.getText() + "\n" +
"===========================================" +
"\nDate:" + Tdate.format(timer.getTime()) +
"\ntTime:" + tTime.format(timer.getTime()) +
"\n\t\tThank you ");
The append method doesn't exist in the String class. You can either user a StringBuilder to do the job, or if it's a light concatenation, just use the + operator
The append method doesn't work on TextField Palette. So, if You're on TextField Palette, replacing that with TextArea Palette should solve the problem.

Convert Loadrunner File Parameter to Java string for payload

I have a java virtual user script that is sending a payload request. I am trying to use values from a file to send via a loadrunner file parameter.
here is the payload:
private static final String PAYLOAD =
"<ips_cad_mdt>\n" +
" <SignOnRequest>\n" +
" <DestApplication>hhhh</DestApplication>\n" +
" <OrigApplication>hhh</OrigApplication>\n" +
" <SessionRef>3</SessionRef>\n" +
" <Aliasing>1234</Aliasing>\n" +
" </SignOnRequest>\n" +
"</ips_cad_mdt>";
I would like to use something like the following:
private static final String PAYLOAD =
"<ips_cad_mdt>\n" +
" <SignOnRequest>\n" +
" <DestApplication>hhh</DestApplication>\n" +
" <OrigApplication>hhh</OrigApplication>\n" +
" <SessionRef>3</SessionRef>\n" +
" <Aliasing>”+lr.eval_string(“{AliasId}”)+”</Aliasing>\n" +
" </SignOnRequest>\n" +
"</ips_cad_mdt>";
for some reason i cant see any output for this value. do i need to declare a variable: e.g. lr.save_string("AliasId", "{AliasId}");
an example of this would help loads. Many Thanks
There seems to be an error in the code completion in VuGen. The parameters should be reversed and without the {} in save_string.
lr.save_string("1234","myId");
lr.message(lr.eval_string("{myId}"));
In the documentation it is correct - https://admhelp.microfocus.com/lr/en/12.55/help/function_reference/FuncRef.htm#FuncRef/c_vuser/lrFr_lr_save_string.htm?Highlight=lr_save_string
I asked the responsible team to fix the code completion in VuGen so you will be able to see this change in one of the future releases.

.split("|") doesn't return the correct string [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I'm sending this string from the client to the server:
Ar|0.04107356|-0.31299785|-0.9991561
That string is as printed out by the server - So it is correct.
"Ar" is the packet name, and the values are the velocity of an arrow that the archer is going to shoot.
So to get those values, I'm using
String[] values = str.split("|");
And then
a.shoot(Float.valueOf(values[1]), Float.valueOf(values[2]), Float.valueOf(values[3])); //a is an archer
The problem is, values1, values[2], and values[3] seem to be corrupt or unrecognizable.
My full code is this:
public void handleMessage(String str){
System.out.println(str);
String[] values = str.split("|");
if (values[0].contains("Ar")){
System.out.println("X: " + values[1] + " Y: " + values[2] + " Z: " + values[3]);
}
System.out.println("Vals: " + values[0] + " " + values[1] + " " + values[2] + " " + values[3]);
if (true) return; //Returning so I can analyze de-bug messages without crashes.
for (Archer a : GameServer.archers){
a.shoot(Float.valueOf(values[1]), Float.valueOf(values[2]), Float.valueOf(values[3]));
}
When I print out the "Vals" message, it comes out like this:
Vals: A r |
What is going wrong here?
The horizontal bar has a special meaning in Java regular expressions which you must escape in order to use as a literal:
String[] values = str.split("\\|");

Aligning a String in JOptionPane

I have a string that I want to display in a JOptionPane.
String info = "Name:" + _name + "\n" +
"Phone:" + _phone;
I tried to add \t but it didn't work.
I tried also to
int choose = JOptionPane.showConfirmDialog(this,new JTextArea(info),"XXX",0);
But it doesn't look good.
Are there another ways to do that? (If you know about a solution where I can use something like \t it will be very usefull for me)
* In this specific example I can manually align it, but I'm looking for a general solution.
HTML formatting could help:
String info = "Name:" + _name + "<br>" +
"Phone:" + _phone + "<br>";
int choose =
JOptionPane.showConfirmDialog(this, "<html>" + info + "</html>", "XXX", 0);
Another option is to use a JTable in the JOptionPane dialog as shown in this solution.
I had something like that and it worked:
String st = "<table border = \"0\">" +
"<tr><td>VALUE1: </td><td>" + _value2 + "</td></tr>"+
"<tr><td>VALUE2: </td><td>" + _value4 + "</td></tr>" +
//.....
"</table>";

What are some better ways to print the following section? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What are some better ways to print the following section? I think it will look better in a table format of some type with a heading. I want to adjust them to fixed lenghts, I think. Here is the code snippet I want to format to look better. The print elements are elements in a sql database.
System.out.println("\nAll records in your table:");
System.out.println("ID# Name GPA Status Mentor Level Thesis Advisor Company"); //table heading...
while (rs.next()) {
String output = " ";
output += rs.getString("studentID") + " "
+ rs.getString("firstName") + " "
+ rs.getString("lastName") + " "
+ rs.getString("gpa") + " "
+ rs.getString("status") + " "
+ rs.getString("mentor") + " "
+ rs.getString("level") + " "
+ rs.getString("thesisTitle") + " "
+ rs.getString("thesisAdvisor") + " "
+ rs.getString("company") + "\n";
System.out.printf("%s", output);
}
|You can|
|Do something|
|like this|
|studentname|
|firstname|
|middlename|
|.......|
"I want to adjust them to fixed lengths":
is that why your concatenating whitespaces in different lengths ?
You can try to concatenate "\t\t" instead.
In case it's HTML, using HTML Table will take care of it for you.

Categories