Get value from multiple checkbox's selected and display in TextArea - java

I will have display all the toppings the user selected and there can be more than one but my code only display one at a time. Please advise.
Here is my code for toppings part:
public String toppingsSelect()
{
String toppingsSelectString = "";
if(tomatoCheckBox.isSelected())
{
toppingsSelectString = "Tomato";
}
if(greenPeppersCheckBox.isSelected())
{
toppingsSelectString = "Green Peppers";
}
if(mushroomsCheckBox.isSelected())
{
toppingsSelectString = "Mushrooms";
}
if(blackOlivesCheckBox.isSelected())
{
toppingsSelectString = "Black Olives";
}
if(sausageCheckBox.isSelected())
{
toppingsSelectString = "Sausage";
}
if(extraCheeseCheckBox.isSelected())
{
toppingsSelectString = "Extra Cheese";
}
return toppingsSelectString;
}
displayString = "Pizza type : " + crustSelect() + "\n" +
"Pizza size : " + sizeSelect() + "\n" +
"Toppings : " + toppingsSelect() + "\n" +
"Amount Due : " + dollarDecimalFormat.format(totalPriceFloat);
outputTextArea.setText(displayString);

Each time you assign a value to toppingsSelectString it replace the precedent value. You should put for exemple toppingsSelectString += " " + "Tomato";

Related

If Else statement logic execution

