Java is printing last number using array method, by ignoring other numbers










-1














I'm trying to let my code print numbers I put in output but using array method.



package pkg11;

import java.util.Scanner;

public class Main

public static void main(String args)
Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

for (int z = 1; z <= b; z++)
System.out.println("Input your" + " " + z + " " + "number");
x = in.nextInt();


System.out.println();
int a = new int[x];;

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);





The problem is when it's printing it only prints the last value, for example, I put that I want to put 3 numbers, the first was 1 the second was 2 the third was 3, it prints the third without putting the first 2.










share|improve this question























  • Are you expecting 1 variable to hold 3 values?
    – shmosel
    Nov 9 at 22:04










  • @shmosel in array, one variable hold many values right?
    – Raphael Eid
    Nov 9 at 22:05






  • 1




    You're not even populating the array.
    – shmosel
    Nov 9 at 22:05










  • @shmosel but sir I don't want to initialize the array, I way it to automatically do it when I ask the user how many numbers he wants.
    – Raphael Eid
    Nov 9 at 22:08










  • Right. And I see no attempt to do so.
    – shmosel
    Nov 9 at 22:10















-1














I'm trying to let my code print numbers I put in output but using array method.



package pkg11;

import java.util.Scanner;

public class Main

public static void main(String args)
Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

for (int z = 1; z <= b; z++)
System.out.println("Input your" + " " + z + " " + "number");
x = in.nextInt();


System.out.println();
int a = new int[x];;

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);





The problem is when it's printing it only prints the last value, for example, I put that I want to put 3 numbers, the first was 1 the second was 2 the third was 3, it prints the third without putting the first 2.










share|improve this question























  • Are you expecting 1 variable to hold 3 values?
    – shmosel
    Nov 9 at 22:04










  • @shmosel in array, one variable hold many values right?
    – Raphael Eid
    Nov 9 at 22:05






  • 1




    You're not even populating the array.
    – shmosel
    Nov 9 at 22:05










  • @shmosel but sir I don't want to initialize the array, I way it to automatically do it when I ask the user how many numbers he wants.
    – Raphael Eid
    Nov 9 at 22:08










  • Right. And I see no attempt to do so.
    – shmosel
    Nov 9 at 22:10













-1












-1








-1







I'm trying to let my code print numbers I put in output but using array method.



package pkg11;

import java.util.Scanner;

public class Main

public static void main(String args)
Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

for (int z = 1; z <= b; z++)
System.out.println("Input your" + " " + z + " " + "number");
x = in.nextInt();


System.out.println();
int a = new int[x];;

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);





The problem is when it's printing it only prints the last value, for example, I put that I want to put 3 numbers, the first was 1 the second was 2 the third was 3, it prints the third without putting the first 2.










share|improve this question















I'm trying to let my code print numbers I put in output but using array method.



package pkg11;

import java.util.Scanner;

public class Main

public static void main(String args)
Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

for (int z = 1; z <= b; z++)
System.out.println("Input your" + " " + z + " " + "number");
x = in.nextInt();


System.out.println();
int a = new int[x];;

for (int i = 0; i < a.length; i++)
System.out.println(a[i]);





The problem is when it's printing it only prints the last value, for example, I put that I want to put 3 numbers, the first was 1 the second was 2 the third was 3, it prints the third without putting the first 2.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 22:06









Ivar

2,693113040




2,693113040










asked Nov 9 at 22:03









Raphael Eid

14




14











  • Are you expecting 1 variable to hold 3 values?
    – shmosel
    Nov 9 at 22:04










  • @shmosel in array, one variable hold many values right?
    – Raphael Eid
    Nov 9 at 22:05






  • 1




    You're not even populating the array.
    – shmosel
    Nov 9 at 22:05










  • @shmosel but sir I don't want to initialize the array, I way it to automatically do it when I ask the user how many numbers he wants.
    – Raphael Eid
    Nov 9 at 22:08










  • Right. And I see no attempt to do so.
    – shmosel
    Nov 9 at 22:10
















  • Are you expecting 1 variable to hold 3 values?
    – shmosel
    Nov 9 at 22:04










  • @shmosel in array, one variable hold many values right?
    – Raphael Eid
    Nov 9 at 22:05






  • 1




    You're not even populating the array.
    – shmosel
    Nov 9 at 22:05










  • @shmosel but sir I don't want to initialize the array, I way it to automatically do it when I ask the user how many numbers he wants.
    – Raphael Eid
    Nov 9 at 22:08










  • Right. And I see no attempt to do so.
    – shmosel
    Nov 9 at 22:10















