[Solved] Add the factors of a number

import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner x=new Scanner(System.in); int n=0;int g=0; int term=0;int temp=0; int sum=0; int factor=1; System.out.print(“Input N:”); n=x.nextInt(); g=n; int number = 0; if (n<=0) { System.out.println(“Please enter a positive integer”); System.exit(0); } if (n>0) { System.out.print(“The factors are:”); while (factor<n) { if (n%factor==0) { … Read more

[Solved] StringBuilder delete methods [closed]

I’d say this is fine. The intent is different. Lets try this for example: String builder sb = new StringBuilder(); sb.append(“hello”).append(“hello”); sb.delete(0,5); sb.toString(); It prints “hello”. Had you read the documentation, you’d see that the end is non-inclusive, meaning it will delete from start to end-1, hence no IndexOutOfBounds. However, try this: StringBuilder builder = … Read more

[Solved] Reverse Names Given [closed]

If you have all names in file (e.g. names.txt): Baynes, Aron Bazemore, Kent Beal, Bradley Beasley, Malik Beasley, Michael Belinelli, Marco Bell, Jordan Bembry, DeAndre’ You can: Read line split line (using separator) display in reverse way import java.io.BufferedReader; import java.io.FileReader; public class Main { public static void main(String[] args) { // File name String … Read more

[Solved] How to calculate percentage of matching values on two arraylists?

Try this: List<String> actual = new ArrayList<>(); List<String> required = new ArrayList<>(); List<String> common = new ArrayList<>(actual); common.retainAll(required); System.out.println(” 100.0 * common / actual = ” + (actual.size() == 0 ? 0 : 100.0 * common.size() / actual.size())); System.out.println(” 100.0 * common / required = ” + (required.size() == 0 ? 0 : 100.0 * … Read more

[Solved] Addition method to java class [duplicate]

Java doesn’t support operator overloading, instead you need to add an add function: public class Money { private BigDecimal amount; private Currency currency; public Money add(Money m) { Money res = new Money(); if (!currency.equals(m.currency)) { throw new UnsupportedOperationException(); } res.currency = currency; res.amount = m.amount.add(amount); return res; } } Money result = one.add(two); 6 … Read more

[Solved] How to add numbers for arraylist

Just create a list and add items to it, List<Integer> checkedList = new ArrayList<Integer>(); for (Product p : boxAdapter.getBox()) { if (p.aBoolean) checkedList.add(p.id); } I would suggest you work on your basics before you start coding, it will help you in such situations. Hope this is the solution you were looking for. 0 solved How … Read more

[Solved] return Array[] or return null Java

Probably this line itemInventory.get(j).getName().equalsIgnoreCase(n) == true causes NPE. Make sure that itemInventory.get(j) is not null before you get the name: if(itemInventory.get(j)!=null) //only then get the name if(itemInventory.get(j).getName().equalsIgnoreCase(n)) 6 solved return Array[] or return null Java

[Solved] Inheritence – Mock Exam

Which of the assignment statements in someMethod are valid? All of them. x, this.x and super.x all point to protected int x in class A which is visible to the subclass B. this.getX() and super.getX() both call public int getX() in class A which is visible to the subclass B. answer, x and the return … Read more