12 Replies - 922 Views - Last Post: 05 March 2010 - 03:23 PM Rate Topic: -----

#1 Guest_D_Akatosh*


Reputation:

Homework help...

Posted 04 March 2010 - 04:17 PM

I'm a complete java noob and this problem has me completely confused.

"Write a program that first prompts the user to specify the number of integers to enter, then asks the user to enter integers, and finally displays the smallest integer entered"

Asking someone to input something is cake. What has me confused is this: When the user says they want to enter, say, 3 integers, how would I get the program to ask the user to enter those 3 integers.

My code so far looks like (using NetBeans 6.8):
import java.util.Scanner;
public class HW24 {
    public static void main (String [] args) {
        Scanner input = new Scanner(System.in);

       // Prompt user to specify the number of integers to enter
       System.out.print ("Enter number of integers to be entered: ");
        final int integerEntry = input.nextInt(); 

      // Enter integers (this is where I'm stuck)



Thanks for any help.

This post has been edited by pbl: 04 March 2010 - 09:02 PM
Reason for edit:: Code tags added for a newbie


Is This A Good Question/Topic? 0

Replies To: Homework help...

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Homework help...

Posted 04 March 2010 - 04:20 PM

Please, :code:

You should consider using a for loop here, which is designed to iterate a given number of times. It has 3 components, which are:
for(var-declaration; condition; var-modification){
   ..code..
}



So if you wanted to iterate 3 times, I would use a for loop like so:
for(int i = 0; i < 3; i++){

}



So think about how you can use this to iterate x number of times, where x is user input. What would you need to replace in my above example? Go ahead and give it a try!
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Homework help...

Posted 04 March 2010 - 04:26 PM

Well first, now that you have integerEntry, you can loop through that many times, getting the smallest. Note that Integer.MAX_VALUE just gives the number a good starting point as to not interfere with any potential starting values.

... we have integerEntry.

// ANY number is smaller than this, it makes a convenient starting point.
int smallestNum = Integer.MAX_VALUE;

// Now, loop through the array. (If this looks foreign to you,
// let us know, we can further explain).
for (int i = 0; i < integerEntry; i++) {
    // At the current index, read an integer.
    int temp = integer.nextInt();
    
    // If temp is smaller than the current smallest
    // set this one as the new smallest.
    if (temp < smallestNum)
        smallestNum = temp;

    System.out.println(smallestNum);
}



edit: oops mac...ruined that one for you! And thanks, was a slip of the finger...

This post has been edited by Dogstopper: 04 March 2010 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 zim1985   User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Homework help...

Posted 04 March 2010 - 04:27 PM

View PostD_Akatosh, on 04 March 2010 - 02:17 PM, said:

I'm a complete java noob and this problem has me completely confused.

"Write a program that first prompts the user to specify the number of integers to enter, then asks the user to enter integers, and finally displays the smallest integer entered"

Asking someone to input something is cake. What has me confused is this: When the user says they want to enter, say, 3 integers, how would I get the program to ask the user to enter those 3 integers.

My code so far looks like (using NetBeans 6.8):

import java.util.Scanner;
public class HW24 {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);

// Prompt user to specify the number of integers to enter
System.out.print ("Enter number of integers to be entered: ");
final int integerEntry = input.nextInt();

// Enter integers (this is where I'm stuck)


Thanks for any help.

First off, no need for any finals
final int integerEntry = input.nextInt();

SHOULD BE

int integerEntry = input.nextInt();



So you need a loop that does something like this:
int times = 0;
while(times <= integerEntry)
{
    // prompt to ask for number
    times++;
}


You are then going to need to implement the max/min checker inside of this loop...

EDIT: They all posted as I was writing... :batman:

This post has been edited by zim1985: 04 March 2010 - 04:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_D_Akatosh*


Reputation:

Re: Homework help...

Posted 04 March 2010 - 05:16 PM

Thanks for all your help so far. I got as far as having the user input the actual integers, but now I'm stuck on the max/min checker. I don't even know where to start. I tried using methods but I kept getting errors. We haven't done much in class with max/min values, so can anyone lead me in the right direction? Thanks again.
Was This Post Helpful? 0

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Homework help...

Posted 04 March 2010 - 05:21 PM

View PostDogstopper, on 04 March 2010 - 07:26 PM, said:

Well first, now that you have integerEntry, you can loop through that many times, getting the smallest. Note that Integer.MAX_VALUE just gives the number a good starting point as to not interfere with any potential starting values.

... we have integerEntry.

// ANY number is smaller than this, it makes a convenient starting point.
int smallestNum = Integer.MAX_VALUE;

// Now, loop through the array. (If this looks foreign to you,
// let us know, we can further explain).
for (int i = 0; i < integerEntry; i++) {
    // At the current index, read an integer.
    int temp = integer.nextInt();
    
    // If temp is smaller than the current smallest
    // set this one as the new smallest.
    if (temp < smallestNum)
        smallestNum = temp;

    System.out.println(smallestNum);
}



edit: oops mac...ruined that one for you!


I think Dogstopper answered this question for you. :)

@Dogstopper: Watch out for your comparative operator symbols. You mixed up < and > here: if (temp > smallestNum). I corrected it in your code when I quoted. :)
Was This Post Helpful? 0
  • +
  • -