Are you expecting 1 variable to hold 3 values?
– shmosel
Nov 9 at 22:04




Are you expecting 1 variable to hold 3 values?
– shmosel
Nov 9 at 22:04












@shmosel in array, one variable hold many values right?
– Raphael Eid
Nov 9 at 22:05




@shmosel in array, one variable hold many values right?
– Raphael Eid
Nov 9 at 22:05




1




1




You're not even populating the array.
– shmosel
Nov 9 at 22:05




You're not even populating the array.
– shmosel
Nov 9 at 22:05












@shmosel but sir I don't want to initialize the array, I way it to automatically do it when I ask the user how many numbers he wants.
– Raphael Eid
Nov 9 at 22:08




@shmosel but sir I don't want to initialize the array, I way it to automatically do it when I ask the user how many numbers he wants.
– Raphael Eid
Nov 9 at 22:08












Right. And I see no attempt to do so.
– shmosel
Nov 9 at 22:10




Right. And I see no attempt to do so.
– shmosel
Nov 9 at 22:10












1 Answer
1






active

oldest

votes


















3














Have a close look at the following code fragment of yours and try to spot the error:



for (int z = 1; z <= b ; z++) 
System.out.println("Input your" +" " +z +" " +"number");
x = in.nextInt();


// here you create the array
int a = new int [x];


If you didnt spot it: You create the array you want to save each integer in after you have read all values from the console. There is no way you can store the users input in the array, since it is not known at that time.



Then, what did you actually do?



You used the same variable x all the time x = in.nextInt();, overriding each input.



What can i do to solve the problem?



Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

int a = new int[b];

for (int z = 0; z < b; z++)
System.out.println("Input your" + " " + (z + 1) + " " + "number");
a[z] = in.nextInt();


for (int i = 0; i < a.length; i++)
System.out.println(a[i]);



First, declare int a = new int[b]; before you read the values and assign each input the the array with a[z] = in.nextInt();. Also, i modified your loop index a little bit to make things easier.



Ok, what else can i do?



Apart from the user entering non numbers, this code is a little bit more bullet-proof! If you are looking for even more, you can use in.nextLine() and Integer.valueOf() to prevent the user from entering strings instead of numbers.



Scanner in = new Scanner(System.in);

int amountOfNumers;
System.out.println("How many number do you want to put? Amount: ");

amountOfNumers = in.nextInt();
while (amountOfNumers < 1)
System.out.println("Please enter a number greater than one:");
amountOfNumers = in.nextInt();


int numbers = new int[amountOfNumers];

for (int i = 0; i < amountOfNumers; i++)
System.out.println("Input your " + (i + 1) + " number: ");
numbers[i] = in.nextInt();


System.out.println("Your numbers are:");
Arrays.stream(numbers).forEach(System.out::println);





share|improve this answer






















  • Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
    – Raphael Eid
    Nov 9 at 22:23










  • Glad i could help!
    – Glains
    Nov 9 at 22:25






  • 2




    RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
    – PeS
    Nov 10 at 0:14










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%2f53233823%2fjava-is-printing-last-number-using-array-method-by-ignoring-other-numbers%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









3














Have a close look at the following code fragment of yours and try to spot the error:



for (int z = 1; z <= b ; z++) 
System.out.println("Input your" +" " +z +" " +"number");
x = in.nextInt();


// here you create the array
int a = new int [x];


If you didnt spot it: You create the array you want to save each integer in after you have read all values from the console. There is no way you can store the users input in the array, since it is not known at that time.



Then, what did you actually do?



You used the same variable x all the time x = in.nextInt();, overriding each input.



