Java is printing last number using array method, by ignoring other numbers
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
java
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
|
show 1 more comment
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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);
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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