Quantcast

Jump to content


Photo

Help needed with Java SE


  • Please log in to reply
14 replies to this topic

#1 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 10:53 AM

I'm having a lot of trouble getting my program to run properly. Any help with this will be greatly appreciated.

Write a Java application program called Largest.java that inputs a series of 10 single-digit numbers and determines and prints the largest of the numbers. Except main() method, no other user-defined method is required. Your program should use at least the following three variables

  • counter: A counter to count to 10 (that is, to keep track of how many numbers have been input and to determine when all 10 numbers have been processed);
  • number: The current digit input to the program; and
  • largest: The largest number found so far.
When each number is entered, make sure it is a single digit number, that is 0 to 9.


Spoiler


The issue that my program is having is that it keeps returning the input as 0 and it doesn't let me input any more than once. I am using Eclipse to compile the code but that shouldn't make a difference as to whether or not it's working.

#2 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 March 2012 - 10:59 AM

You declared but haven't populated your inputArray so there are never any numbers in it.

#3 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 11:00 AM

Ah okay, how do I go about populating the array? I am learning but I've not learned how to do this yet.

#4 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 March 2012 - 11:01 AM

Is the user supposed to be entering 10 numbers to go in the array?

#5 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 11:02 AM

My professor really isn't being any help at all with answering questions so I thought it best to turn for help here.

Yes the user should be entering 10 numbers in the array.

#6 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 March 2012 - 11:06 AM

My professor really isn't being any help at all with answering questions so I thought it best to turn for help here.

Yes the user should be entering 10 numbers in the array.


Then you should have your dialog within a loop in order to prompt the user to enter 10 numbers (unless you can give the user a multi-line dialog where they can enter all the numbers in the same box, I don't use Java so I'm not sure what kind of dialogs they have).

#7 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 11:17 AM

I'm not entirely sure myself but I will try and figure it out from there, thanks Waser.

I was actually thinking that

if(inputArray[input]>largest){

largest = inputArray[input];

Is what would re-prompt the user for the next number.

#8 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 March 2012 - 11:18 AM

I'm not entirely sure myself but I will try and figure it out from there, thanks Waser.

I was actually thinking that

if(inputArray[input]>largest){

largest = inputArray[input];

Is what would re-prompt the user for the next number.


You don't really need to use an array, you could do something like:

int largest = 0;
int counter = 0;

while(counter < 10){
	String input = INPUTDIALOG("Whatever");
	int number = Integer.parseInt(input);
	
	if (number is between 0 and 9) {
		if(number > largest){
			set largest variable to the current number because it's more than the current largest
		}
		increment your counter variable by 1
	}else{
		message that number needs to be between 0 and 9
	}
}

output the largest variable


#9 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 11:42 AM

It still isn't working but I'm sure that would work I know I'm just not coding it correctly. Gonna try scrapping it and starting over again but slower so that I might figure out where I went wrong.

#10 iargue

iargue
  • 10048 posts


Users Awards

Posted 11 March 2012 - 02:47 PM

import javax.swing.*;

public class largest {
	public static void main(String[] args) {
		int largest = 0;
		int counter = 0;
		int number = -1;

		while(counter < 10){
				String name = JOptionPane.showInputDialog("Enter a number between 0 and 9: ");
			    
			    try
			    { 
			    	number = Integer.parseInt(name);
			    } 
			    catch (NumberFormatException e)
			    { 
			    	continue;
			    } 
		        
		        
		        if (number >= 0 && number < 10) {
		                if(number > largest){
		                        largest = number;
		                }
		                counter++;
		        }else{
		                System.out.println("That number is not single digit. Try Again");
		        }
		}


		System.out.println(largest);
    }

}

I think I got all the errors.

Edited by iargue, 11 March 2012 - 03:03 PM.


#11 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 02:50 PM

Ah see that would work great but the professor specified that it has to have JOptionPane.showInputDialog or it will have points deducted.

Edited by Yung, 11 March 2012 - 02:50 PM.


#12 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 11 March 2012 - 02:55 PM

Ah see that would work great but the professor specified that it has to have JOptionPane.showInputDialog or it will have points deducted.


I'm sure you can figure out yourself how to fit that in.

#13 iargue

iargue
  • 10048 posts


Users Awards

Posted 11 March 2012 - 03:03 PM

Ah see that would work great but the professor specified that it has to have JOptionPane.showInputDialog or it will have points deducted.



Stupid. But I'll fix that up then :p, unless you think you can handle it. Also, I forgot some error handling on the java (If they just press enter) so I updated the above post.

Updated

#14 Yung

Yung
  • Codexian

  • 3361 posts


Users Awards

Posted 11 March 2012 - 03:05 PM

I had just finished updating it to show pretty much what you just did. Thanks. I'm going to break it down piece by piece so I fully understand how each section works. I really appreciate a working example to go off of thank you!

#15 iargue

iargue
  • 10048 posts


Users Awards

Posted 11 March 2012 - 03:11 PM

I had just finished updating it to show pretty much what you just did. Thanks. I'm going to break it down piece by piece so I fully understand how each section works. I really appreciate a working example to go off of thank you!



I was lazy and didn't comment it. I'll do that now.

import javax.swing.*;

public class largest {
	public static void main(String[] args) {
		int largest = 0; //Defines the largest number. We start at 0 as thats the lowest possible number to start at.
		int counter = 0; //This keeps track of how many times we have asked for a number
		int number = -1; //This keeps track of the current number. Starts at -1 for error handling

		while(counter < 10){ //This is a while loop. As long as our counter varible is below 10, we will keep repeating the code. THIS CAN GO ON FOREVER
				String name = JOptionPane.showInputDialog("Enter a number between 0 and 9: "); //Ask for their input. No matter what happens, we handle the error with the next line.
			    
			    try // Lets Try doing something!
			    { 
			    	number = Integer.parseInt(name); //Parse the input into an int. If its not a number, it will throw an error
			    } 
			    catch (NumberFormatException e) // Lets catch the error
			    { 
			    	number = -1; //Since they didn't put in a number, lets set the number ot -1 so our next statement will tell them its not a single digit
			    } 
		        
		        
		        if (number >= 0 && number < 10) { //This checks if its a number greater then or equal to 0 AND less then 10. This ensures its a single digit number
		                if(number > largest){ //Did they give us a number larger then our current largest number?
		                        largest = number; //Update our largest number with the current number!
		                }
		                counter++; // Since we did get a valid number between 0 and 9, we can increase the counter variable by 1
		        }else{ //Not a single digit number (Or a number at all <img src='http://www.neocodex.us/forum/public/style_emoticons/<#EMO_DIR#>/tongue.gif' class='bbc_emoticon' alt=':p' />)
		                System.out.println("That number is not single digit. Try Again"); //Complain to them!
		        }
		}


		System.out.println(largest); //We got 10 numbers, tell them which one was the highest
    }

}



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users