Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this printNumbers method i need it to print out the integers in the numbers list. And im not sure what im doing wrong.

What I have tried:

Java
<pre>public class WrapperExample
{
    private Scanner scanner;
    private ArrayList<Integer> numbers;

    /**
     * Constructor for objects of class WrapperExample.
     */
    public WrapperExample()
    {
        scanner = new Scanner(System.in);
        numbers = new ArrayList<>();
    }

    /**
     * Read integer from standard input (System.in)
     * and store them in the list. The end of the
     * input is indicated by a negative integer.
     * The negative integer must not be stored in the list.
     */
    public void readNumbers()
    {
        int i = scanner.nextInt();
        // Get rid of any previous numbers.
        numbers.clear();
        // Read a fresh set of integers ...
        while (i >= 0){
            numbers.add(i);
            i = scanner.nextInt();
        }
    }

    /**
     * Print out the integers in the numbers list.
     * Print the numbers on a single line, with a single space
     * between each.
     */
    public void printNumbers()
    {
        // Print out the numbers.

        // Terminate the line.
        for(Integer interger: numbers){
            System.out.println(scanner.nextInt());
        }

    }   
}
Posted
Updated 2-Dec-22 4:30am

Simple: don't use println which - as the name suggests - prints your text followed by a new line. Use print instead, and add a separator (space, comma, whatever you want) between the numbers. If you add a "separator string" variable to your method and start with it empty, print it before each number, and then set it to your actual separator string it will work really nicely.
 
Share this answer
 
Comments
Freddie Francis 2-Dec-22 11:14am    
would this be correct

public void printNumbers()
{
System.out.print (numbers+"");
}
OriginalGriff 2-Dec-22 12:08pm    
Did you read what I said?
Quote:
Java
for(Integer interger: numbers){
    System.out.println(scanner.nextInt());
}
Read that code again: for each number in the numbers list, you ask the user to enter a new number, and then print that new number.

Based on your description, you need to print the number the user has already entered instead.

If only there was an interger variable you could use to get that number...
 
Share this answer
 
Java
for(Integer interger: numbers){
    System.out.println(scanner.nextInt()); // print on a single line

The println method prints whatever data you pass to it, followed by a line end. If you want to write multiple items on the same line then use one of the print or printf methods instead. See PrintStream (Java Platform SE 7 )[^] for full details.

Also, why are you calling scanner.nextInt() in your printNumbers method? You should be printing the interger variable defined in the loop control.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900