Solved: PLEASE READ! Java, print largest number

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Hello. :)

I have written a program in Java that reads in a sequence of x amount of numbers, terminated by -9999 and adds them together, printing the result to the screen.
My problem is that I also want to make it so the program prints out the largest number the user entered as well as the sum of the sequence.
I should point-out that I must do this using METHODS ONLY.

I am currently using Windows XP.

Thanks in advance for any assistance offered.

Code:
import java.util.*;

public class AddUp01
{
	
  public static void main(String[] args)
  {
  	static int largest(currentValue())
  	
  	{
  		if(currentValue > largest)
  	{
  		largest = currentValue;
  		return largest;
  	}
  	
  	}
  	
  	int limit = -9999;
    int sum = 0;
      int values = 0;
      int largest = 0;
	int currentValue = 0;
    
  	Scanner input = new Scanner(System.in);  
  	
    System.out.print("Enter N number of values to add together: ");
    values = input.nextInt();
      
      System.out.print("Enter value: "); 
      currentValue = input.nextInt();    
      
      while (currentValue >=1 && currentValue <=100)
          
          
      {
          sum = sum + currentValue;
          currentValue = input.nextInt();
      }
      
      if (currentValue == -9999)
          
      {
          System.out.println("Invalid value entered!");
      }
      
      System.out.println("sum = " + sum);
  }
}
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
It does not appear you are using arrays. You really should use arrays for this.

Here is a good method from Stack: http://stackoverflow.com/questions/8920353/how-to-find-the-largest-int-in-an-array-using-java

Using the array, you can also do something like this for the total:

Code:
for (int i : someArray)
    sum += i;
Thanks, but as I said in my opening post, it's not calculating/printing the total I'm stuck on as that's working fine.
What I'm stuck on is printing out the largest valid number entered by the user, along with the sum total.
I also mentioned in my opening post that this is something I can only do using a method, as I haven't covered arrays yet (hence why I'm not currently using them).
 
Joined
Jun 19, 2004
Messages
7,800
Alright... In that case:

Pseudocode:

Code:
int biggest = 0;
(While loop running)
if (currentNumber > biggest)
biggest = currentNumber
Just have it check if its bigger than the current value of biggest, if it is not, it does nothing. If it is then it over writes biggest with the current value.
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Alright... In that case:

Pseudocode:

Code:
int biggest = 0;
(While loop running)
if (currentNumber > biggest)
biggest = currentNumber
Just have it check if its bigger than the current value of biggest, if it is not, it does nothing. If it is then it over writes biggest with the current value.
Okie doke, thanks a lot! :D
Where abouts in my original code would that be placed, exactly?
I think that's what's been tripping me up quite a lot. :(
 
Joined
Jun 19, 2004
Messages
7,800
IDK, LOL... Your code doesn't look like it should run to me :D

I don't have a JAVA IDE in front of me right at the moment either...

I would re-arrange your code and nest the loops a little cleaner if I were you.

Something like:

Code:
import java.util.*;

public class AddUp01
{
	
  public static void main(String[] args)
  {
	int limit = -9999;
    int sum = 0;
	int values = 0;
	int largest = 0;
	int currentValue = 0;
	
	
	System.out.print("Enter N number of values to add together: ");
	Scanner input = new Scanner(System.in); 
	values = input.nextInt();
	
	If (currentValue != -9999)
	{
	
		static int largest(currentValue())
		{
		if(currentValue > largest)
			{
			largest = currentValue;
			return largest;
			}
		}
		
		sum = sum + currentValue;
		System.out.print("Enter value: "); 
        currentValue = input.nextInt();
	}
	else
		{
		System.out.println("Invalid value entered!");
		}

    }
      
      
      System.out.println("sum = " + sum);
	  System.out.println("Largest = " + largest);
  }
}
I am going to test this shortly...
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
IDK, LOL... Your code doesn't look like it should run to me :D

I don't have a JAVA IDE in front of me right at the moment either...

I would re-arrange your code and nest the loops a little cleaner if I were you.

Something like:

Code:
import java.util.*;

public class AddUp01
{
	
