Results 1 to 19 of 19
  1. #1
    veena is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default [SOLVED] getting number from console and calculating

    Hi everyone,

    I need to get numbers from console and need to check which is greater and less
    im trying but unable to get

    can any 1 help me out plz

    thanks in advance
    Last edited by veena; 05-05-2008 at 02:50 PM.

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Can you please show us some code. We here at the Java forums believe in helping people learn not giving them the answer.
    My IP address is 127.0.0.1

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You can use Scanner class to capture the input....

    Maybe, you can implement that using a while loop, or do-while loop...

    Filter every input, if allowed? store in an array.... if 0, stop the loop... and calculate the total values that was stored in the array....
    Put a try-catch block to catch such NumberFormatException if accidentally entered a non-integer values....
    freedom exists in the world of ideas

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I would use a vector in particular for this problem.
    My IP address is 127.0.0.1

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    If he knows Vector, then he could make it too....
    freedom exists in the world of ideas

  6. #6
    veena is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    can u jus plz give me some idea to proceed
    Last edited by veena; 05-05-2008 at 02:52 PM.

  7. #7
    veena is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    how to do with vectors

  8. #8
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Java Code:
    initialize an array
    node for array = 0;
    
    print("Enter numbers or 0 to stop: ");
    do{
         try{
              Input = fromUser;
              if( Input != 0){
                    array[node] = UserInput;
                    node++; 
              }
         }catch(Exception e){}
    }while(Input != 0)
    You have now the values stored in an array....
    You can now manipulate it....

    If you are not sure about your program, start with less inputs... max ten and implement on an array....
    If successful, try to read about ArrayList... it is flexible.
    Last edited by sukatoa; 05-02-2008 at 06:40 AM.
    freedom exists in the world of ideas

  9. #9
    veena is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    i got it using bufferstmt thank u

  10. #10
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Here's an example of a design.

    Java Code:
    Vector myVector = new Vector();
    
    while(true)
    {
       data = ; //however your going to get you data I would recommend JInputDialog
    
       if(data == 0)
       {
           break;
       }
       else 
       {
            myVector.add(data);
       }
    }
     this.whatever you want to call the method that manipulates your vector.
    My IP address is 127.0.0.1

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by veena View Post
    i got it using bufferstmt thank u
    Are you talking about BufferStraem? Why don't you use Scanner, more flexible way to handle user inputs.

  12. #12
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hi,

    Try this.

    Java Code:
    import java.io.*;
    import java.util.*;
    public class CalculateNum
    {
    	public static void main(String[] args){
    		BufferedReader br = new BufferedReader(new InputStreamReader( System.in ));
    		String str;
    		ArrayList even = new ArrayList();
    		ArrayList odd = new ArrayList();
    		try{
    			System.out.println( "Enter 0 to stop: " );
    			System.out.println( "Enter numbers: " );
    			do
    			{
    			str = br.readLine(); // from console input example above.
    			int number = Integer.parseInt( str); // converts a String into an int value
    			
    			if(!str.equals("0")){
    				if(number%2==0)
    					even.add(number);
    				else
    					odd.add(number);
    			}
    
    			System.out.println( "input = " + number);
    			}while(!str.equals("0"));
    
    			System.out.println("EVEN : "+Collections.min(even));
    			System.out.println("ODD : "+Collections.max(odd));
    
    		}catch(Exception err){
    
    		}
    	}
    }
    sanjeev,संजीव

  13. #13
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Why don't you use Scanner, more flexible way to handle user inputs.
    .....Yep..... Absolutely :)
    freedom exists in the world of ideas

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In Java we have lots of choices. So we should have to select the best one depending on the application we have. :)

  15. #15
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    All of our suggestions will work, it is up to you decide which one is the most efficient and most flexible for your case. This may seem easy or not even a matter for such a small problem such as yours but later it will become a big issue.
    My IP address is 127.0.0.1

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, and also keep in mind. Moving from one to another can be really difficult in later, when your application become too complex.

  17. #17
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Yep, and also keep in mind. Moving from one to another can be really difficult in later, when your application become too complex.
    That is why interfaces are so good. It makes it a lot easier to change what you use because instead of changing every place you use it you just change where you initialize it.
    My IP address is 127.0.0.1

  18. #18
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Zosden View Post
    That is why interfaces are so good. It makes it a lot easier to change what you use because instead of changing every place you use it you just change where you initialize it.
    Inheritance and Polymorphism are also very powerful and great thing in Java.
    sanjeev,संजीव

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Calculating per hour statistics
    By vipergt89 in forum New To Java
    Replies: 2
    Last Post: 05-01-2008, 06:25 AM
  2. Calculating trigonometric functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:56 PM
  3. Calculating hyperbolic functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:55 PM
  4. Calculating the DB connection time
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:55 AM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •