How to assign value to objects in java












-3















How do I add a value to my ArrayList in java then print out the value to each object.



import java.util.ArrayList;
import java.util.Scanner;

public class exercise {
private ArrayList<String> files;
public void ArrayList() {
a = new ArrayList<>();
}

public void addObject() {
a.add("blue")
a.add("green")
a.add("yellow")
}

public void addValue(String Object) {
for (String filenames : a)
Scanner reader = new Scanner(System.in);

int n = reader.nextInt();
a.set(n)
}
}


I want to try assign values by user input to "blue" , "green" and "yellow" so it can be any value










share|improve this question

























  • Where did you get the ArrayList a from? Where do you declare it?

    – Ishaan Javali
    Nov 21 '18 at 23:27






  • 1





    Firstly, class declarations cannot have parentheses.

    – Logan
    Nov 21 '18 at 23:27











  • private ArrayList<String> a;

    – henchness
    Nov 21 '18 at 23:35











  • First, fix compile errors and format your code properly. Second, it is totally unclear what you are trying to achieve with your code. You can use java ArrayList directly without some weird wrappers on top of it.

    – Sergei Sirik
    Nov 21 '18 at 23:41
















-3















How do I add a value to my ArrayList in java then print out the value to each object.



import java.util.ArrayList;
import java.util.Scanner;

public class exercise {
private ArrayList<String> files;
public void ArrayList() {
a = new ArrayList<>();
}

public void addObject() {
a.add("blue")
a.add("green")
a.add("yellow")
}

public void addValue(String Object) {
for (String filenames : a)
Scanner reader = new Scanner(System.in);

int n = reader.nextInt();
a.set(n)
}
}


I want to try assign values by user input to "blue" , "green" and "yellow" so it can be any value










share|improve this question

























  • Where did you get the ArrayList a from? Where do you declare it?

    – Ishaan Javali
    Nov 21 '18 at 23:27






  • 1





    Firstly, class declarations cannot have parentheses.

    – Logan
    Nov 21 '18 at 23:27











  • private ArrayList<String> a;

    – henchness
    Nov 21 '18 at 23:35











  • First, fix compile errors and format your code properly. Second, it is totally unclear what you are trying to achieve with your code. You can use java ArrayList directly without some weird wrappers on top of it.

    – Sergei Sirik
    Nov 21 '18 at 23:41














-3












-3








-3








How do I add a value to my ArrayList in java then print out the value to each object.



import java.util.ArrayList;
import java.util.Scanner;

public class exercise {
private ArrayList<String> files;
public void ArrayList() {
a = new ArrayList<>();
}

public void addObject() {
a.add("blue")
a.add("green")
a.add("yellow")
}

public void addValue(String Object) {
for (String filenames : a)
Scanner reader = new Scanner(System.in);

int n = reader.nextInt();
a.set(n)
}
}


I want to try assign values by user input to "blue" , "green" and "yellow" so it can be any value










share|improve this question
















How do I add a value to my ArrayList in java then print out the value to each object.



import java.util.ArrayList;
import java.util.Scanner;

public class exercise {
private ArrayList<String> files;
public void ArrayList() {
a = new ArrayList<>();
}

public void addObject() {
a.add("blue")
a.add("green")
a.add("yellow")
}

public void addValue(String Object) {
for (String filenames : a)
Scanner reader = new Scanner(System.in);

int n = reader.nextInt();
a.set(n)
}
}


I want to try assign values by user input to "blue" , "green" and "yellow" so it can be any value







java arraylist java.util.scanner






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 23:36







henchness

















asked Nov 21 '18 at 23:25









henchnesshenchness

36