What can i do to solve the problem?



Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

int a = new int[b];

for (int z = 0; z < b; z++)
System.out.println("Input your" + " " + (z + 1) + " " + "number");
a[z] = in.nextInt();


for (int i = 0; i < a.length; i++)
System.out.println(a[i]);



First, declare int a = new int[b]; before you read the values and assign each input the the array with a[z] = in.nextInt();. Also, i modified your loop index a little bit to make things easier.



Ok, what else can i do?



Apart from the user entering non numbers, this code is a little bit more bullet-proof! If you are looking for even more, you can use in.nextLine() and Integer.valueOf() to prevent the user from entering strings instead of numbers.



Scanner in = new Scanner(System.in);

int amountOfNumers;
System.out.println("How many number do you want to put? Amount: ");

amountOfNumers = in.nextInt();
while (amountOfNumers < 1)
System.out.println("Please enter a number greater than one:");
amountOfNumers = in.nextInt();


int numbers = new int[amountOfNumers];

for (int i = 0; i < amountOfNumers; i++)
System.out.println("Input your " + (i + 1) + " number: ");
numbers[i] = in.nextInt();


System.out.println("Your numbers are:");
Arrays.stream(numbers).forEach(System.out::println);





share|improve this answer






















  • Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
    – Raphael Eid
    Nov 9 at 22:23










  • Glad i could help!
    – Glains
    Nov 9 at 22:25






  • 2




    RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
    – PeS
    Nov 10 at 0:14















3














Have a close look at the following code fragment of yours and try to spot the error:



for (int z = 1; z <= b ; z++) 
System.out.println("Input your" +" " +z +" " +"number");
x = in.nextInt();


// here you create the array
int a = new int [x];


If you didnt spot it: You create the array you want to save each integer in after you have read all values from the console. There is no way you can store the users input in the array, since it is not known at that time.



Then, what did you actually do?



You used the same variable x all the time x = in.nextInt();, overriding each input.



What can i do to solve the problem?



Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

int a = new int[b];

for (int z = 0; z < b; z++)
System.out.println("Input your" + " " + (z + 1) + " " + "number");
a[z] = in.nextInt();


for (int i = 0; i < a.length; i++)
System.out.println(a[i]);



First, declare int a = new int[b]; before you read the values and assign each input the the array with a[z] = in.nextInt();. Also, i modified your loop index a little bit to make things easier.



Ok, what else can i do?



Apart from the user entering non numbers, this code is a little bit more bullet-proof! If you are looking for even more, you can use in.nextLine() and Integer.valueOf() to prevent the user from entering strings instead of numbers.



Scanner in = new Scanner(System.in);

int amountOfNumers;
System.out.println("How many number do you want to put? Amount: ");

amountOfNumers = in.nextInt();
while (amountOfNumers < 1)
System.out.println("Please enter a number greater than one:");
amountOfNumers = in.nextInt();


int numbers = new int[amountOfNumers];

for (int i = 0; i < amountOfNumers; i++)
System.out.println("Input your " + (i + 1) + " number: ");
numbers[i] = in.nextInt();


System.out.println("Your numbers are:");
Arrays.stream(numbers).forEach(System.out::println);





share|improve this answer






















  • Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
    – Raphael Eid
    Nov 9 at 22:23










  • Glad i could help!
    – Glains
    Nov 9 at 22:25






  • 2




    RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
    – PeS
    Nov 10 at 0:14













3












3








3






Have a close look at the following code fragment of yours and try to spot the error:



for (int z = 1; z <= b ; z++) 
System.out.println("Input your" +" " +z +" " +"number");
x = in.nextInt();


// here you create the array
int a = new int [x];


If you didnt spot it: You create the array you want to save each integer in after you have read all values from the console. There is no way you can store the users input in the array, since it is not known at that time.



Then, what did you actually do?



You used the same variable x all the time x = in.nextInt();, overriding each input.



What can i do to solve the problem?



Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

int a = new int[b];

for (int z = 0; z < b; z++)
System.out.println("Input your" + " " + (z + 1) + " " + "number");
a[z] = in.nextInt();


