• Log in or sign up for an account here: https://www.sythe.org/login

User Input Java Help *Advanced?*

DeepSnow

Active Member
Joined
Jun 30, 2009
Sythe Gold
0
Messages
232
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?
 
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
 
Wait so you do not want to use the Scanner command, or you do but don't know how?
 
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

Scanner are just for you to type, why needed so long?
 
Scanner are just for you to type, why needed so long?

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

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.
 
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.
 
Runescape Gamblingrunescape gambling
Back
Top