36













  • Where did you get the ArrayList a from? Where do you declare it?

    – Ishaan Javali
    Nov 21 '18 at 23:27






  • 1





    Firstly, class declarations cannot have parentheses.

    – Logan
    Nov 21 '18 at 23:27











  • private ArrayList<String> a;

    – henchness
    Nov 21 '18 at 23:35











  • First, fix compile errors and format your code properly. Second, it is totally unclear what you are trying to achieve with your code. You can use java ArrayList directly without some weird wrappers on top of it.

    – Sergei Sirik
    Nov 21 '18 at 23:41



















  • Where did you get the ArrayList a from? Where do you declare it?

    – Ishaan Javali
    Nov 21 '18 at 23:27






  • 1





    Firstly, class declarations cannot have parentheses.

    – Logan
    Nov 21 '18 at 23:27











  • private ArrayList<String> a;

    – henchness
    Nov 21 '18 at 23:35











  • First, fix compile errors and format your code properly. Second, it is totally unclear what you are trying to achieve with your code. You can use java ArrayList directly without some weird wrappers on top of it.

    – Sergei Sirik
    Nov 21 '18 at 23:41

















Where did you get the ArrayList a from? Where do you declare it?

– Ishaan Javali
Nov 21 '18 at 23:27





Where did you get the ArrayList a from? Where do you declare it?

– Ishaan Javali
Nov 21 '18 at 23:27




1




1





Firstly, class declarations cannot have parentheses.

– Logan
Nov 21 '18 at 23:27





Firstly, class declarations cannot have parentheses.

– Logan
Nov 21 '18 at 23:27













private ArrayList<String> a;

– henchness
Nov 21 '18 at 23:35





private ArrayList<String> a;

– henchness
Nov 21 '18 at 23:35













First, fix compile errors and format your code properly. Second, it is totally unclear what you are trying to achieve with your code. You can use java ArrayList directly without some weird wrappers on top of it.

– Sergei Sirik
Nov 21 '18 at 23:41





First, fix compile errors and format your code properly. Second, it is totally unclear what you are trying to achieve with your code. You can use java ArrayList directly without some weird wrappers on top of it.

– Sergei Sirik
Nov 21 '18 at 23:41












1 Answer
1






active

oldest

votes


















0














Edit:



You want to take user's input and if it's a predefined color, like "blue", then add a predefined number for that color, like 3, to a list? Right?



If that is the case then this code takes user's input from the console, checks if that input is a predefined color and then stores a matching number in the ArrayList.



When the user enters 'stop' then the program prints only the numbers in the list and then terminates.



import java.util.ArrayList;

public class Exercise {

private static ArrayList<Integer> list = new ArrayList<Integer>(); // You can initialize your ArrayList here as well

public static void main(String args) {

String userInput = null;
int number = 0;

do{
// Read inputs as long as user did not entered 'stop'
userInput = readUserInput();
number = createNumberFromColor(userInput);
// add the number to the list if it's not 0
if (number!=0) {
list.add(number);
}
}while (!userInput.equals("stop"));

// If you want afterwards you can print the list elements
System.out.println("The list contains:");
for (int i=0; i<list.size(); i++) {
System.out.print(""+list.get(i)+" ");
}
System.out.println();
}

private static String readUserInput(){
// This is an easier way to read the user input from the console
// than using the Scanner class, which needs to be closed etc...
return System.console().readLine();
}

private static int createNumberFromColor(String input){
switch (input) {

// Add your own cases here, for example case "red": return 1, etc...

case "blue":
return 5;

case "yellow":
return 3;

// If the input was not a known color then return 0
default:
return 0;
}
}
}





share|improve this answer


























  • Careful the class here is named Exercise, not exercice.

    – Themelis
    Nov 22 '18 at 0:01











  • Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

    – henchness
    Nov 22 '18 at 0:08











  • So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

    – Themelis
    Nov 22 '18 at 0:11











  • yes thats exactly what i mean

    – henchness
    Nov 22 '18 at 0:17











  • @henchness Check the new code snippet and tell me if that's what you've wanted.

    – Themelis
    Nov 22 '18 at 0:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421850%2fhow-to-assign-value-to-objects-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Edit:



You want to take user's input and if it's a predefined color, like "blue", then add a predefined number for that color, like 3, to a list? Right?



If that is the case then this code takes user's input from the console, checks if that input is a predefined color and then stores a matching number in the ArrayList.



When the user enters 'stop' then the program prints only the numbers in the list and then terminates.



import java.util.ArrayList;

