Results 1 to 5 of 5
  1. #1
    new person is offline Member
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default [SOLVED] User Input - loop

    Some help with this would be greatly appreicated, since it's due in tomorrow.

    I've been through all my notes and such, but the problem is they don't really explain these things very much at all, they seem to just expect people to automatically understand things.

    It's quite a long question, so i'll just copy/paste it:

    Note: I'm using BlueJ

    12.2: project from qu 12.1, looping

    This question requires keyboard input so you need to make appropriate classes available. One way to do this is to include the InputReader class in your project whose complete code is provided below, though you could use the Scanner class directly, if you prefer. Whichever way you prefer, include code so your Loops class can use a Scanner object.


    import java.util.Scanner;


    /**
    * InputReader reads typed text input from the standard text terminal.
    *
    * @author Lisa Payne
    * @version Jan 2007
    */
    public class InputReader
    {
    private Scanner reader;

    /**
    * Create a new InputReader that reads text from the text terminal.
    */
    public InputReader()
    {
    reader = new Scanner(System.in);
    }

    /**
    * Accesses a String typed in text terminal
    *
    * @returns String value input
    */
    public String getString()
    {
    String input = reader.nextLine();
    return input;
    }

    /**
    * Accesses a int typed on a single line in text terminal
    *
    * @returns int value input
    */
    public int getInt()
    {
    int input = reader.nextInt();
    reader.nextLine();
    return input;
    }

    }



    Start a second method in your Loops class - any sensible name. In this method you need to write code similar to that in question 12.1 (indeed you may want to copy 'n paste that to give you a start). This method should total a series of positive integers which the user enters from the keyboard. The user will type in each value followed by an <Enter> keypress. After the last value the user will type a negative value: this is the terminal sentinel. (Of course this method requires only a single loop - not a loop inside a loop.) (not from Barnes and Kolling)
    Basically i think it wants me to add a method to the class i've been using which lets the user enter several numbers seperated by the <enter> key and then loop through them and add them up [That's what 12.1 was about].

    But i just have no idea how to do that, when i press the enter key when entering numbers, it just sends it through to the method and runs it and doesn't let me add anything else, and as far as i can see this is the first time she's even mentioned this scanner and reader things, and it's confusing the hell out of me =(

    Could some body possibily point me in the right direction of what i'm supposed to be doing to get this to work?

    Thank you.

    [If it helps, here is the Looping class:]

    Java Code:
    /**
     * Write a description of class Loops here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Loops
    {
        // instance variables - replace the example below with your own
        private int i = 1;
        private int a = 0;
        
        
        /**
         * Constructor for objects of class Loops
         */
        public Loops()
        {
                  
        }
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
        public void loopIt()
        {
            // put your code here
            do
            {
                
                // Calc
                int num = 0;
                
                int d = i - 1;
                
                a = i;
                
                while(d > 0)
                {
                    a += d;
                    d--;
                }
                
                
                
             System.out.println(i + ": " + a);   
             i++;
            } while(i <= 5);
        }
        
    
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

  3. #3
    new person is offline Member
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default

    I've got this far with my method now:

    Java Code:
        public void userInput(String args[])
        {
            System.out.println("Please Enter Your Integer [Adding a negative number or a non-integer will stop the program]:");
    
            Scanner sc = new Scanner(System.in);
            
            while(sc.hasNextInt())
                    {
                        int i = sc.nextInt();
                                
                if(i < 0)
                {
                    System.out.println("Thank you. Your total is: " + total);
                    return;
                }
                else if(1 >= 0)
                {
                 total += i;   
                 System.out.println("Thank you, please add another.");
                }
            
                    } 
     
                    
    
        }
    So i can get it to let me enter more than 1 thing, and add them up if they are positive integers, however if i enter something other than an integer it just crashes, how can i alter the code so that it continues if you enter something other than an integer, but just doesn't print/add anything?

    Thanks.

  4. #4
    new person is offline Member
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default

    Never mind, i worked it out myself:

    Java Code:
    import java.util.Scanner;
    /**
     * Write a description of class Loops here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Loops
    {
        // instance variables - replace the example below with your own
        private int i = 1;
        private int a = 0;
        private int total = 0;
        
        /**
         * Constructor for objects of class Loops
         */
        public Loops()
        {
                  
        }
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
        public void loopIt()
        {
            // put your code here
            do
            {
                
                // Calc
                int num = 0;
                
                int d = i - 1;
                
                a = i;
                
                while(d > 0)
                {
                    a += d;
                    d--;
                }
                
                
                
             System.out.println(i + ": " + a);   
             i++;
            } while(i <= 5);
        }
        
    
        public void userInput(String args[])
        {
            System.out.println("Please Enter Your Integer [Adding a negative number or a non-integer will stop the program]:");
    
            Scanner sc = new Scanner(System.in);
            
            while(sc.hasNext())
                    { // While
                        
                        if(sc.hasNextInt())
                        { // Int
                        
                        
                        int i = sc.nextInt();
                                
                if(i < 0)
                {
                    System.out.println("Thank you. Your total is: " + total);
                    return;
                }
                else if(1 >= 0)
                {
                 total += i;   
                 System.out.println("Thank you, please add another.");
                }
            
                    } // Int
                    else
                    {                   
     
                     String l = sc.nextLine();
                    }
     
                    
    
        } // While
        
       }   
    }

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    put it in a try-catch block that does nothing
    try{
    // code here
    }catch(NumberFormatException ex){}
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  2. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM
  3. Multiple line user input
    By jointhelabel in forum New To Java
    Replies: 3
    Last Post: 10-26-2008, 02:41 AM
  4. who to take user input ,not by console...
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 10:09 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

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