Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:33:10+00:00 2026-06-16T05:33:10+00:00

Ran into a little problem and I’m not entirely sure why this code won’t

  • 0

Ran into a little problem and I’m not entirely sure why this code won’t work.

I have a 2d arraylist:

List<List<String>> matrix = new ArrayList<List<String>>();

I have a button that adds an arraylist to the matrix based on user input. But before user input is added, I need the button to search for if that string already exists.
The code I have doesn’t produce any errors, but it doesn’t seam to discriminate between existing and none existing strings except for the very first element. It adds everything the user puts in regardless of it’s existence. Also the code will only function if the matrix array already has some elements in it, if the matrix is empty, the code won’t work at all. What am I doing wrong?

   String name = NameTXT.getText(); 
   String amount = CountTXT.getText();

   for (int i = 0; i < matrix.size(); i ++){
     String search = matrix.get(i).get(0);
     if (name.equals(search)){
         OutputTXT.setText("Item already exists");
         break;
     } else {
         List<String> col = new ArrayList<String>();
         col.add(name);
         col.add(amount);
         matrix.add(col);
         OutputTXT.setText(amount +" "+ name +" added");
         break;
     }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:33:11+00:00Added an answer on June 16, 2026 at 5:33 am

    separate searching for item and item insertion:

    String name = NameTXT.getText(); 
    String amount = CountTXT.getText();
    
    // searching for product
    boolean isNewItem = true;
    
    for (int i = 0; i < matrix.size(); i++){
        String search = matrix.get(i).get(0);
    
        if (name.equals(search)){
            OutputTXT.setText("Item already exists");
            isNewItem = false;
            break;
        } 
    }
    
    // insert if new item 
    if ( isNewItem ) {
        List<String> col = new ArrayList<String>();
        col.add(name);
        col.add(amount);
        matrix.add(col);
        OutputTXT.setText(amount +" "+ name +" added");
    }
    

    actually, it would be better to design in abstract way and use the language tools to solve your problem, for example the list would be easier to work if its List of item:

    class Item {
        private String name;
        private String amount;
    
        public Item(String name, String amount) {
            this.name = name;
            this.amount = amount;
        }
    
        public String getName() { 
            return this.name;
        }
    
        public String getAmount() {
            return this.amount;
        }
    
        @Override
        public boolean equals(Object other) {
            if ( other == this )
                return true;
    
            if ( other == null )
                return false;
    
            if ( other.getClass() != this.getClass() )
                return false;
    
            Item otherItem = (Item) other;
            return otherItem.getName().equals(this.name);
        }
    }
    

    then the list would be:

    List<Item> items = new ArrayList<Item>();
    

    when you need to search:

    Item item = new Item(NameTXT.getText(), CountTXT.getText());
    
    if ( !items.contains(item) ) {
       items.add(item);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a little problem and need some help: If I have an
I ran into a little problem I have a has_many through relationship here is
I ran into a little problem. I've created a code that creates a Excel
I ran into a little problem. I am creating a web app with code
I ran into something a little odd this morning and thought I'd submit it
I ran into a little problem today when I was creating a really quick
I ran into this quite often stated problem but even after looking up nearly
I ran into a little problem today that I can't seem to solve in
I ran into a little problem that seems to be a pain in the
I am new to socket programming and ran into a puzzling problem: I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.