How to print the area of a circle in another method - multithreading

I am trying to print out the answer for the Area of a circle in the myPrint(); method but it keeps calculating zero why?
I did my calculations in the Math(); method that I created. I am very new at coding and would greatly appreciate the help.
Thanks
package Project1;
import java.util.Scanner;
public class Code1 {
static String myStr;
static double radius;
static double answer;
static double pie;
public static void main(String[] args){
Name();
Info();
Math();
myPrint();
}
public static void Name(){
Scanner input = new Scanner(System.in);
System.out.println("What Is Your Name");
myStr = input.nextLine();
}
public static void Info(){
Scanner input = new Scanner(System.in);
String myStr;
System.out.println("What is Your Phone Number");
myStr = input.nextLine();
System.out.println("What is Your Age");
myStr = input.nextLine();
System.out.println("What is Your Postal Code");
myStr = input.nextLine();
}
public static void Math(){
double radius,answer;
double pie = 3.14;
Scanner input = new Scanner(System.in);
System.out.println("What is the Area of the Circle");
radius = input.nextDouble();
answer = pie *(radius * radius);
}
public static void myPrint(){
System.out.print("The answer is:"+ answer);
}
}

You have the static variable answer, but you declare answer again in Math(), so when you assign answer equal to your calculation, that's not changing the global variable, but it's changing the local variable. When you print answer in myPrint(), the answer will be 0 because it will have been assigned a value of 0 by default.

Related

The method next() is undefined for the type String

I'm new to programming and I'm trying to call a scanner from a method- and receiving the error before compiling, I could not find an answer to this in this forum or outplace, that I could understand
public class BL {
static double outcome = 0;
private static String input;
private static void main() {
Scanner input = new Scanner(System.in);
}
public static void sort() {
**input= input.next();** // the error is under .next()
}
Thanks upfront!
I guess you want
public static void sort() {
Scanner in = new Scanner(System.in);
input = in.next(); //or in.nextLine() for the whole line
}
The Scanner is the class that reads input from the keyboard. It is the class who has the method next(), that retrieves the input of the user (until it finds a space or a new line) converted in a String.

Using Scanner to take input from two objects of the same class

I have a problem with functionality of my simple program I am making at the moment. I am trying to create two objects, each of them is asking user to provide a name and then choose from few options, by using Scanner. First object, monster of class createMonster, is asking user to provide information through Scanner. However, while creating second object monster2 of class createMonster, program does not asking for user input.
Do I need to do some changes in my class CreateScanner or is it a bigger problem?
public class MainClass {
public static void main(String[] args) {
RandomMonsterGenerator monster = new RandomMonsterGenerator();
monster.createMonster();
RandomMonsterGenerator monster2 = new RandomMonsterGenerator();
monster2.createMonster();
}
}
RandomMonsterGenerator code:
public class RandomMonsterGenerator {
// Objects
Attributes attr = new Attributes();
CreateScanner createScanner = new CreateScanner();
// Variables
String monsterName;
String attributesValues;
int choice;
// Main method for generating monster
public void createMonster() {
attr.generateAttributes();
generateName();
chooseClass();
System.out.println("Generating random stats:");
attributesValues = attr.toString();
System.out.println(attributesValues);
createScanner.closeScanner();
}
// Generating monster name
private void generateName() {
System.out.println("Name your monster: ");
monsterName = createScanner.stringInput();
System.out.println("Name of the monster: " + monsterName);
}
// Choosing a class
private void chooseClass() {
System.out.println("Class descriptions: ");
System.out.println("Warrior has +2 to Strength and +2 to Condititon.");
System.out.println("Thief has +2 to Dexterity and +2 to Charisma.");
System.out.println("Mage has +2 to Intelligence and +2 to Wisdom.");
System.out.println("**************************************************");
System.out.println("Choose your class from following options: ");
System.out.println("Warrior, press '1'");
System.out.println("Thief, press '2'");
System.out.println("Mage, press '3'");
choice = createScanner.intInput();
switch(choice) {
case 1:
Warrior warrior = new Warrior(attr);
System.out.println(monsterName + " is a warrior.");
break;
case 2:
Thief thief = new Thief(attr);
System.out.println(monsterName + " is a thief.");
break;
case 3:
Mage mage = new Mage(attr);
System.out.println(monsterName + " is a mage.");
break;
default:
System.out.println("No option choosen.");
break;
}
}
}
CreateScanner code:
import java.util.Scanner;
public class CreateScanner {
Scanner sc = new Scanner(System.in);
public String stringInput() {
String input = "";
if (sc.hasNextLine()) {
input = sc.nextLine();
}
return input;
}
public int intInput() {
int input2 = 0;
if (sc.hasNextLine()) {
input2 = sc.nextInt();
}
return input2;
}
public void closeScanner() {
sc.close();
}
}
Two things.
First, don't close the scanner until you're done with it. This closes the System.in
as well and as soon as you do that you won't be getting anymore input. This is why it just skips over the second RandomMonsterGenerator input.
Second, only create one Scanner and pass it to your RandomMonsterGenerator as an argument. This keeps things simple.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
RandomMonsterGenerator monster = new RandomMonsterGenerator(scanner);
RandomMonsterGenerator monster2 = new RandomMonsterGenerator(scanner);
}

im trying to get an input from user using an edittext then convert its string to int to validate

