I have a list of Nodes which I loop through:
for (int i = 0; i < aNodes.getLength(); i++) {
//
}
For example, assume the list contains 12 items, and that I know 3 items represent a row in a table, and the next 3 belong to the following row. That means my list of 12 Nodes originates from an HTML table containing 4 rows.
After each row I want to do something, for example, create a new object and populate it...or whatever. I have a solution:
ArrayList<Account> mAccounts = new ArrayList<Account>();
Account account = new Account();
for (int i = 0; i < aNodes.getLength(); i++) {
String nodeValue = aNodes.item(i).getNodeValue();
boolean isNewRow = (i % COLS_PER_ROW == 0);
if (isNewRow) {
account = new Account();
mAccounts.add(account);
}
switch (i % COLS_PER_ROW) {
case ACCOUNT_POS:
account.setAccount(nodeValue);
break;
case BALANCE_POS:
account.setBalance(nodeValue);
break;
case DATE_POS:
account.setDate(nodeValue);
break;
}
}
But there are numerous things I don't like about this solution:
An Account instance is created twice first time, once outside the loop and then once because a new row is detected.
It uses integer constants ACCOUNT_POS=0, BALANCE_POS=1, DATE_POS=2...this doesn't feel very nice and I think I should be using an enum.
I can't use an enum with the loop variable 'i'.
I can't use a for each loop since Nodes doesn't implement the correct interface,
Can anyone suggest a better way of doing this that solves the list of things I don't like about it?
Thank you.
You can increment i by COLS_PER_ROW instead of 1 and then write:
for (int i = 0; i < aNodes.getLength(); i += COLS_PER_ROW) {
account = new Account();
String account = aNodes.item(i).getNodeValue();
account.setAccount(account);
String balance = aNodes.item(i+1).getNodeValue();
account.setBalance(balance);
String date = aNodes.item(i+2).getNodeValue();
account.setDate(date);
mAccounts.add(account);
}
I can't use an enum with the loop variable 'i'.
No, but you can use:
for(SomeEnum item : SomeEnum.values()){
// code here
}
You're working with a position based file, so, if I understand you correctly, you've a structure which is organized as follows:
account;
balance,
date;
Knowing this, maybe you could better deal with the index in a while loop, rather than using a for loop, as follows:
int i = 0;
while (i < aNodes.getLength()) {
account = new Account();
account.setAccount(aNodes.item(i).getNodeValue());
account.setBalance(aNodes.item(i+1).getNodeValue());
account.setBalance(aNodes.item(i+2).getNodeValue());
mAccounts.add(account)
i += 3;
}
I would also suggest to extract the account-filling-code in a new method, like 'extractAccountFromNodes', and then call it inside the loop; so the loop could become:
int i = 0;
while (i < aNodes.getLength()) {
mAccount.add(extractAccountFromNodes(aNodes, i));
i += 3;
}
with the method extractAccountFromNodes like this:
private Account extractAccountFromNodes(Nodes nodes, int position) {
account = new Account();
account.setAccount(nodes.item(i).getNodeValue());
account.setBalance(nodes.item(i+1).getNodeValue());
account.setBalance(nodes.item(i+2).getNodeValue());
return account;
}
Related
ArrayList<Integer> companiesId = new ArrayList<>();
int numberOfCompanies = 10; // Just for example
...
float profit;
Scanner input = new Scanner(System.in);
for(int i = 0; i < numberOfCompanies; i++) {
int companyId = input.nextInt();
if (!companiesId.contains(companyId)) {
companiesId.add(companyId);
}
if (companiesId.get(i) == 1) {
profit = 1000;
} else if (companiesId.get(i) == 2) {
profit = 2000;
}
}
Now I want to print all the companyIds from the ArrayList with the profit entered for each id, but I don't know how to do it with the ArrayList.
It should be:
1 1000
2 2000
...
You cannot do what you ask because part of the information you need to print (profit) is lost. What you need to do first is to create a class that holds a company ID and the profits. With the new version of Java, you can create a record that will hold such information. A Java Record is nothing more than a POJO that is identified with that new keyword (record) and does not require you to create all the boilerplate code. Your record class will look something like this:
public record CompanyRecord(int companyID, float profit) {
}
You don't even need to override toString(). That is, unless you want to print the contents of the record in a different way than the default. Then, you will need to create a list of CompanyRecord objects:
ArrayList<CompanyRecord> companies = new ArrayList<>();
Then, you can do whatever you need. For example, I created this simple demo that create a list of 10 company records and uses the loop counter to set the company ID and as a multiplier for the profits. Lastly, it prints out the record to the console.
public class CompanyRecordDemo {
public static void main(String[] args) {
ArrayList<CompanyRecord> companies = new ArrayList<>();
float profit = 1000.0f;
for (int i = 1; i <= 10; i++) {
CompanyRecord rec = new CompanyRecord(i, profit * i);
companies.add(rec);
System.out.println(rec);
}
// do whatever you need with the list...
}
}
The output of this small program is:
CompanyRecord[companyID=1, profit=1000.0]
CompanyRecord[companyID=2, profit=2000.0]
CompanyRecord[companyID=3, profit=3000.0]
CompanyRecord[companyID=4, profit=4000.0]
CompanyRecord[companyID=5, profit=5000.0]
CompanyRecord[companyID=6, profit=6000.0]
CompanyRecord[companyID=7, profit=7000.0]
CompanyRecord[companyID=8, profit=8000.0]
CompanyRecord[companyID=9, profit=9000.0]
CompanyRecord[companyID=10, profit=10000.0]
This is probably the simplest way to accomplish what you need. You will need to use Java 14 or later to make use of Java Records, but I recommend you use the latest version.
UPDATE: One important thing to note is that Java records are immutable. So, they have no setters (mutator methods). You will have to set the values through the constructor and values cannot be changed afterwards. You can access (get) the property values by calling a method that has the same name as the field. For example, the getter method for profit is profit(). For example rec.profit().
I am new to Java programming and I had a task in class.
So the main thing was I had to make a public class named BankAccount with these 3: int number, String owner, int amount.
Then I had to make an array of 10 with this class and fill it up (the owner names were Name1, Name2, etc.).
Then I had to make a String array of 5 with 5 names in it (for example: "James", "Jack", etc.)
and then I had to kind of "modify" the owner names so that my program will attach one of the 5 names randomly to the end of the current owner names.
So it will be like this for example: Name2Jack, Name3James, etc.
I successfully did all of this.
But then.
My teacher told me to make another method which will decide, how many names do I have out of the 10 ownernames in which name X is present.
So I did this:
public static int Count(BankAccount[] accounts, String name){
int number=0;
for (i=0; i<accounts.length, i++){
if(accounts[i].owner.contains(name)==true)
number++;
}
return number;
}
At least if I remember correctly, it was this. Or something similar like that.
And this worked as well.
But then, my teacher said, how would I do it with not .contains but with .equals ?
And if I would do it with that, would I need 1 or 2 "=" marks?
I had no idea what she means, like I dont know how to do it with .equals... because the owner names are like Name1Jack for example..
She told me I would need 1 "=" mark instead of 2, and that I should look after this for the next class.
Can you guys actually tell me what she meant with this ".equals" method instead of the .contains one?
How could I do this with .equals , and I dont get it why would I need 1 "=" mark instead of 2 whatsoever.
Any help would be really appreciated!
It appears that you need to just change this one line:
From:
if (accounts[i].owner.contains(name) == true)
To:
if (accounts[i].owner.equals(name))
Here is a sample code with both variations:
public class BankAccountDemo {
public static void main(String[] args) {
BankAccount[] bankAccounts = new BankAccount[5];
bankAccounts[0] = (new BankAccount(1, "Name1James", 1000));
bankAccounts[1] = (new BankAccount(2, "Name2Jack", 2000));
bankAccounts[2] = (new BankAccount(3, "Name3Henry", 3000));
bankAccounts[3] = (new BankAccount(4, "Name4Jack", 4000));
bankAccounts[4] = (new BankAccount(5, "Name5James", 5000));
System.out.println("Check A:");
System.out.println(BankAccountDemo.Count(bankAccounts, "James"));
System.out.println(BankAccountDemo.Count2(bankAccounts, "James"));
System.out.println("Check B:");
System.out.println(BankAccountDemo.Count(bankAccounts, "Name5James"));
System.out.println(BankAccountDemo.Count2(bankAccounts, "Name5James"));
}
public static int Count(BankAccount[] accounts, String name) {
int number = 0;
for (int i = 0; i < accounts.length; i++) {
if (accounts[i].owner.contains(name) == true) {
number++;
}
}
return number;
}
public static int Count2(BankAccount[] accounts, String name) {
int number = 0;
for (int i = 0; i < accounts.length; i++) {
if (accounts[i].owner.equals(name)) {
number++;
}
}
return number;
}
}
Run output is:
Check A:
2
0
Check B:
1
1
I am trying to add Books in my code. Let's say someone wants to add 30 books, the iteration goes from 0 to 30 which is fine. What if he wants to add 10 more books later, then it will simply do nothing useful, since I need them to start from 30 to 40. How can I fix this?
int currentBooks = 0;
do {
System.out.print("How many books would you like to add? ");
int nbBooks = sc.nextInt();
// Add nbBooks amount to inventory array
if (inventory.length-currentBooks >= nbBooks) {
for (int w = 0; w < inventory.length; w++) {
inventory[currentBooks] = new Book();
currentBooks = w;
}
valid = true;
break password;
}
else {
System.out.print("You can only add " + inventory.length + " books.\n");
add = true;
}
} while(add);
The disadvantage of a plain array (Book[] in your case) is that its length cannot be changed. You should use a List (despite the fact that you're not allowed to, for some strange reason).
With the List interface
Therefore, you are better off using the List interface (and an implementation of it, for instance, ArrayList), which uses an array internally, but it automatically extends its internal array if needed, so you don't have to worry about it:
// List is an interface, so we need a certain implementation of that interface
// to use. ArrayList is a good candidate:
List<Book> books = new ArrayList<>();
Now we have created an ArrayList with the initial length of 0. The length can be obtained using the size() method, as opposed to an array's length property.
int nbBooks = sc.nextInt();
for (int i = 0; i < nbBooks; i++) {
books.add(new Book());
}
Without the List interface
However, if you cannot or may not use the List interface, you have a few options, depending on what exacly you want.
One of the options is to create a class which holds your array with Books, and a length as a property, because you have to store the length somewhere:
class BookList {
private Book[] books = new Book[100]; // Or some maximum length
private int size;
public void add(Book book) {
this.books[this.size] = book;
this.size++;
// You could optionally 'extend' the array with System.arraycopy
// when the internal array exceeds 100, but I'll leave that to
// you
}
}
Note that this is virtually a kind of homebrew version of the ArrayList class.
In your case you have defined inventory somewhere. You'll need to introduce inventorySize or something, and each time you add a book, you also increment the inventorySize variable.
Book[] inventory;
int inventorySize;
and your method:
...
System.out.print("How many books would you like to add? ");
int nbBooks = sc.nextInt();
for (int i = 0; i < nbBooks; i++) {
this.inventory[this.inventorySize + i] = new Book();
}
this.inventorySize += nbBooks;
You can also check for the last non-null element (or the first null element) and consider that the length of the array, but that would be very bad code, because, for instance, you have to walk over the array to calculate its length, which might be pretty expensive in performance.
I'm quite new to arrays and methods, and I've been seeing this error recurring through several programs: error '[' expected.
In each occasion, it seems to correct itself as I adjust something else, but in this particular case, I am completely stumped.
By the way, I am using several methods and arrays to create a quiz (before you ask, yes, this is an assignment and I agree, a list is a better way to handle this data - but that is not an option).
It is possible that I am not passing the arrays correctly between methods, as I'm a little muddy on that process. From my understanding, in order to send/receive (i.e. import/export) an array or other variable between methods, I must declare that variable/array in the method header parameters.
import java.util.Scanner;
public class H7pseudo
{
public static void main(String[] args)
{
//call getAnswerkey method
getAnswerkey(answerkey[i]);
//call getAnswers method
getAnswers(answers[i]);
//call passed method? necessary or no?
boolean passed = passed(answerkey[i], answers[i], qMissed[i], points);
//Print results of grading
if (passed)
{
System.out.println("Congratulations! You passed.");
}
else
{
System.out.println("Try again, sucka. You FAILED.");
}
//call totalPoints
totalIncorrect(points);
//call questionsMissed
questionsMissed(qMissed[i]);
}
//get answer key (create answerkey array & export)
public static void getAnswerkey(answerkey[i])
{
//create answerkey array here
char[] answerkey;
//determine number of questions (indices)
answerkey = new char[20];
//input values (correct answers) for each index
//for our purposes today, the answer is always 'c'.
for (int i = 0; i <=20; i++)
{
answerkey[i] = 'c';
}
}
//get student answers (create answers array & export)
public static void getAnswers(answers[i])
{
//initialize scanner for user input
Scanner scan = new Scanner(System.in);
//create answer array here
char[] answers;
//determine number of questions (indices)
answers = new char[20];
//prompt for user input as values of each index
for (int i = 0; i <= 20; i++) {
answers[i] = scan.nextChar();
}
}
//grade student answers (import & compare index values of arrays:answers&answerkey
//create & export qMissed array
public static boolean passed(answerkey[i], answers[i], qMissed[i], points)
{
int points = 0;
//create new array: qMissed
boolean[] qMissed;
//determine number of questions to be graded
qMissed = new boolean[20];
//initialize values for array
for (int i = 0; i <= 20; i++) {
qMissed[i] = false;
}
//cycle through indices of answerkey[i] & answers[i];
for (int i = 0; i =< 20; i++)
{
if (answers[i] == answerkey[i])
{
correct = true;
points = points+1;
qMissed[i] = true;
}
else {
qMissed[i] = false;
}
}
//evaluate whether or not the student passed (15+ correct answers)
if (points >= 15)
{
passed = true;
}
else
{
passed = false;
}
return passed;
}
public static void totalIncorrect(points)
{
int missed = 20 - points;
System.out.println("You missed " + missed + " questions.");
}
public static void questionsMissed(qMissed[i])
{
// for each index of the array qMissed...
for (int i = 0; i < qMissed.length; i++)
{
//...print correct and false answers.
system.out.println(i + ": " + qMissed[i] + "\n");
}
}
}
You can't define array size in the method signature, in Java.
public static void getAnswerkey(answerkey[i])
You can't put anything inside the [] in a method declaration. Also, you have to mention the type:
public static void getAnswerKey(char[] answerkey)
This is not the only reason your code won't work as intended, but I'll leave the rest as part of the exercise.
Look at your method definitions:
public static void questionsMissed(qMissed[i])
This is wrong. You should define the type of the variable and it should not contain [i] like an element of an array. It should be something like this:
public static void questionsMissed(int qMissed)
Or if you want to pass the array, write it like this:
public static void questionsMissed(int[] qMissed)
Apart of this, there are other several errors in your code:
getAnswerkey(answerkey[i]); //answerkey is not defined
getAnswers(answers[i]); //answers is not defined
It would be better if you start reading a Java tutorial first.
I want to vote up Luiggi's answer, but I don't have enough reputation to do that :)
Congrats, cordivia, on getting started with Java!
Here is how an array is declared:
type[] arrayName = new type[numberOfElements]
For example, you did this right in your method definition for getAnswerkey():
char[] answerkey;
answerkey = new char[20];
The part in the method definition inside the parentheses defines the kind of data the method is willing to accept from the outside. So if you don't need to put something into the method to get something out of it, you don't need to put anything in the parentheses. Define the method like this:
getAnswerkey() {
...But that's not the whole story. If you want to get something out of the method, it needs to have a return type as well. A return type is what you're gonna get out of the method when the method's done doing it's magic. For example, if you want to get an int array out of a method you would do something like this:
public static int getTheInteger() {
Since you want an array of chars from the method, you'll want to do something like this:
public static char[] getAnswerkey() {
So that's how you get a method to give you something back. If don't want anything back, you put void:
public static void noMarshmallows() {
Now, when you use the method, you're gonna need to do something with what it gives you, or it did all that work for nothing. So you need to store the return value in a variable when you call the array (calling methods is what you've been doing in main). You know how to store something in a variable. You use the '=' operator:
int myVeryFavoriteNumber;
myVeryFavoriteNumber = 5;
So, you do the same thing when you're getting something out of an array. You assign the return value to a variable. If you want to do this with an array, do this:
int[] myFavs;
myFavs = getMyFavoriteNumbers();
Same with chars:
char[] answerKey;
answerKey = getAnswerKey();
Voila! Your answer key is now right out in the open for the rest of main to see :)
Now, if you want to put something into a method and have it do something with what you put in, you define a parameter. You know how this works. It's just like declaring a variable, and that's exactly what it is. Parameters go in the parentheses and only the method using the parameter sees that variable name (it's local). Something like this:
public static void plusOneToAll (int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i] + 1;
}
}
Notice int[] numbers in the parentheses. The type being passed in is int[] or integer array. numbers is just the parameter name. It functions just like a variable, but it is declared locally (inside the parentheses) and use locally (inside the method). So, if you wanted to compare the answers from two arrays and return the number of matches (like a total score for instance), you would do something like this:
public static int getTotalScore (char[] correctAnswers, char[] userAnswers) {
int totalScore = 0;
for (int i = 0; i < correctAnswers.length; i++) {
if (userAnswers[i] == correctAnswers[i]) {
totalScore = totalScore + 1;
}
}
return totalScore;
}
Notice the return type: int (written before the method name). Inside the array I'm using the variable totalScore to keep track of the number of times the answers match. The method takes two char arrays from the outside and uses them on the inside. Finally, I return an int: totalScore. That kicks the value of totalScore back out to whatever called it (main).
If I might add one last thing: Do yourself a favor and go pick up a copy of Head First Java. It's hard to find a good tutorial online and Java textbooks are just plain boring. The Head First series are kind of like coloring books for adults.
Best of luck!
I'm new to using OOP, I typically just put all my code in a single class and use methods. But I want to maintain state information and think classes are the best fit but I'm having trouble wrapping my head around it.
Say I have a list of items and I want to stop when the total sum of all previous items in the list equals X(in this case 10 so it takes item 1 + 2, then 2+3.etc..until it hits the threshold 10), I can use a method to calculate it but it involves me doing the entire process all over again when all I really need to do is increment by the last item and then see if my data exceeds the threshold. Here's my code so far but I know its not good because although it works its really just using the class as an independent method and recalculating on every loop. My goal is to,using this structure, reduce loops if not necessary to check thresholds.
Any suggestions?
Code:
public class LearningClassesCounter {
public static void main(String[] args) {
int[] list = new int[]{1,2,3,4,5,6,7,8,9,10};
int[] data_list = new int[list.length];
for (int current_location = 0; current_location<list.length;current_location++) {
//can only put commands in here. Nothing above.
Counter checker = new Counter(data_list);
System.out.println(checker.check_data(current_location));
for (int i =0; i<100; i++){
if (checker.check_data(current_location) == false) {
break;
}
data_list[current_location] = (list[current_location]+1); //this is just a random function, it could be any math function I just put it in here to show that some work is being done.
}
}
//its done now lets print the results
for (Integer item : data_list) {
System.out.println(item);
}
}
}
class Counter {
private int[] data_list;
private int total_so_far;
// create a new counter with the given parameters
public Counter(int[] data_list) {
this.data_list = data_list;
this.total_so_far = 0;
}
public boolean check_data(int current_location) {
// TODO Auto-generated method stub
int total_so_far = 0;
//System.out.println(total_so_far);
for (int item : data_list) {
total_so_far = item + total_so_far;
if (total_so_far >= 10) {
break;
}
}
if (total_so_far>=10) {
return false;
} else {
return true;
}
}
}
I don't need anyone to fix my code or anything(I want to do it myself, the code is just to give an idea of what I'm doing). I'm more interested in the flaw in my logic and maybe a way for me to better think about designing classes so I can apply them to my own situations better.
So the solution is that you do not update the data_list directly. Instead have a setter method in the Counter class that takes the index and value to update. It updates the value in the array and also updates a count value.
Something like this:
class Counter{
private final int[] list;
private count = 0;
private final maxCount = 10;
public Counter(int[] list){
this.list = list;
}
public boolean updateValueAndCheckPastMax(int index, int value){
list[index] = value;
count += value;
return count >= maxCount;
}
}
You are way over thinking this, and a counter class is not really necessary in this case.
I'm also interested as to why you'd be doing this line:
data_list[current_location] = (list[current_location]+1);
Do you want your data_list to be the same as list, but each value is incremented by 1?
If you are merely trying to return a sub-array of the values that are < 10, i would suggest just doing this in a for loop, and using an int as a counter.