  public static void main(String[] args)
  {
	int limit = -9999;
    int sum = 0;
	int values = 0;
	int largest = 0;
	int currentValue = 0;
	
	
	System.out.print("Enter N number of values to add together: ");
	Scanner input = new Scanner(System.in); 
	values = input.nextInt();
	
	If (currentValue != -9999)
	{
	
		static int largest(currentValue())
		{
		if(currentValue > largest)
			{
			largest = currentValue;
			return largest;
			}
		}
		
		sum = sum + currentValue;
		System.out.print("Enter value: "); 
        currentValue = input.nextInt();
	}
	else
		{
		System.out.println("Invalid value entered!");
		}

    }
      
      
      System.out.println("sum = " + sum);
	  System.out.println("Largest = " + largest);
  }
}
I am going to test this shortly...
I just did and I got a list of errors from the compiler, mainly syntax ones.
Also, the while loop from my original code doesn't seem to be there anymore in this code. ^
 
Joined
Jun 19, 2004
Messages
7,800
Yep. I am going to re-do it with your code and see whats going on...

I have not used Java in a bit.
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Are you required to use a while loop? If yes, why not use while input != -9999?
Yes, the while loop is required to tell the program to only accept numbers between 1 and 100 inclusive.
The -9999 is currently being used in an 'if' statement to tell the program to terminate if that number is entered.
 
Joined
Jun 19, 2004
Messages
7,800
Thats the requirement, or they way you are doing it???

Are you required to use *an if statement* ?

Because this would be the better approach, IMO:

Code:
        int limit = -9999;
        int sum = 0;
	int values;
	int largest = 0;
	int currentValue = 0;

        
    Scanner input = new Scanner(System.in);  
  	
    System.out.println("Enter N number of values to add together: ");
    values = input.nextInt();
      
         
      int i = 0;
      while ((i < values) && (currentValue != -9999))
      {
          System.out.println("Enter value: "); 
          currentValue = input.nextInt(); 
          
          sum = sum + currentValue;
          
          if (currentValue > largest)
          {
              largest = currentValue;
          }
          
      
      System.out.println("sum = " + sum);
      i++;
      }
      
      if (currentValue == -9999)  
      {
          System.out.println("Invalid value entered!");
      }
      
      System.out.println("sum: " + sum);
      System.out.println("Largest: " + largest);
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Thats the requirement, or they way you are doing it???

Are you required to use *an if statement* ?

Because this would be the better approach, IMO:

Code:
        int limit = -9999;
        int sum = 0;
	int values;
	int largest = 0;
	int currentValue = 0;

        
    Scanner input = new Scanner(System.in);  
  	
    System.out.println("Enter N number of values to add together: ");
    values = input.nextInt();
      
         
      int i = 0;
      while ((i < values) && (currentValue != -9999))
      {
          System.out.println("Enter value: "); 
          currentValue = input.nextInt(); 
          
          sum = sum + currentValue;
          
          if (currentValue > largest)
          {
              largest = currentValue;
          }
          
      
      System.out.println("sum = " + sum);
      i++;
      }
      
      if (currentValue == -9999)  
      {
          System.out.println("Invalid value entered!");
      }
      
      System.out.println("sum: " + sum);
      System.out.println("Largest: " + largest);
No, the 'if' statement wasn't a requirement, just the way I was doing it. Your way does seem to work better though. :)

The only thing I would point-out is that if I say I want to add together 2 numbers, for example, and I enter 1 as the first and -9999 as the second to terminate the program, for some reason it minuses 1 from -9999 and prints -9998 as the sum. I'm just curious as to why it's doing that. :confused:
 
Joined
Jun 19, 2004
Messages
7,800
poor nesting.

I was trying to get you to sign off with the direction before I trimmed the logic the rest of the way.

You can either add another if statement to it (around the sum print) for if currentValue != -9999 then print sum, or move the sum print in the existing loop.
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
poor nesting.

I was trying to get you to sign off with the direction before I trimmed the logic the rest of the way.

You can either add another if statement to it (around the sum print) for if currentValue != -9999 then print sum, or move the sum print in the existing loop.
I tried both ways and it's still happening, I'm not sure if I'm doing it right? :confused:
 
Joined
Jun 19, 2004
Messages
7,800
in the main loop, where you have
sum = sum + currentValue;

change it to:

Code:
if (currentValue != -9999)
          {
          sum = sum + currentValue;
          }
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top