Input in an array with no dimension without array list
I want to create a program that reads int values from the user until a value that is not an int is introduced. Then i want to get how many numbers are equal.
I tried this code
import java.util.Scanner;
public class Equals {
public static void main(String args){
Scanner keyboard = new Scanner(System.in);
Scanner input = keyboard;
int index = 0;
int equals = 0;
while(keyboard.hasNextInt()){
keyboard.nextInt();
index++;
}
int equals = new int[index];
for(int i = 0 ; i < index ; i++){
int aux = input.nextInt();
values[i] = aux;
for(int b = 0 ; b < index ; b++){
if(aux == values[b]){
equals++;
}
}
}
System.out.print(equals);
}
}
This code doesnt work because the keyboard scanner only gets the number of values introduced by the user and i use that for the array size but i cant get each individual value to compare. I cant use array lists.
java arrays input size
add a comment |
I want to create a program that reads int values from the user until a value that is not an int is introduced. Then i want to get how many numbers are equal.
I tried this code
import java.util.Scanner;
public class Equals {
public static void main(String args){
Scanner keyboard = new Scanner(System.in);
Scanner input = keyboard;
int index = 0;
int equals = 0;
while(keyboard.hasNextInt()){
keyboard.nextInt();
index++;
}
int equals = new int[index];
for(int i = 0 ; i < index ; i++){
int aux = input.nextInt();
values[i] = aux;
for(int b = 0 ; b < index ; b++){
if(aux == values[b]){
equals++;
}
}
}
System.out.print(equals);
}
}
This code doesnt work because the keyboard scanner only gets the number of values introduced by the user and i use that for the array size but i cant get each individual value to compare. I cant use array lists.
java arrays input size
Why not ask user how many numbers they plan to enter, initialize anArray
of that size and then fill it with user input and check to see how many are duplicates
– GBlodgett
Nov 15 '18 at 22:00
this a problem to do in school and they dont want us to ask the total
– JASOS
Nov 15 '18 at 22:01
Well a hint: Unless you want the user to input everything twice, you cannot have a loop to count how many times the user inputs an int, and then another loop to assign the input to an array
– GBlodgett
Nov 15 '18 at 22:10
Yes i got that wrong, that is asking to input values again, i was trying to find a way to get the values from the first input
– JASOS
Nov 15 '18 at 22:14
add a comment |
I want to create a program that reads int values from the user until a value that is not an int is introduced. Then i want to get how many numbers are equal.
I tried this code
import java.util.Scanner;
public class Equals {
public static void main(String args){
Scanner keyboard = new Scanner(System.in);
Scanner input = keyboard;
int index = 0;
int equals = 0;
while(keyboard.hasNextInt()){
keyboard.nextInt();
index++;
}
int equals = new int[index];
for(int i = 0 ; i < index ; i++){
int aux = input.nextInt();
values[i] = aux;
for(int b = 0 ; b < index ; b++){
if(aux == values[b]){
equals++;
}
}
}
System.out.print(equals);
}
}
This code doesnt work because the keyboard scanner only gets the number of values introduced by the user and i use that for the array size but i cant get each individual value to compare. I cant use array lists.
java arrays input size
I want to create a program that reads int values from the user until a value that is not an int is introduced. Then i want to get how many numbers are equal.
I tried this code
import java.util.Scanner;
public class Equals {
public static void main(String args){
Scanner keyboard = new Scanner(System.in);
Scanner input = keyboard;
int index = 0;
int equals = 0;
while(keyboard.hasNextInt()){
keyboard.nextInt();
index++;
}
int equals = new int[index];
for(int i = 0 ; i < index ; i++){
int aux = input.nextInt();
values[i] = aux;
for(int b = 0 ; b < index ; b++){
if(aux == values[b]){
equals++;
}
}
}
System.out.print(equals);
}
}
This code doesnt work because the keyboard scanner only gets the number of values introduced by the user and i use that for the array size but i cant get each individual value to compare. I cant use array lists.
java arrays input size
java arrays input size
asked Nov 15 '18 at 21:57
JASOSJASOS
11
11
Why not ask user how many numbers they plan to enter, initialize anArray
of that size and then fill it with user input and check to see how many are duplicates
– GBlodgett
Nov 15 '18 at 22:00
this a problem to do in school and they dont want us to ask the total
– JASOS
Nov 15 '18 at 22:01
Well a hint: Unless you want the user to input everything twice, you cannot have a loop to count how many times the user inputs an int, and then another loop to assign the input to an array
– GBlodgett
Nov 15 '18 at 22:10
Yes i got that wrong, that is asking to input values again, i was trying to find a way to get the values from the first input
– JASOS
Nov 15 '18 at 22:14
add a comment |
Why not ask user how many numbers they plan to enter, initialize anArray
of that size and then fill it with user input and check to see how many are duplicates
– GBlodgett
Nov 15 '18 at 22:00
this a problem to do in school and they dont want us to ask the total
– JASOS
Nov 15 '18 at 22:01
Well a hint: Unless you want the user to input everything twice, you cannot have a loop to count how many times the user inputs an int, and then another loop to assign the input to an array
– GBlodgett
Nov 15 '18 at 22:10
Yes i got that wrong, that is asking to input values again, i was trying to find a way to get the values from the first input
– JASOS
Nov 15 '18 at 22:14
Why not ask user how many numbers they plan to enter, initialize an
Array
of that size and then fill it with user input and check to see how many are duplicates– GBlodgett
Nov 15 '18 at 22:00
Why not ask user how many numbers they plan to enter, initialize an
Array
of that size and then fill it with user input and check to see how many are duplicates– GBlodgett
Nov 15 '18 at 22:00
this a problem to do in school and they dont want us to ask the total
– JASOS
Nov 15 '18 at 22:01
this a problem to do in school and they dont want us to ask the total
– JASOS
Nov 15 '18 at 22:01
Well a hint: Unless you want the user to input everything twice, you cannot have a loop to count how many times the user inputs an int, and then another loop to assign the input to an array
– GBlodgett
Nov 15 '18 at 22:10
Well a hint: Unless you want the user to input everything twice, you cannot have a loop to count how many times the user inputs an int, and then another loop to assign the input to an array
– GBlodgett
Nov 15 '18 at 22:10
Yes i got that wrong, that is asking to input values again, i was trying to find a way to get the values from the first input
– JASOS
Nov 15 '18 at 22:14
Yes i got that wrong, that is asking to input values again, i was trying to find a way to get the values from the first input
– JASOS
Nov 15 '18 at 22:14
add a comment |
1 Answer
1
active
oldest
votes
If you can't use array lists, what can you use? Can you use this?
int values = new int[0];
while(keyboard.hasNextInt()){
values = Arrays.copyOf(values, values.length + 1);
values[values.length-1] = keyboard.nextInt();
index++;
}
And if you aren't allowed to do the arrays class, you can resize the array manually like this, and use this function instead of Arrays.copyOf:
private int resize(ints, int capacity) {
int copy = new int[capacity];
for (int i = 0; i < s.length; i++) {
copy[i] = s[i];
s = copy;
}
return copy;
}
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
This will copy a newArray
every time a user inputs anint
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size
– GBlodgett
Nov 15 '18 at 22:18
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
|
show 2 more comments
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%2f53328473%2finput-in-an-array-with-no-dimension-without-array-list%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
If you can't use array lists, what can you use? Can you use this?
int values = new int[0];
while(keyboard.hasNextInt()){
values = Arrays.copyOf(values, values.length + 1);
values[values.length-1] = keyboard.nextInt();
index++;
}
And if you aren't allowed to do the arrays class, you can resize the array manually like this, and use this function instead of Arrays.copyOf:
private int resize(ints, int capacity) {
int copy = new int[capacity];
for (int i = 0; i < s.length; i++) {
copy[i] = s[i];
s = copy;
}
return copy;
}
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
This will copy a newArray
every time a user inputs anint
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size
– GBlodgett
Nov 15 '18 at 22:18
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
|
show 2 more comments
If you can't use array lists, what can you use? Can you use this?
int values = new int[0];
while(keyboard.hasNextInt()){
values = Arrays.copyOf(values, values.length + 1);
values[values.length-1] = keyboard.nextInt();
index++;
}
And if you aren't allowed to do the arrays class, you can resize the array manually like this, and use this function instead of Arrays.copyOf:
private int resize(ints, int capacity) {
int copy = new int[capacity];
for (int i = 0; i < s.length; i++) {
copy[i] = s[i];
s = copy;
}
return copy;
}
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
This will copy a newArray
every time a user inputs anint
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size
– GBlodgett
Nov 15 '18 at 22:18
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
|
show 2 more comments
If you can't use array lists, what can you use? Can you use this?
int values = new int[0];
while(keyboard.hasNextInt()){
values = Arrays.copyOf(values, values.length + 1);
values[values.length-1] = keyboard.nextInt();
index++;
}
And if you aren't allowed to do the arrays class, you can resize the array manually like this, and use this function instead of Arrays.copyOf:
private int resize(ints, int capacity) {
int copy = new int[capacity];
for (int i = 0; i < s.length; i++) {
copy[i] = s[i];
s = copy;
}
return copy;
}
If you can't use array lists, what can you use? Can you use this?
int values = new int[0];
while(keyboard.hasNextInt()){
values = Arrays.copyOf(values, values.length + 1);
values[values.length-1] = keyboard.nextInt();
index++;
}
And if you aren't allowed to do the arrays class, you can resize the array manually like this, and use this function instead of Arrays.copyOf:
private int resize(ints, int capacity) {
int copy = new int[capacity];
for (int i = 0; i < s.length; i++) {
copy[i] = s[i];
s = copy;
}
return copy;
}
edited Nov 15 '18 at 22:17
answered Nov 15 '18 at 22:09
JustinJustin
1,0431512
1,0431512
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
This will copy a newArray
every time a user inputs anint
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size
– GBlodgett
Nov 15 '18 at 22:18
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
|
show 2 more comments
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
This will copy a newArray
every time a user inputs anint
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size
– GBlodgett
Nov 15 '18 at 22:18
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
it says i cant use methods from the classes Arrays, Integers, and Math from the package java.lang
– JASOS
Nov 15 '18 at 22:17
This will copy a new
Array
every time a user inputs an int
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size– GBlodgett
Nov 15 '18 at 22:18
This will copy a new
Array
every time a user inputs an int
. Wouldn't it be better to create a copy of the array that is bigger than just one slot bigger than the last? And then once input is done, trim it down to the size– GBlodgett
Nov 15 '18 at 22:18
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
@JASOS I edited with a solution that doesn't use Arrays
– Justin
Nov 15 '18 at 22:19
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
they also dont let me create another method besides the method main
– JASOS
Nov 15 '18 at 22:25
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
im doing this program with arrays i dont know if i can do it without using an array but i dont think it would be possible
– JASOS
Nov 15 '18 at 22:26
|
show 2 more comments
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%2f53328473%2finput-in-an-array-with-no-dimension-without-array-list%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
Why not ask user how many numbers they plan to enter, initialize an
Array
of that size and then fill it with user input and check to see how many are duplicates– GBlodgett
Nov 15 '18 at 22:00
this a problem to do in school and they dont want us to ask the total
– JASOS
Nov 15 '18 at 22:01
Well a hint: Unless you want the user to input everything twice, you cannot have a loop to count how many times the user inputs an int, and then another loop to assign the input to an array
– GBlodgett
Nov 15 '18 at 22:10
Yes i got that wrong, that is asking to input values again, i was trying to find a way to get the values from the first input
– JASOS
Nov 15 '18 at 22:14