for (int i = 0; i < a.length; i++)
System.out.println(a[i]);



First, declare int a = new int[b]; before you read the values and assign each input the the array with a[z] = in.nextInt();. Also, i modified your loop index a little bit to make things easier.



Ok, what else can i do?



Apart from the user entering non numbers, this code is a little bit more bullet-proof! If you are looking for even more, you can use in.nextLine() and Integer.valueOf() to prevent the user from entering strings instead of numbers.



Scanner in = new Scanner(System.in);

int amountOfNumers;
System.out.println("How many number do you want to put? Amount: ");

amountOfNumers = in.nextInt();
while (amountOfNumers < 1)
System.out.println("Please enter a number greater than one:");
amountOfNumers = in.nextInt();


int numbers = new int[amountOfNumers];

for (int i = 0; i < amountOfNumers; i++)
System.out.println("Input your " + (i + 1) + " number: ");
numbers[i] = in.nextInt();


System.out.println("Your numbers are:");
Arrays.stream(numbers).forEach(System.out::println);





share|improve this answer














Have a close look at the following code fragment of yours and try to spot the error:



for (int z = 1; z <= b ; z++) 
System.out.println("Input your" +" " +z +" " +"number");
x = in.nextInt();


// here you create the array
int a = new int [x];


If you didnt spot it: You create the array you want to save each integer in after you have read all values from the console. There is no way you can store the users input in the array, since it is not known at that time.



Then, what did you actually do?



You used the same variable x all the time x = in.nextInt();, overriding each input.



What can i do to solve the problem?



Scanner in = new Scanner(System.in);
int x = 0;

System.out.println("How many number do you want to put?");
int b = in.nextInt();

int a = new int[b];

for (int z = 0; z < b; z++)
System.out.println("Input your" + " " + (z + 1) + " " + "number");
a[z] = in.nextInt();


for (int i = 0; i < a.length; i++)
System.out.println(a[i]);



First, declare int a = new int[b]; before you read the values and assign each input the the array with a[z] = in.nextInt();. Also, i modified your loop index a little bit to make things easier.



Ok, what else can i do?



Apart from the user entering non numbers, this code is a little bit more bullet-proof! If you are looking for even more, you can use in.nextLine() and Integer.valueOf() to prevent the user from entering strings instead of numbers.



Scanner in = new Scanner(System.in);

int amountOfNumers;
System.out.println("How many number do you want to put? Amount: ");

amountOfNumers = in.nextInt();
while (amountOfNumers < 1)
System.out.println("Please enter a number greater than one:");
amountOfNumers = in.nextInt();


int numbers = new int[amountOfNumers];

for (int i = 0; i < amountOfNumers; i++)
System.out.println("Input your " + (i + 1) + " number: ");
numbers[i] = in.nextInt();


System.out.println("Your numbers are:");
Arrays.stream(numbers).forEach(System.out::println);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 20:42

























answered Nov 9 at 22:14









Glains

865517




865517











  • Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
    – Raphael Eid
    Nov 9 at 22:23










  • Glad i could help!
    – Glains
    Nov 9 at 22:25






  • 2




    RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
    – PeS
    Nov 10 at 0:14
















  • Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
    – Raphael Eid
    Nov 9 at 22:23










  • Glad i could help!
    – Glains
    Nov 9 at 22:25






  • 2




    RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
    – PeS
    Nov 10 at 0:14















Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
– Raphael Eid
Nov 9 at 22:23




Thank you sir for your help, you solved my problem, I understood that I asked the user to put new numbers while numbers aren't saved in an array.
– Raphael Eid
Nov 9 at 22:23












Glad i could help!
– Glains
Nov 9 at 22:25




Glad i could help!
– Glains
Nov 9 at 22:25




2




2




RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
– PeS
Nov 10 at 0:14




RaphaelEid - Welcome to SO. Make sure you visit tour. If @Glains answer helped you, mark that as accepted, please. It will eventually help others who find this question in future.
– PeS
Nov 10 at 0:14

















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53233823%2fjava-is-printing-last-number-using-array-method-by-ignoring-other-numbers%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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?