8 Replies - 784 Views - Last Post: 15 September 2013 - 10:09 AM Rate Topic: -----

#1 Pannkakan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-July 13

Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 08:56 AM

Hello,

Can somebody just quickly tell me how to print out how many numbers a user entered? the values are stored in an array.

Ive managed to do the part where a user enters numbers from 0-9.
Then i also managed to display which numbers he entered : Lets say : you entered : 5436
But im stuck on how to display : Amount of numbers entered : 4

i have understood that i have to count the entered values somehow and probably with a for loop, but im not sure how to do it exactly. I just need a small hint.
Shall i use indexOf ? Any kind of help appriciated

Is This A Good Question/Topic? 0
  • +

Replies To: Print amount of numbers entered by user using a for loop?

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 09:00 AM

Show your code that declares the array, then collects and stores the numbers.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 09:04 AM

Increment a counter with each item that you read.
Was This Post Helpful? 0
  • +
  • -

#4 Pannkakan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-July 13

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 09:04 AM

Ok, but it isnt really finished. I thought i´d just ask you quickly in here without taking up too much of your time. :-)

Right now i can do the input and display the numbers i have input (which is what i want) but need to display how many numbers were entered by user also.


import java.util.Scanner;

public class NummerAnalys {

  /*Program som ska ta emot användar input i heltal och lagras i en array.
   * När användaren är klar, genom att trycka enter skrivs 
   * - Alla inmatade nummer ut ex : 1,5,7,6,6,4,4,9,0,1
   * - Totala inmatade tal : 10 st
   * - Det lägsta inmatade talet : 0
   * - Det högsta inmatade talet : 9
   */
    public static void main(String[] args) {
    
       int[] array = new int[10];
       int min = 0;;
       int max = 9;
       int antal = 0;
       //user input skriv in siffror
       Scanner s = new Scanner (System.in);
       
            for(int i= 0; i<10; i++)
     {
    System.out.println("Ange nummer(mellan 0-9)");
   array[i] = s.nextInt(); 
   
    {
       
   System.out.println("Du valde dessa nummer " + array[i]);

     }


}
        
   

Was This Post Helpful? 0
  • +
  • -

#5 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 09:24 AM

Υοu prompt the user to enter 10 numbers ..he has no option ,the numbers entered by user will always be 10 .

This post has been edited by ChrisNt: 15 September 2013 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6545
  • View blog
  • Posts: 26,533
  • Joined: 12-December 12

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 09:35 AM

The question needs more explanation. Currently, the user can only enter 10, and only 10, integer numbers. Perhaps you want to count the number of digits entered? But what if someone enters 009?

This post has been edited by andrewsw: 15 September 2013 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 Pannkakan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-July 13

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 09:56 AM

But we were told to have a maximum amount of number, so i chose 10, so that the user can put any amount of numbers. I tried the program and doesnt seem to have any problems with that... I dont want them to be able to put a 100 numbers. just maximum 10.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11041
  • View blog
  • Posts: 18,857
  • Joined: 19-March 11

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 10:04 AM

So there is a requirements gathering phase that you need to deal with - you need to specify what you want your program to do. Even for a simple homework assignment, this is an important step. You can't build it if you don't know what it's supposed to be.

So picture the finished program in your head: what happens when the user runs this program?

They enter numbers, okay. How many? Is it a fixed number, or does the user decide how many? When do they decide? Is it before they start, or can they just quit? If the latter, how do they just quit?


Then you can start making design decisions, like "when should I create the array that holds the user's integers?" and "should I use a counter to track the number of integers entered?"
Was This Post Helpful? 0
  • +
  • -

#9 Pannkakan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-July 13

Re: Print amount of numbers entered by user using a for loop?

Posted 15 September 2013 - 10:09 AM

Ok well i tried that, to write a sort of a pseudo code. I was told by an other user here to break down the "problem" into smaller chunks. Lets hope i figure it out soon enough :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1