This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 7 years ago.
I am writing a program that the main method asks the user for how many numbers they want in an array, then asks them to input that many numbers, and stores them in initialArray. In another method reverseTheArray, a NEW array is created that stores the elements of initialArray in reverse order. The method is called in main and prints the reversed array. No matter what input, the string "[I#33909752" is printed at the end. What is this and how do I get rid of it?
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers do you want in your array?: ");
int num = scan.nextInt();
int[] initialArray = new int[num];
System.out.println("Please enter your numbers into the array: ");
for(int i=0; i<num; i++){
initialArray[i] = scan.nextInt();
}
System.out.println(reverseTheArray(initialArray));
}
public static int[] reverseTheArray (int[] initialArray){
int[] reversedArray = new int[initialArray.length];
for(int j=0; j<reversedArray.length; j++){
reversedArray[j] = initialArray[initialArray.length-1-j];
System.out.print(reversedArray[j]+" ");
}
return reversedArray;
}
}
You're seeing the default toString() from an array. To see the array items themselves you could use the java.util.Arrays toString(...) method like so:
System.out.println(java.util.Arrays.toString(reverseTheArray(initialArray)));
Related
This question already has answers here:
How to append elements at the end of ArrayList in Java?
(4 answers)
Closed last year.
public void getDisMarks()
{
marks=new int[3];
System.out.print("Enter marks of Physics: ");
marks[0]=sc.nextInt();
System.out.print("Enter marks of Chemistry: ");
marks[1]=sc.nextInt();
System.out.print("Enter marks of Maths: ");
marks[2]=sc.nextInt();
}
So in this piece of code we are using array for 3 definite subjects. And we're using scanner class to input from the user. Let's say in the future I want to add a couple of more subject. So coding it again would not make it any flexible.
So I read that we could use arrayList, How can I use scanner class with arrayList similar to this piece of code.
You could do something like this:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
System.out.println("How many marks to enter?");
int marksToEnter = sc.nextInt();
for (int i = 0; i < marksToEnter; i++) {
System.out.println("Enter next mark");
list.add(sc.nextInt());
}
System.out.println(list);
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was trying to take input for the number of names to be stored in an array from user and then using that i was taking names from the user ,first i tried to take names from the user using next() method and all the things were fine but when i tried to take input using nextLine() method the output was as shown below
package learningJava;
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
Output for the nextLine() method
Enter the number of names you are gonna enter
5
Enter the name of friend 1
Enter the name of friend 2
It is not prompting me to enter the name of friend 1 and directly skipping it and coming to the friend 2 line.
I am beginner in Java , i know the basic difference in next and nextLine() that next() doesn't take input after a space but nextLine() takes complete input , So what is happening here ??
just in for loop, just change "println" to "print" because nextLine() consumes new line character.
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
check this answer: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?
This question already has answers here:
Get specific ArrayList item
(8 answers)
Closed 7 years ago.
The code is trying to take String entries, add them to an array and then stop when no text is written (user just presses enter). Then it is meant to display all of the String items in the array thus far on new lines.
The error in my title is coming up on my if query, and I'm additionally getting error's reading the value 'x' in the for loop as a variable (cannot find symbol).
Can anyone help me out
import java.util.Scanner;
import java.util.ArrayList;
public class FirstPart {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ArrayList<String> tillEmpty = new ArrayList<String>();
int i = 0;
while (true) {
System.out.print("Type a word: ");
tillEmpty.add(reader.nextLine());
if (tillEmpty[i].isEmpty()) {
break;
} else {
i++;
}
}
System.out.println("You typed the following words: ");
for (x = 0; x < tillEmpty.size; x++){
System.out.println(tillEmpty.get(x));
}
}
}
The error message is telling you exactly what is wrong. You've got an ArrayList and are trying to treat it as if it were an array. It isn't, and you can't use array indices, [i] on it. Instead use the get(...) method as any tutorial will tell you (and which I strongly recommend that you read -- Google can help you find one).
Your tillEmpty is not an array but an arraylist...you have to use tillEmpty.get(i).isEmpty instead of tillEmpty[i]
You missed int in your for loop:
for (int x = 0; x < tillEmpty.size; x++){
System.out.println(tillEmpty.get(x));
}
You are accessing list as Array, use below code, this should work:
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ArrayList<String> tillEmpty = new ArrayList<String>();
int i = 0;
while (true) {
System.out.print("Type a word: ");
tillEmpty.add(reader.nextLine());
if (tillEmpty.isEmpty()) {
break;
} else {
i++;
}
}
System.out.println("You typed the following words: ");
for (int x = 0; x < tillEmpty.size(); x++){
System.out.println(tillEmpty.get(x));
}
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I am trying to create a simple String Revert program that does the following:
Prompts the user for an integer n
Creates an array of n Strings
Keeps reading character strings from user and stores them in the array, until the end of the array or user types "quit"
Prints the strings from the array in reverse order excluding empty slots
Here is my attempt so far:
-However, when i take input and make the size 4, the buffer only reads 3 strings and stops rather than 4.
import java.util.*;
import java.io.*;
class StringRevert {
public static void main(String[] args) {
String myArray[];
Scanner Scan = new Scanner(System.in);
System.out.println("Enter Number: ");
int size = Scan.nextInt();
myArray = new String[size];
for(int i=0; i<myArray.length; i++) {
myArray[i] = Scan.nextLine();
}
}
}
You need to put Scan.nextLine(); before starting of for loop.
public static void main(String[] args) {
String myArray[];
Scanner Scan = new Scanner(System.in);
System.out.println("Enter Number: ");
int size = Scan.nextInt();
myArray = new String[size];
Scan.nextLine();
for(int i=0; i<myArray.length; i++) {
System.out.println("Enter String");
myArray[i] = Scan.nextLine();
}
}
I'm just trying to read the input integers from the user for example
2 2 1 1 1 < as a whole
In the debugger, it works places each integer but when
the resulting array is printed something like [I#19eda2c is printed.
public static void main(String[] args) {
int count=0;
int[] array = new int[10];
String input;
Scanner scan = new Scanner(System.in);
System.out.println("Enter up to 10 integers: ");
while(scan.hasNextInt()){
array[count] = scan.nextInt();
count++;
}
System.out.println(array);
}
}
I understand now that it needs to be printed with a for loop or toString method
but I realized when I run the code,
the program waits for me even though the user inputs the integers
is my scanner logistics incorrect?
You need to use java.util.Arrays.toString() method for a 1D array .
or
java.util.Arrays.deepToString() for multi-dimensional arrays.
Your program is fine except that it will read 12 integers if user enters 12 numbers. Your loop needs to run from 0 to 9 to read 10 numbers and not as long as there are tokens in the input
Here is how Arrays.toString() works:
Returns a string representation of the contents of the specified
array. The string representation consists of a list of the array's
elements, enclosed in square brackets ("[]"). Adjacent elements are
separated by the characters ", " (a comma followed by a space).
Elements are converted to strings as by String.valueOf(int)
Here is how Arrays.deepToString() works:
Returns a string representation of the "deep contents" of the
specified array. If the array contains other arrays as elements, the
string representation contains their contents and so on. This method
is designed for converting multidimensional arrays to strings.
For more, read the docs: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
You can't print the elements of the array just by System.out.println(array);. Iterate through the array and print each element in the array.
for(int index=0; index < count; index++ )
System.out.println(array[index]);
You are just printing textual representation of array object, use for loop to iterate in array and to display its content.
public static void main(String[] args) {
int count = 0;
int[] array = new int[10];
String input;
Scanner scan = new Scanner(System.in);
System.out.println("Enter up to 10 integers: ");
while (scan.hasNextInt()) {
array[count] = scan.nextInt();
count++;
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
System.out.println(array); // prints value from default toString() method
// implementation (e.g. 'className + '#' + hashCode' in Java Oracle)
You should use something like
System.out.println(Arrays.toString(array));
to print values.
Better is to use List instead of array
public static void main(String[] args)
{
List<Integer> array = new ArrayList<Integer>();
String input;
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt())
{
array.add(scan.nextInt());
}
System.out.println("Count=" + array.size());
System.out.println(array);
}