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 8672149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:07:47+00:00 2026-06-12T19:07:47+00:00

I’m having a problem of storing multiple input value in Java. First, I’m using

  • 0

I’m having a problem of storing multiple input value in Java. First, I’m using an ArrayList to store how many inputs the user has entered. Then I want to do the calculation after collect all the input.

My program allows user to enter 5 different values between 1 to 5.
1 is equal to 100,
2 is equal to 200,
3 is equal to 300,
4 is equal to 400,
5 is equal to 500

I create an Array to store these values

double numberArray[] = {100, 200, 300, 400, 500};

When user enters 1, the ArrayList will store the first input value.
When user enters 2, the ArrayList will store the second input value and so on.
When user hit “n”, it will exit and do the addition. It means it will add 100 and 200 together which the output will equal to 300.

However, the problem is when the user keeps input the number, my program will only add up the total of the first input together which is 100 + 100 even I enter 2 as the second input.

Here is my code:

import java.util.*;

public class Total{

   static int total;
   public static void main(String[] args) {

    int numberArray[] = {100, 200, 300, 400, 500};

    List<String> list = new ArrayList<String>();
    Scanner input = new Scanner(System.in);

    do{
        System.out.println("Add item? Please enter \"y\" or \"n\"");
        if (input.next().startsWith("y")){
            System.out.println("Enter item number: ");
            list.add(input.next());
            if (list.contains("1")){
                int item1 = numberArray[0];
                total = total + item1;
            } else if(list.contains("2")){
                int item2 = numberArray[1];
                total = total + item2;
            } else if(list.contains("3")){
                int item3 = numberArray[2];
                    total = total + item3;
            } else if(list.contains("4")){
                    int item4 = numberArray[3];
                total = total + item4;
            } else {
            System.out.println("You have entered invalid item number!");
            break;
            }               
        }else{
            System.out.println("You have entered all the item(s).");
            break;
        }       
     } while(true);
             System.out.println(The total is: " + total);
     }
}
  • 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-12T19:07:48+00:00Added an answer on June 12, 2026 at 7:07 pm

    IF conditions should be modified to get the expected output like below

    if (list.get(list.size() -1).contains("1"))
    

    See full code with all errors fixed:

        import java.util.*; 
    
    public class Total{ 
    
       static int total; 
       public static void main(String[] args) { 
    
        int numberArray[] = {100, 200, 300, 400, 500}; 
    
        List<String> list = new ArrayList<String>(); 
        Scanner input = new Scanner(System.in); 
    
        do{ 
            System.out.println("Add item? Please enter \"y\" or \"n\""); 
            if (input.next().startsWith("y")){ 
                System.out.println("Enter item number: "); 
                String temp = input.next(); //store the user inputed element
                list.add(temp); //add that element in list
                //System.out.println(input.next()); 
    
                //Now use the temporary variable in if conditions
                if (temp.contains("1")){ 
                    int item1 = numberArray[0]; 
                    total = total + item1; 
                } else if(temp.contains("2")){ 
                    int item2 = numberArray[1]; 
                    total = total + item2; 
                } else if(temp.contains("3")){ 
                    int item3 = numberArray[2]; 
                        total = total + item3; 
                } else if(temp.contains("4")){ 
                        int item4 = numberArray[3]; 
                    total = total + item4; 
                } else { 
                System.out.println("You have entered invalid item number!"); 
                break; 
                }                
            }else{ 
                System.out.println("You have entered all the item(s)."); 
                break; 
            }        
         } while(true);
    
            System.out.println("Total is: " + total);
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I am reading a book about Javascript and jQuery and using one of the

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.