I'm trying to get the string input from EditText be converted to int and validate the int using the if statement.
public class Main3Activity extends AppCompatActivity {
public Button but5;
public EditText et;
public String hello;
public Integer myNum;
public void main5(){
et = (EditText) findViewById(R.id.editText);
hello = et.getText().toString();
myNum = Integer.parseInt(et.getText().toString());
but5 = (Button)findViewById(R.id.button5);
but5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myNum >= 18){
Intent a = new Intent(Main3Activity.this,Main5Activity.class);
startActivity(a);
} else {
Intent a = new Intent(Main3Activity.this,Main4Activity.class);
startActivity(a);
}
}
});
}
Any solution ?
so you are a beginner. Remember that each time you ask question please copy and paste the log cat error too. It will be easy for us to debug your error.
At the moment just try
myNum = Integer.parseInt(hello);
instead of
myNum = Integer.parseInt(et.getText().toString());

Using a user input to make an object in Java

Is there a way for me to take a string input from scanner and create a new object with that string entry? Such as:
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name: ");
String name = input.nextLine();
ListOfNames name = new ListOfNames();
}
Note that in the last line the "ListOfNames name = new ListOfNames();" I want the new object to be the string that the variable "name" holds.
I want to do this so that after the object is made I can add it to an array then be able to search the array later after inputting multiple names in for the one I need.
There is probably a way to do this with the arrays class but I am new and unfimliar with the class. Any help would be fantastic! Thanks!
You could do...
public class Test {
public static void main(String[] args) {
ArrayList<String> listOfNames = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("What is your name: ");
String name = input.nextLine();
listOfNames.add(name);
}
Just add the received input from Scanner to listOfNames array list.
Or if you are really need to use another Object...
public class Name {
String value;
public Name(String value) {
this.value = value;
}
}
public class Test {
public static void main(String[] args) {
ArrayList<Name> listOfNames = new ArrayList<Name>();
Scanner input = new Scanner(System.in);
System.out.println("What is your name: ");
String nameValue = input.nextLine();
Name n = new Name(nameValue);
listOfNames.add(n);
}

Storing text from JTextField

So I have been working on this program and I cannot get the input from the 3 fields (a,b,c) to store as variables. Any help will be appreciated. Everything seems to work and if the button is pressed it closes the window but it will not proceed because the input is not stored.
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
public class Herons extends JFrame implements ActionListener {
public static JTextField a;
public static JTextField b;
public static JTextField c;
public static String aa1;
public static String bb1;
public static String cc1;
public static JFrame main = new JFrame("Herons Formula");
public static JPanel myPanel = new JPanel(new GridLayout (0,1));
public static void main(String args[]){
Herons object = new Herons();
}
Herons(){
//JFrame main = new JFrame("Herons Formula");
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel myPanel = new JPanel(new GridLayout (0,1));
//JPanel pane = new JPanel(new GridLayout(0,1));
myPanel.setPreferredSize(new Dimension(250,300));
JTextField a = new JTextField(3);
JTextField b = new JTextField(3);
JTextField c = new JTextField(3);
JButton find = new JButton("Calculate!");
main.add(myPanel);
myPanel.add(new JLabel ("Input the lengh of each side:"));
main.add(myPanel);
myPanel.add(new JLabel ("A:"));
myPanel.add(a);
myPanel.add(new JLabel ("B:"));
myPanel.add(b);
myPanel.add(new JLabel ("C:"));
myPanel.add(c);
myPanel.add(find);
//find.setActionCommand("Calculate!");
find.addActionListener(this);
main.pack();
main.setVisible(true);
String aa = a.getText();
String bb = b.getText();
String cc = c.getText();
aa1 = aa;
bb1 = bb;
cc1 = cc;
//JOptionPane.showMessageDialog(null, myPanel);
}
public void actionPerformed(ActionEvent e) {
String actionCommand = ((JButton) e.getSource()).getActionCommand();
//System.out.println("Action command for pressed button: " + actionCommand);
if (actionCommand == "Calculate!") {
main.setVisible(false);
myPanel.setVisible(false);
main.dispose();
//String aa = a.getText();
//String bb = b.getText();
//String cc = c.getText();
double aaa = Double.parseDouble(aa1);
double bbb = Double.parseDouble(bb1);
double ccc = Double.parseDouble(cc1);
double s = 0.5 * (aaa + bbb + ccc);
double area = Math.sqrt(s*(s-aaa)*(s-bbb)*(s-ccc));
area = (int)(area*10000+.5)/10000.0;
if (area == 0){
area = 0;
}
JOptionPane.showMessageDialog(null, "The area of the triangle is: " + area);
}
}
}
The problem is basically this:
class Herons {
static JTextField a;
Herons() {
JTextField a = new JTextField(); // 'a' is shadowed
}
}
When you say JTextField a = ... in the constructor it declares a different local variable called a and shadows the field.
It should be like this:
Herons() {
a = new JTextField(); // field 'a' is assigned
}
Otherwise you may have noticed that when you tried to call getText on your fields they were null in actionPerformed.
I found this page about hiding/shadowing if you want to read more.
As a side note, your variables also do not need to be static (and maybe shouldn't be since you're creating an instance of the class to refer to them).
You must get the value of the TextFields in the actionPerformed method, not in your constructor. When you get the value of these text fields in the constructor, they're empty, because they've just been added to the interface. So add this at the beginning of the method:
public void actionPerformed(ActionEvent e) {
String aa = a.getText();
String bb = b.getText();
String cc = c.getText();
and remove those lines from the constructor:
String aa = a.getText();
String bb = b.getText();
String cc = c.getText();
aa1 = aa;
bb1 = bb;
cc1 = cc;

Resources