User Input Java Help *Advanced?*

Discussion in 'Programming General' started by DeepSnow, Sep 7, 2010.

User Input Java Help *Advanced?*
  1. Unread #1 - Sep 7, 2010 at 10:53 AM
  2. DeepSnow
    Joined:
    Jun 30, 2009
    Posts:
    232
    Referrals:
    0
    Sythe Gold:
    0

    DeepSnow Active Member

    User Input Java Help *Advanced?*

    Code:
    import java.io.*;
    import java.util.*;
    public class triangletest
    {
      public static void main(String args[])
      {
        triangle theTriangle = new triangle();
        theTriangle.setBase(4);
        theTriangle.setHeight(2);
        theTriangle.calculateArea();
        theTriangle.printArea();
        theTriangle.calculatePerimeter();
        theTriangle.printPerimeter();
      }
    }
    Code:
    import java.io.*;
    import java.util.*;
    public class triangle
    {
     private double area;
     private double base;
     private double height;
     private double perimeter;
     public static void printInstructions()
    {
     System.out.println();
    }
    public void setBase(double theBase)
    {
     base = theBase;
    }
    public void setHeight(double theHeight)
    {
     height = theHeight;
    }
    public void calculateArea()
    {
     area = base *height * .5;
    }
    public void printArea()
    {
    System.out.println("The area is: " + area);
    }
    public void calculatePerimeter()
    {
     perimeter = height * 3;
    }
    public void printPerimeter()
    {
     System.out.println("The perimeter is: " + perimeter);
    }
    }
    As you can see there are two different programs. One called triangletest and one called triangle. In triangletest notice how I hardcoded what the dimensions of the triangle were. Instead of that, and likely using the "Scanner" command, ask for and accept user input to use in my CalculatePerimeter/Area of my second code, triangle.

    Guys know anything about this?
     
  3. Unread #2 - Sep 7, 2010 at 2:13 PM
  4. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    User Input Java Help *Advanced?*

    Code:
    import java.util.Scanner;
    Scanner myScanner = new Scanner(System.in);
    String thisString = myScanner.next(); // this gets a string
    int thisInt = myScanner.nextInt(); // gets an int, what you need to use
    
     
  5. Unread #3 - Sep 18, 2010 at 10:46 AM
  6. diabl0
    Joined:
    Jul 25, 2008
    Posts:
    902
    Referrals:
    0
    Sythe Gold:
    0

    diabl0 Apprentice
    Banned

    User Input Java Help *Advanced?*

    Wait so you do not want to use the Scanner command, or you do but don't know how?
     
  7. Unread #4 - Sep 30, 2010 at 10:29 PM
  8. meodows92
    Joined:
    Jun 8, 2009
    Posts:
    232
    Referrals:
    0
    Sythe Gold:
    0

    meodows92 Active Member

    User Input Java Help *Advanced?*

    Scanner are just for you to type, why needed so long?
     
  9. Unread #5 - Oct 5, 2010 at 4:50 PM
  10. Zed'
    Joined:
    Oct 4, 2010
    Posts:
    93
    Referrals:
    0
    Sythe Gold:
    0

    Zed' Member

    User Input Java Help *Advanced?*

    What do you mean? For console programs you can type whenever you want. A scanner just waits until you type something and retrieves it for to use in your program.
     
  11. Unread #6 - Oct 13, 2010 at 2:22 PM
  12. xRSx Newb
    Joined:
    Sep 27, 2010
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0

    xRSx Newb Member

    User Input Java Help *Advanced?*

    For java, I/O is really more complicated than it should be in my opinion. I would recommend looking up a class called TextIO.java on google, and using that for programs in which you need to read in or output data. For example, instead of doing as suggested and creating a scanner object that you must use to read in input, you could simply type TextIO.getInt(), and the user would then type in the input in the console. Of course, if you are doing this as a school assignment you might have to check if you are allowed to utilize a third-party class like this because it could easily mess up the grading script assuming they grade it automatically.
     
  13. Unread #7 - Oct 13, 2010 at 9:01 PM
  14. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    User Input Java Help *Advanced?*

    How is your example of TextIO.getInt() better than my example of myScanner.nextInt()? Is creating an object so hard that you have to rely on a 3rd party module's static function to do it? Doing so defeats the entire purpose of Java. >__>

    Not to mention Scanner can be used with files too.
     
  15. Unread #8 - Oct 13, 2010 at 10:33 PM
  16. xRSx Newb
    Joined:
    Sep 27, 2010
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0

    xRSx Newb Member

    User Input Java Help *Advanced?*

    I never said that it was "better" and I'm sorry if I gave off the impression that it is. I'm simply saying that for someone who is clearly just getting started with object oriented programming, it can be a lot to take in and they can easily become confused with what exactly it is that they are doing, and will likely end up just copy and pasting code without really understanding the meaning behind it. I'm simply saying that my approach lets them abstract out the details of creating and using objects until they have a more complete idea of what objects actually are and how to use them. Sorry again if I gave off the impression that your way was "bad" or mine was "better".

    Also, TextIO does support reading and writing to files. It also has built in error detection so that new programmers don't have to worry about details such as EOF.
     
< Really good C video tutorials | BankAccount Class I made in AP >

Users viewing this thread
1 guest


 
 
Adblock breaks this site