#7 Guest_D_Akatosh*


Reputation:

Re: Homework help...

Posted 04 March 2010 - 05:51 PM

Hmmm...I couldn't get it to work. I should post my code so far since I have a different style of code now:

 import java.util.Scanner;
public class HW24 {
    public static void main (String [] args) {
        Scanner input = new Scanner(System.in);

        System.out.print ("Enter number of integers to be entered: ");
        int integerEntry = input.nextInt();

        int count = 0;
        while(count < integerEntry)
 System.out.print ("Enter integer: ");
 int integerInput = input.nextInt();
    count++; 


I created the integerInput variable and tried to find the minimum of that, bot got nowhere.
Was This Post Helpful? 0

#8 zim1985   User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Homework help...

Posted 04 March 2010 - 05:54 PM

View PostD_Akatosh, on 04 March 2010 - 03:51 PM, said:

Hmmm...I couldn't get it to work. I should post my code so far since I have a different style of code now:

 import java.util.Scanner;
public class HW24 {
    public static void main (String [] args) {
        Scanner input = new Scanner(System.in);

        System.out.print ("Enter number of integers to be entered: ");
        int integerEntry = input.nextInt();

        int count = 0;
        while(count < integerEntry)
 System.out.print ("Enter integer: ");
 int integerInput = input.nextInt();
    count++; 


I created the integerInput variable and tried to find the minimum of that, bot got nowhere.

Your while loop is missing braces. Try this:
 import java.util.Scanner;
public class HW24 {
    public static void main (String [] args) {
        Scanner input = new Scanner(System.in);

        System.out.print ("Enter number of integers to be entered: ");
        int integerEntry = input.nextInt();

        int count = 0;
        while(count < integerEntry)
        {
             System.out.print ("Enter integer: ");
             int integerInput = input.nextInt();
            count++; 
        }

This post has been edited by zim1985: 04 March 2010 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#9 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Homework help...

Posted 04 March 2010 - 05:55 PM

Try creating an int, smallestInt outside the loop, and then inside of it set smallestInt to input.nextInt(), if it is less than the current value of smallestInt.

You could do this with a simple if statement, or, you could do it in one line using the ternary operator ?: (if you know it)
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Homework help...

Posted 04 March 2010 - 05:57 PM

When you omit curly braces for a conditional or loop, only the first statement following the loop is executed in the loop. So your while loop below without the curly braces is the same as the following with braces:
while(count < integerEntry){
   System.out.print ("Enter integer: "); 
}
int integerInput = input.nextInt(); 
count++; 



So essentially, you create an infinite loop. To fix this, move the end brace to include the other two statements, like so:
while(count < integerEntry){
   System.out.print ("Enter integer: "); 
   int integerInput = input.nextInt(); 
   count++; 
}



Also, you have to use a min variable outside of the loop and update in the loop. So I've outlined the logic, and I'll let you fill in the code:
int min = Integer.MAX_VALUE;

while(count < integerEntry){
   System.out.print ("Enter integer: "); 
   int integerInput = input.nextInt(); 
   count++; 

   //think about what you want to do here
   //if the new input is less than the current min
   //isn't the new input the overall min?
   //so you need to update the min variable
}


Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Homework help...

Posted 04 March 2010 - 06:03 PM

:whistling: I knew that...the keys were right next to each other and I didn't notice...oops...Thanks :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Homework help...

Posted 04 March 2010 - 06:16 PM

I know, right. I make that typo all the time.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_D_Akatosh*


Reputation:

Re: Homework help...

Posted 05 March 2010 - 03:23 PM

I got it to work. Thanks guys, you all are very helpful.
Was This Post Helpful? 0

Page 1 of 1