public class Exercise {

private static ArrayList<Integer> list = new ArrayList<Integer>(); // You can initialize your ArrayList here as well

public static void main(String args) {

String userInput = null;
int number = 0;

do{
// Read inputs as long as user did not entered 'stop'
userInput = readUserInput();
number = createNumberFromColor(userInput);
// add the number to the list if it's not 0
if (number!=0) {
list.add(number);
}
}while (!userInput.equals("stop"));

// If you want afterwards you can print the list elements
System.out.println("The list contains:");
for (int i=0; i<list.size(); i++) {
System.out.print(""+list.get(i)+" ");
}
System.out.println();
}

private static String readUserInput(){
// This is an easier way to read the user input from the console
// than using the Scanner class, which needs to be closed etc...
return System.console().readLine();
}

private static int createNumberFromColor(String input){
switch (input) {

// Add your own cases here, for example case "red": return 1, etc...

case "blue":
return 5;

case "yellow":
return 3;

// If the input was not a known color then return 0
default:
return 0;
}
}
}





share|improve this answer


























  • Careful the class here is named Exercise, not exercice.

    – Themelis
    Nov 22 '18 at 0:01











  • Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

    – henchness
    Nov 22 '18 at 0:08











  • So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

    – Themelis
    Nov 22 '18 at 0:11











  • yes thats exactly what i mean

    – henchness
    Nov 22 '18 at 0:17











  • @henchness Check the new code snippet and tell me if that's what you've wanted.

    – Themelis
    Nov 22 '18 at 0:29


















0














Edit:



You want to take user's input and if it's a predefined color, like "blue", then add a predefined number for that color, like 3, to a list? Right?



If that is the case then this code takes user's input from the console, checks if that input is a predefined color and then stores a matching number in the ArrayList.



When the user enters 'stop' then the program prints only the numbers in the list and then terminates.



import java.util.ArrayList;

public class Exercise {

private static ArrayList<Integer> list = new ArrayList<Integer>(); // You can initialize your ArrayList here as well

public static void main(String args) {

String userInput = null;
int number = 0;

do{
// Read inputs as long as user did not entered 'stop'
userInput = readUserInput();
number = createNumberFromColor(userInput);
// add the number to the list if it's not 0
if (number!=0) {
list.add(number);
}
}while (!userInput.equals("stop"));

// If you want afterwards you can print the list elements
System.out.println("The list contains:");
for (int i=0; i<list.size(); i++) {
System.out.print(""+list.get(i)+" ");
}
System.out.println();
}

private static String readUserInput(){
// This is an easier way to read the user input from the console
// than using the Scanner class, which needs to be closed etc...
return System.console().readLine();
}

private static int createNumberFromColor(String input){
switch (input) {

// Add your own cases here, for example case "red": return 1, etc...

case "blue":
return 5;

case "yellow":
return 3;

// If the input was not a known color then return 0
default:
return 0;
}
}
}





share|improve this answer


























  • Careful the class here is named Exercise, not exercice.

    – Themelis
    Nov 22 '18 at 0:01











  • Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

    – henchness
    Nov 22 '18 at 0:08











  • So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

    – Themelis
    Nov 22 '18 at 0:11











  • yes thats exactly what i mean

    – henchness
    Nov 22 '18 at 0:17











  • @henchness Check the new code snippet and tell me if that's what you've wanted.

    – Themelis
    Nov 22 '18 at 0:29
















0












0








0







Edit:



You want to take user's input and if it's a predefined color, like "blue", then add a predefined number for that color, like 3, to a list? Right?



If that is the case then this code takes user's input from the console, checks if that input is a predefined color and then stores a matching number in the ArrayList.



When the user enters 'stop' then the program prints only the numbers in the list and then terminates.



import java.util.ArrayList;

