Java method: prints statement number of times entered by user

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2202 Views - Last Post: 05 July 2011 - 11:04 PM Rate Topic: -----

#1 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Java method: prints statement number of times entered by user

Posted 02 July 2011 - 12:37 PM

I'm not sure whats wrong with my code, but it won't even run. Something wrong with " myNumEntry = input.nextInt();" I don't understand why. The point is to print the statement the number of times that a user enters. Will those program even do what it's supposed to do, and any thoughts on what's wrong?

public class PrintStatement
{
       public static int posNum(int x)
    {
        
        while (x <=0)
        {
        System.out.println("Enter a number greater than 0");
        }
        
        return x;
    }
    public static void main(String[] args)
    {
        int myNumEntry, howAreYou; // variable declaration 
        Scanner input= new Scanner( System. in); 
        System.out.println("Enter an integer number : "); //prompt user for input
        myNumEntry = input.nextInt();
        howAreYou = posNum(myNumEntry); // pass input to method getPos
        for (int i = 0; i  <= howAreYou; i++)
        System.out.println("How Are You?" );
    }
}


Is This A Good Question/Topic? 2
  • +

Replies To: Java method: prints statement number of times entered by user

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 12:45 PM

When you say something "won't run" or "doesn't work," be sure to include details including any error messages you're receiving or what it's doing/not doing that you think it shouldn't/should be doing.

In this case, you need simply to add an import statement. Can you figure out what import statement is required?

This post has been edited by GregBrannon: 02 July 2011 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 harmy01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 179
  • Joined: 15-June 10

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 01:14 PM

Also consider changing your for loop variable "i" to 1. Because if the user enters 5, The output will be "How Are You?" 6 times.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 01:21 PM

and you do not change the value of x inside the while() in posNum
this while will loop for ever if ever it is entered (if x <= 0)
Was This Post Helpful? 1
  • +
  • -

#5 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 02:11 PM

Whoops I had the import and forget to put it first time. I'm still getting an error: java.utilNoSuchElementException null (in java.util Scanner). Any thoughts?

import java.util.*; 

public class printStatement
{
       public static int posNum(int x)
    {
        
        if (x <=0)
        {
        System.out.println("Enter a number greater than 0");
        }
        
        return x;
    }
    public static void main(String[] args)
    {
        int myNumEntry, howAreYou; 
        Scanner input= new Scanner( System. in); 
        System.out.println("Enter an integer number : "); for input
        myNumEntry = input.nextInt();
        howAreYou = getPos(myNumEntry); 
        for (int i = 1; i  <= helloThere; i++)
        System.out.println("HowAreYou" );
    }
}

Was This Post Helpful? -1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 02:13 PM

Your posted code won't even compile

System.out.println("Enter an integer number : "); for input
getPos() is not defined
neither helloThere

This post has been edited by pbl: 02 July 2011 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 02:25 PM

Check line 19 in the latest version. Compare it to line 17 in the original version. Something changed that shouldn't have.
Was This Post Helpful? 2
  • +
  • -

#8 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 02:31 PM

So sorry. I was changing names around and got mixed up. This is what I have so far, but I still get that error message:

import java.util.*; 

public class PrintStatement
{
       public static int getPos(int x)
    {
        
        if (x <=0)
        {
        System.out.println("Enter a number greater than 0");
        }
        
        return x;
    }
    public static void main(String[] args)
    {
        int myNumEntry, helloThere; 
        Scanner input= new Scanner( System. in); 
        System.out.println("Enter an integer number : "); 
        myNumEntry = input.nextInt();
        helloThere = getPos(myNumEntry); 
        for (int i = 1; i  <= helloThere; i++)
        System.out.println("Hello There" );
    }
}

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 02:52 PM

Your latest version works fine when I run it, though I haven't tested all of the other errors others mentioned like the infinite loop in your getPos() method.

Are you compiling and running at a command line prompt? If so, show us what you're typing and then the response you're getting. Cut and paste the entire session from start to finish.
Was This Post Helpful? 3
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 03:01 PM

Your getPos() method is kind of useless, it prints an error if x is not in range, and that's it
May be you should consider:

static int getPos() {
  Scanner scan = new Scanner(System.in);
  while(true) {
      System.out.print("Enter a number > 0: ");
      int x = scan.nextInt();
      if(x > 0)
         return x;
  }
}


*Edited nextInt() not nextInput()

This post has been edited by pbl: 02 July 2011 - 05:41 PM

Was This Post Helpful? 3
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 03:02 PM

When looking to your updated version, following what others have said:
- you have space between System. and in it should be
  Scanner input= new Scanner( System.in); // no space 

Was This Post Helpful? 2
  • +
  • -

#12 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 03:44 PM

View Postsmohd, on 02 July 2011 - 03:02 PM, said:

When looking to your updated version, following what others have said:
- you have space between System. and in it should be
  Scanner input= new Scanner( System.in); // no space 



WOW!! my code is fine and I ran it and get the results I want. It was that SINGLE space I put in. And @pbl, why is my method useless? I tried it and it works fine (i.e. the user puts in "3" and the result is "Hello There Hello There Hello There". Does it look as if something is still wrong. Thanks so much smohd!!

OH! but I do need it to run again if, say, 0 or a negative number is entered because if that happens then it just stops. Would something need to be added to the first method so it can ask again and again until a positive number is entered?
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 03:50 PM

View Postlolitacharm, on 02 July 2011 - 06:44 PM, said:

]
And @pbl, why is my method useless?

Right now your method prints "Enter a number greater than 0" if the parameter received is <= 0
and does nothing else... like asking for a number > 0

All the -1 you want to give me will change nothing :)
Your method displays "Enter a number greater than 0" and then does nothing to correct it

If the user inputs -10 in the main program, you will prompt an do not read any answer back
This is a major bug in your design despite the fact you changed your while() for a simple if... as useless :)

This post has been edited by pbl: 02 July 2011 - 03:57 PM

Was This Post Helpful? 4
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 03:57 PM

Quote

Would something need to be added to the first method so it can ask again and again until a positive number is entered?

Yes, something like what pbl did in his previous post. Look at his previous post he has shown you how to make it make sense :)
Was This Post Helpful? 3
  • +
  • -

#15 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: Java method: prints statement number of times entered by user

Posted 02 July 2011 - 04:00 PM

ok, let's see. I'm still learning about methods and I think I'm confused about what you can and can't do in them. Will changes need to be in the first method or the main then? Because if I try to add anything else to getPos it is saying I am missing a return. Can loops or something be added to that method?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2