The first position of my array isn't being modified
I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit(String bar)
Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
For example, when entering 1, 0, 0, 0, 0 into the array, the output is
"1||:::||:::||:::||:::"
When entering 0, 1, 1, 1, 1 into the array, the output is
"0:::||:::||:::||:::||"
Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?
java arrays for-loop zipcode
add a comment |
I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit(String bar)
Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
For example, when entering 1, 0, 0, 0, 0 into the array, the output is
"1||:::||:::||:::||:::"
When entering 0, 1, 1, 1, 1 into the array, the output is
"0:::||:::||:::||:::||"
Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?
java arrays for-loop zipcode
1
Because the initial value ofbar
is retained when you usebar = bar + ...
– Jacob G.
Nov 14 '18 at 21:00
add a comment |
I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit(String bar)
Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
For example, when entering 1, 0, 0, 0, 0 into the array, the output is
"1||:::||:::||:::||:::"
When entering 0, 1, 1, 1, 1 into the array, the output is
"0:::||:::||:::||:::||"
Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?
java arrays for-loop zipcode
I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit(String bar)
Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
For example, when entering 1, 0, 0, 0, 0 into the array, the output is
"1||:::||:::||:::||:::"
When entering 0, 1, 1, 1, 1 into the array, the output is
"0:::||:::||:::||:::||"
Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?
java arrays for-loop zipcode
java arrays for-loop zipcode
asked Nov 14 '18 at 20:56
ddalcantoddalcanto
254
254
1
Because the initial value ofbar
is retained when you usebar = bar + ...
– Jacob G.
Nov 14 '18 at 21:00
add a comment |
1
Because the initial value ofbar
is retained when you usebar = bar + ...
– Jacob G.
Nov 14 '18 at 21:00
1
1
Because the initial value of
bar
is retained when you use bar = bar + ...
– Jacob G.
Nov 14 '18 at 21:00
Because the initial value of
bar
is retained when you use bar = bar + ...
– Jacob G.
Nov 14 '18 at 21:00
add a comment |
3 Answers
3
active
oldest
votes
It is because of s.nextLine()
it will read the first int entered in console and save it in String bar
passing into checkDigit()
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
Pass an empty string
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(""));
Input : 0 1 1 1
Output : ||::::::||:::||:::||
add a comment |
There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.
- The 1st input is displayed as it is because you are appending the updated values to the
bar
string which is nothing but the first accepted character because of thes.nextLine()
in themain()
function. Hence, see point number 2. - There's no need to send the
s.nextLine()
as argument to the function call ofcheckDigit()
. Also remove thestring bar
as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc. - Your
input
array is of size 4, but you are looping from0 to 4
, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. =>input = new int[5];
Here is the updated working code along with the associated output.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit()
Scanner s = new Scanner(System.in);
String bar = "";
int input;
input = new int [5];
for(int p = 0; p < 5; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit());
OUTPUT:
1
1
1
1
1
:::||:::||:::||:::||:::||
Another input-output pair:
0
1
1
0
1
||::::::||:::||||::::::||
add a comment |
You are creating and reading from a Scanner object in main() so the first digit goes into the bar
parameter sent to the checkDigit
method.
I would remove the bar parameter altogether and also make some other changes.
public static String checkDigit()
StringBuilder bar = new StringBuilder();
Scanner s = new Scanner(System.in);
int input = 0;
for(int p = 0; p < 5; p++)
input = s.nextInt();
if (input == 0)
if (input == 1) ");
return bar.toString();
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%2f53308591%2fthe-first-position-of-my-array-isnt-being-modified%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is because of s.nextLine()
it will read the first int entered in console and save it in String bar
passing into checkDigit()
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
Pass an empty string
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(""));
Input : 0 1 1 1
Output : ||::::::||:::||:::||
add a comment |
It is because of s.nextLine()
it will read the first int entered in console and save it in String bar
passing into checkDigit()
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
Pass an empty string
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(""));
Input : 0 1 1 1
Output : ||::::::||:::||:::||
add a comment |
It is because of s.nextLine()
it will read the first int entered in console and save it in String bar
passing into checkDigit()
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
Pass an empty string
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(""));
Input : 0 1 1 1
Output : ||::::::||:::||:::||
It is because of s.nextLine()
it will read the first int entered in console and save it in String bar
passing into checkDigit()
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));
Pass an empty string
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(""));
Input : 0 1 1 1
Output : ||::::::||:::||:::||
answered Nov 14 '18 at 21:02
DeadpoolDeadpool
7,3572829
7,3572829
add a comment |
add a comment |
There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.
- The 1st input is displayed as it is because you are appending the updated values to the
bar
string which is nothing but the first accepted character because of thes.nextLine()
in themain()
function. Hence, see point number 2. - There's no need to send the
s.nextLine()
as argument to the function call ofcheckDigit()
. Also remove thestring bar
as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc. - Your
input
array is of size 4, but you are looping from0 to 4
, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. =>input = new int[5];
Here is the updated working code along with the associated output.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit()
Scanner s = new Scanner(System.in);
String bar = "";
int input;
input = new int [5];
for(int p = 0; p < 5; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit());
OUTPUT:
1
1
1
1
1
:::||:::||:::||:::||:::||
Another input-output pair:
0
1
1
0
1
||::::::||:::||||::::::||
add a comment |
There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.
- The 1st input is displayed as it is because you are appending the updated values to the
bar
string which is nothing but the first accepted character because of thes.nextLine()
in themain()
function. Hence, see point number 2. - There's no need to send the
s.nextLine()
as argument to the function call ofcheckDigit()
. Also remove thestring bar
as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc. - Your
input
array is of size 4, but you are looping from0 to 4
, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. =>input = new int[5];
Here is the updated working code along with the associated output.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit()
Scanner s = new Scanner(System.in);
String bar = "";
int input;
input = new int [5];
for(int p = 0; p < 5; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit());
OUTPUT:
1
1
1
1
1
:::||:::||:::||:::||:::||
Another input-output pair:
0
1
1
0
1
||::::::||:::||||::::::||
add a comment |
There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.
- The 1st input is displayed as it is because you are appending the updated values to the
bar
string which is nothing but the first accepted character because of thes.nextLine()
in themain()
function. Hence, see point number 2. - There's no need to send the
s.nextLine()
as argument to the function call ofcheckDigit()
. Also remove thestring bar
as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc. - Your
input
array is of size 4, but you are looping from0 to 4
, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. =>input = new int[5];
Here is the updated working code along with the associated output.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit()
Scanner s = new Scanner(System.in);
String bar = "";
int input;
input = new int [5];
for(int p = 0; p < 5; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit());
OUTPUT:
1
1
1
1
1
:::||:::||:::||:::||:::||
Another input-output pair:
0
1
1
0
1
||::::::||:::||||::::::||
There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.
- The 1st input is displayed as it is because you are appending the updated values to the
bar
string which is nothing but the first accepted character because of thes.nextLine()
in themain()
function. Hence, see point number 2. - There's no need to send the
s.nextLine()
as argument to the function call ofcheckDigit()
. Also remove thestring bar
as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc. - Your
input
array is of size 4, but you are looping from0 to 4
, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. =>input = new int[5];
Here is the updated working code along with the associated output.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Zip
public static String checkDigit()
Scanner s = new Scanner(System.in);
String bar = "";
int input;
input = new int [5];
for(int p = 0; p < 5; p++)
input[p] = s.nextInt();
if (input[p] == 0)
:::";
if (input[p] == 1)
System.out.println();
return bar;
public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit());
OUTPUT:
1
1
1
1
1
:::||:::||:::||:::||:::||
Another input-output pair:
0
1
1
0
1
||::::::||:::||||::::::||
answered Nov 15 '18 at 5:21
RaiRai
1,0263822
1,0263822
add a comment |
add a comment |
You are creating and reading from a Scanner object in main() so the first digit goes into the bar
parameter sent to the checkDigit
method.
I would remove the bar parameter altogether and also make some other changes.
public static String checkDigit()
StringBuilder bar = new StringBuilder();
Scanner s = new Scanner(System.in);
int input = 0;
for(int p = 0; p < 5; p++)
input = s.nextInt();
if (input == 0)
if (input == 1) ");
return bar.toString();
add a comment |
You are creating and reading from a Scanner object in main() so the first digit goes into the bar
parameter sent to the checkDigit
method.
I would remove the bar parameter altogether and also make some other changes.
public static String checkDigit()
StringBuilder bar = new StringBuilder();
Scanner s = new Scanner(System.in);
int input = 0;
for(int p = 0; p < 5; p++)
input = s.nextInt();
if (input == 0)
if (input == 1) ");
return bar.toString();
add a comment |
You are creating and reading from a Scanner object in main() so the first digit goes into the bar
parameter sent to the checkDigit
method.
I would remove the bar parameter altogether and also make some other changes.
public static String checkDigit()
StringBuilder bar = new StringBuilder();
Scanner s = new Scanner(System.in);
int input = 0;
for(int p = 0; p < 5; p++)
input = s.nextInt();
if (input == 0)
if (input == 1) ");
return bar.toString();
You are creating and reading from a Scanner object in main() so the first digit goes into the bar
parameter sent to the checkDigit
method.
I would remove the bar parameter altogether and also make some other changes.
public static String checkDigit()
StringBuilder bar = new StringBuilder();
Scanner s = new Scanner(System.in);
int input = 0;
for(int p = 0; p < 5; p++)
input = s.nextInt();
if (input == 0)
if (input == 1) ");
return bar.toString();
edited Nov 14 '18 at 21:06
answered Nov 14 '18 at 21:01
Joakim DanielsonJoakim Danielson
10.2k3725
10.2k3725
add a comment |
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.
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%2f53308591%2fthe-first-position-of-my-array-isnt-being-modified%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
1
Because the initial value of
bar
is retained when you usebar = bar + ...
– Jacob G.
Nov 14 '18 at 21:00