public class Exercise {

private static ArrayList<Integer> list = new ArrayList<Integer>(); // You can initialize your ArrayList here as well

public static void main(String args) {

String userInput = null;
int number = 0;

do{
// Read inputs as long as user did not entered 'stop'
userInput = readUserInput();
number = createNumberFromColor(userInput);
// add the number to the list if it's not 0
if (number!=0) {
list.add(number);
}
}while (!userInput.equals("stop"));

// If you want afterwards you can print the list elements
System.out.println("The list contains:");
for (int i=0; i<list.size(); i++) {
System.out.print(""+list.get(i)+" ");
}
System.out.println();
}

private static String readUserInput(){
// This is an easier way to read the user input from the console
// than using the Scanner class, which needs to be closed etc...
return System.console().readLine();
}

private static int createNumberFromColor(String input){
switch (input) {

// Add your own cases here, for example case "red": return 1, etc...

case "blue":
return 5;

case "yellow":
return 3;

// If the input was not a known color then return 0
default:
return 0;
}
}
}





share|improve this answer















Edit:



You want to take user's input and if it's a predefined color, like "blue", then add a predefined number for that color, like 3, to a list? Right?



If that is the case then this code takes user's input from the console, checks if that input is a predefined color and then stores a matching number in the ArrayList.



When the user enters 'stop' then the program prints only the numbers in the list and then terminates.



import java.util.ArrayList;

public class Exercise {

private static ArrayList<Integer> list = new ArrayList<Integer>(); // You can initialize your ArrayList here as well

public static void main(String args) {

String userInput = null;
int number = 0;

do{
// Read inputs as long as user did not entered 'stop'
userInput = readUserInput();
number = createNumberFromColor(userInput);
// add the number to the list if it's not 0
if (number!=0) {
list.add(number);
}
}while (!userInput.equals("stop"));

// If you want afterwards you can print the list elements
System.out.println("The list contains:");
for (int i=0; i<list.size(); i++) {
System.out.print(""+list.get(i)+" ");
}
System.out.println();
}

private static String readUserInput(){
// This is an easier way to read the user input from the console
// than using the Scanner class, which needs to be closed etc...
return System.console().readLine();
}

private static int createNumberFromColor(String input){
switch (input) {

// Add your own cases here, for example case "red": return 1, etc...

case "blue":
return 5;

case "yellow":
return 3;

// If the input was not a known color then return 0
default:
return 0;
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 0:28

























answered Nov 21 '18 at 23:58









ThemelisThemelis

755517




755517













  • Careful the class here is named Exercise, not exercice.

    – Themelis
    Nov 22 '18 at 0:01











  • Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

    – henchness
    Nov 22 '18 at 0:08











  • So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

    – Themelis
    Nov 22 '18 at 0:11











  • yes thats exactly what i mean

    – henchness
    Nov 22 '18 at 0:17











  • @henchness Check the new code snippet and tell me if that's what you've wanted.

    – Themelis
    Nov 22 '18 at 0:29





















  • Careful the class here is named Exercise, not exercice.

    – Themelis
    Nov 22 '18 at 0:01











  • Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

    – henchness
    Nov 22 '18 at 0:08











  • So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

    – Themelis
    Nov 22 '18 at 0:11











  • yes thats exactly what i mean

    – henchness
    Nov 22 '18 at 0:17











  • @henchness Check the new code snippet and tell me if that's what you've wanted.

    – Themelis
    Nov 22 '18 at 0:29



















Careful the class here is named Exercise, not exercice.

– Themelis
Nov 22 '18 at 0:01





Careful the class here is named Exercise, not exercice.

– Themelis
Nov 22 '18 at 0:01













Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

– henchness
Nov 22 '18 at 0:08





Im trying to add a value to the previous items in the ArrayList such as blue = 5 or yellow = 3 instead of terminated the items in it

– henchness
Nov 22 '18 at 0:08













So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

– Themelis
Nov 22 '18 at 0:11





So if the user inserts 5 you will assign "blue", if he inserts 3 you will assign "yellow" etc?

– Themelis
Nov 22 '18 at 0:11













yes thats exactly what i mean

– henchness
Nov 22 '18 at 0:17





yes thats exactly what i mean

– henchness
Nov 22 '18 at 0:17













@henchness Check the new code snippet and tell me if that's what you've wanted.

– Themelis
Nov 22 '18 at 0:29







@henchness Check the new code snippet and tell me if that's what you've wanted.

– Themelis
Nov 22 '18 at 0:29






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421850%2fhow-to-assign-value-to-objects-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$