The line with the problem is. This line should check if the 3rd index contains character and it should not contain the words north and america.
else if ( salida[3].matches("[a-zA-Z]+") && !salida[3].equals("North") ) { // not working correctly
if ( !salida[3].equals("America")) {
salida1 = salida1 + salida[0] + " " + salida[1] + " " + salida[2] + " " + salida[3] + ",";
The code above should run for the 4th line of the array data below
[United, States, 1,527,664, 90,978, North, America]
[Canada, 77,002, 5,782, North, America]
[Turks, and, Caicos, 12, 1, North, America]
[St., Vincent, &, Grenadines, 17, 0, North, America]
this is the string output I'm currently getting which doesn't add the 3rd index of the array to the string
United States,Canada ,Mexico ,Dominican Republic,Panama ,Honduras ,Guatemala ,Cuba ,El Salvador,Costa Rica,Jamaica ,Haiti ,Martinique ,Guadeloupe ,Bermuda ,Trinidad and,Aruba ,Bahamas ,Cayman Islands,Barbados ,Sint Maarten,Saint Martin,Nicaragua ,Antigua and,Grenada ,Belize ,Saint Lucia,St. Vincent,CuraƧao ,Dominica ,Saint Kitts,Turks and,Montserrat ,Greenland ,British Virgin,Saint Barthelemy,Caribbean Netherlands,Anguilla ,Saint Pierre,
Input country to display data:
This is my entire code
public String setCountriesList() {
String salida1 = "";
try {
Document doc = Jsoup.connect("https://www.worldometers.info/coronavirus/countries-where-coronavirus-has-spread/").get();
Elements tr = doc.select("tr");
String [] na = {"north", "america"};
for (int i = 0; i < tr.size(); i++) {
if (tr.get(i).text().contains("North America")) {
String[] salida = tr.get(i).text().split(" ");
System.out.println(salida[3].contains("North") + " and " + salida[3].contains("America") );
System.out.println(Arrays.deepToString(salida)); //split salida to country, number ,number in array
if ( salida[1].matches("[a-zA-Z]+")) {
salida1 = salida1 + salida[0] + " " + salida[1] + ",";
}
else if ( salida[2].matches("[a-zA-Z]+")) {
salida1 = salida1 + salida[0] + " " + salida[1] + " " + salida[2] + ",";
}
else if ( salida[3].matches("[a-zA-Z]+") && !salida[3].equals("North") ) { // not working correctly
if ( !salida[3].equals("America")) {
salida1 = salida1 + salida[0] + " " + salida[1] + " " + salida[2] + " " + salida[3] + ",";
}}
```
else {
salida1 = salida1 + salida[0] + " ,";
}
}
}
return salida1;
} catch (Exception ex) {
System.out.println("error");
return "error";
}
}
The issue is that the names of the countries contain a comma "," which is your separator character. You need to find a way to have the country name within the same index 0, or at least wrap the country name in quotes "", so that "North" is effectively index 3. In the examples above "North" was only index 3 for Canada.

How do I add "and" before the last author's last name?

public String Cite()
{
String authorsList = "";
Collections.sort(authors);
for(Author a: authors)
{
authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", ";
}
String cite = authorsList + "\"" + title + "\", " + venue + "(" + getAcronym() + ")" + " , " +
publisher;
return cite;
}
How would I go about adding the word "and" to separate the last two names of the list?
Use a for loop with index.
for (int i = 0; i < authors.size(); ++i) {
if (i == authors.size() - 2) {
authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + "and ";
} else {
authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", ";
}
}
public static String Cite(ArrayList<Author> authors){
String authorsList = "";
Collections.sort(authors, new CustomComperator());
int size = authors.size();
int count = 0;
for(Author a: authors) {
if (size ==1) {
authorsList = a.firstName.toUpperCase().charAt(0) + ". " + a.lastName;
}
else if (count == size-2) {
authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName ;
}
else if (count == size - 1) {
authorsList += " and " + a.firstName.toUpperCase().charAt(0) + ". " + a.lastName ;
}
else{
authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", ";
}
count ++;
}
return authorsList;
}
You should use a normal for loop, so you can detect that you're on the first and/or last element.
Other changes:
Remove the , after the last author.
Use StringBuilder to build a String.
List<Author> authors = new ArrayList<>(List.of(
new Author("Stephen", "King"),
new Author("John", "Grisham"),
new Author("William", "Shakespeare"),
new Author("Charles", "Dickens") ));
Collections.sort(authors);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < authors.size(); i++) {
Author a = authors.get(i);
if (i != 0)
buf.append(i < authors.size() - 1 ? ", " : " and ");
buf.append(a.firstName.toUpperCase().charAt(0) + ". " + a.lastName);
}
String authorsList = buf.toString();
System.out.println(authorsList);
Output
C. Dickens, J. Grisham, S. King and W. Shakespeare
Oxford comma
Whether or not you want , comma before and (Oxford comma) is of course entirely up to you.
buf.append(i < authors.size() - 1 ? ", " : ", and ");
Output
C. Dickens, J. Grisham, S. King, and W. Shakespeare
UPDATE
Since all 4 other answers at this time gave bad result for a single Author, here is test result for various number of authors.
Full Test
List<Author> allAuthors = List.of(
new Author("Stephen", "King"),
new Author("John", "Grisham"),
new Author("William", "Shakespeare"),
new Author("Charles", "Dickens") );
for (int aCount = 0; aCount <= allAuthors.size(); aCount++) {
List<Author> authors = new ArrayList<>(allAuthors.subList(0, aCount));
Collections.sort(authors);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < authors.size(); i++) {
Author a = authors.get(i);
if (i != 0)
buf.append(i < authors.size() - 1 ? ", " : " and ");
buf.append(a.firstName.toUpperCase().charAt(0) + ". " + a.lastName);
}
String authorsList = buf.toString();
System.out.println(aCount + ": \"" + authorsList + "\"");
}
Output
0: ""
1: "S. King"
2: "J. Grisham and S. King"
3: "J. Grisham, S. King and W. Shakespeare"
4: "C. Dickens, J. Grisham, S. King and W. Shakespeare"
If you want to do the same without loops you could:
Define a toString or similar method inside your Author class:
class Author{
String firstName;
String lastName;
Author(String firstName,String lastName){
this.firstName=firstName;
this.lastName=lastName;
}
#Override
public String toString(){
return String.format("%s. %s",this.firstName.toUpperCase().charAt(0), this.lastName);
}
}
And use the Collectors.joining to create the initial list (comma separated).
List<Author> authors = Arrays.asList(new Author("Jules", "Verne"),
new Author("Pablo", "Neruda"), new Author("JK", "Rowling"));
authors.sort(Comparator.comparing(a -> a.lastName));
StringBuffer result = new StringBuffer(
authors.stream().limit(authors.size() - 1).map(a -> a.toString()).collect(
Collectors.joining(" ,")));
And after that, add the last "and":
if (authors.size() > 1) {
result.append(String.format(" and %s", authors.get(authors.size() - 1).toString()));
} else {
result.append(authors.get(authors.size() - 1).toString());
}
System.out.println(result);
Output: P. Neruda ,J. Rowling and J. Verne

why does the following keep returning an out of bounds error?

Please help my figure out why this keeps throwing an out of bounds error. i tried making a separate loop keep track of AdminDecisions
String returnProfile() {
String uniPicksString ="";
String studentInfo = null;
//for(int i = 0; i<ApplicantArray.size(); i++) {
studentInfo =FAMILYNAME+ ", " + "average = " + AVERAGE + " ";
for (int j = 0; j<CHOICES.size(); j++) {
if(j<CHOICES.size() - 1) {
uniPicksString = uniPicksString + CHOICES.get(j)+ ": " + " admin decision, " ;
}else {
uniPicksString = uniPicksString + CHOICES.get(j)+ ": " + " admin decision" + "\n";
}
}
//}
return studentInfo + uniPicksString + "\n";
}
the following code shows the desired out put but i cant return it as a string
String printProof() {
// System.out.println("from inside the student class");
String temp=null;
d = new ArrayList<String>();
for(int j = 0 ; j<1; j++) {
System.out.print("\n >>printProof<< " + FAMILYNAME+", " + "average = " + AVERAGE + " ");
for (int i = 0; i<AdminDecision.size(); i++) {
//System.out.println(AdminDecision.get(i));
//temp = CHOICES.get(i)+ ": " + AdminDecision.get(i) + ", ";
if(i<CHOICES.size() - 1) {
temp = CHOICES.get(i)+ ": " + AdminDecision.get(i) + ", ";
}else {
temp = CHOICES.get(i)+ ": " + AdminDecision.get(i) + "\n";
}
System.out.print(temp + " ");
d.add(AdminDecision.get(i));
}
}
return temp + "\n";
}

How to get LAC and Cell id for LTE Network

i am using GsmCellLocation to get LAC and cell id for 3G network with below code :
mCid = gmsCellLocation.getCid() & 0xffff;
mLac = gmsCellLocation.getLac();
and is there any library or formula how to get/calculate the correct LAC and cell id for LTE network (4G) ? Thanks.
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
for (int i = 0; i < cellInfoList.size(); i++) {
if (cellInfoList.get(i) instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfoList.get(i);
mCid = cellInfoLte.getCellIdentity().getCi();
mLac = cellInfoLte.getCellIdentity().getTac();
}
}
Note the method name, it is getCi for LTE. Also, getTac for LTE instead of getLac. See this answer for more.
I hope this might help you :
public class MobileInfoRecognizer {
public String getCellInfo(CellInfo cellInfo) {
String additional_info;
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();
additional_info = "cell identity " + cellIdentityGsm.getCid() + "\n"
+ "Mobile country code " + cellIdentityGsm.getMcc() + "\n"
+ "Mobile network code " + cellIdentityGsm.getMnc() + "\n"
+ "local area " + cellIdentityGsm.getLac() + "\n";
} else if (cellInfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();
additional_info = "cell identity " + cellIdentityLte.getCid() + "\n"
+ "Mobile country code " + cellIdentityLte.getMcc() + "\n"
+ "Mobile network code " + cellIdentityLte.getMnc() + "\n"
+ "physical cell " + cellIdentityLte.getPci() + "\n"
+ "Tracking area code " + cellIdentityLte.getTac() + "\n";
} else if (cellInfo instanceof CellInfoWcdma){
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();
additional_info = "cell identity " + cellIdentityWcdma.getCid() + "\n"
+ "Mobile country code " + cellIdentityWcdma.getMcc() + "\n"
+ "Mobile network code " + cellIdentityWcdma.getMnc() + "\n"
+ "local area " + cellIdentityWcdma.getLac() + "\n";
}
return additional_info;
}
}

Is there any possibility to get the currentStockLevel from this method?

I need the currentStockLevel for another void Method in java, is there any possibility to get it?
I think no, because of void right?
public void receive (int currentStock)
{
String outLine;
if (currentStockLevel > 0)
outLine = productCode;
{
outLine = ". Current Stock: " + currentStockLevel;
outLine += " Current Stock changed from " + currentStockLevel;
currentStockLevel += currentStock;
outLine += " to " + currentStockLevel;
int storeCost = wholeSalePrice * currentStockLevel;
System.out.println (productCode + ":" + " Received " + currentStockLevel + "." + " Store Cost " + "$" + storeCost + "." + " New stock level: " + currentStockLevel);
}

Categories

Resources