1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PLEASE READ! Java, print largest number

Discussion in 'Software Development' started by ElementOfMagic, Dec 30, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. ElementOfMagic

    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);
      }
    }
    
     
  2. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
  3. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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).
     
  4. loserOlimbs

    loserOlimbs

    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.
     
  5. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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. :(
     
  6. loserOlimbs

    loserOlimbs

    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...
     
  7. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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. ^
     
  8. loserOlimbs

    loserOlimbs

    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.
     
  9. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    Are you required to use a while loop? If yes, why not use while input != -9999?
     
  10. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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.
     
  11. loserOlimbs

    loserOlimbs

    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);
    
     
  12. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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:
     
  13. loserOlimbs

    loserOlimbs

    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.
     
  14. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    I tried both ways and it's still happening, I'm not sure if I'm doing it right? :confused:
     
  15. loserOlimbs

    loserOlimbs

    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;
              }
    
     
  16. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1083019

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice