Define Variable When User Has To Type Into Program So It Can Be Converted
Sep 23, 2014
I don't understand how i'm supposed to define a variable when the user has to type it into the program so it can be converted. This program i'm working on is supposed to convert from meters to feet (meter = feet * 0.305) but the user has to type in the meters they want converted. how do i solve the problem that keeps coming up that says "variable meter symbol cant be found" ? I keep trying different lines to no avail
public class Lab2
{
public static void main(String[] args) {
double feet = meter * 0.305;
double meter = feet/0.305;
System.out.println("Enter in feet for conversion to meters");
meter = meter * 0.305;
I am writing a program to store books (Book class) in volumes (Volume class). Within the main class (DemoVolume) I am doing a couple different things. I want to ask the user if they are creating a new array, if yes, I was to create a new array with a variable names assigned by the user. Is this possible? The reason is I want to allow the user to be able to search their library of volumes. If no, then the user will enter the book information and I will assign it to an array unassignedVolume that will contain individual books. I would also like to create book objects that are defined with the book name, if this is possible. I'm just messing around and trying to deepen my understanding of JAVA. My main issue is how to allow the user to title the volume (array variable).
import java.util.Scanner; public class DemoVolume { public static void main(String[] args) { Scanner input = new Scanner(System.in); String [] volume, unassignedVolume; Volume volumeOne = new Volume("JAVA", 3, volume);
Ok, so I have to make a program that tells the user to type a sentence and at the end of the sentence a "." must be put to end the program. If a "." period is not entered, it must re-prompt the user to enter a period. On top of all this, the program has to count the number of letters in the sentence. I cant seem to get the while loop working and I cant find anywhere how to make a counter count the number of letters.
import java.io.*; class Sentence //The purpose of this program is to
I am trying to write a password checker program and thats the error i get. (the final error!) but this one i dont know how to fix. here is my code.
/* Text Text text */
import java.util.Scanner; // Imports the Scanner class to get Keyboard Inputs class PriceRichardChapter5Test { public static void main (String [] args) { String Password; // Creates the Password variable
I am trying to understand Life time of a variable by writing a below program. But unable to compile it as it is throwing some error. Code snippet and error as follows,
// Understanding Lifetime of a variable. class LifeTime { public static void main(String args[]) { int x; for(x = 0; x <3 ; x++) { int y = -1; // y is initialized each time block is entered
[Code] .....
Please refer to the print screen attached 'LifeTime.jpeg' to this thread to know more about error.
I'm making a Hangman program for Java, here's my code:
import java.util.Scanner; public class Hangman { /** * @param args the command line arguments */ public static void main(String[] args) { String answerWord = WordList.getWord();
[Code] .....
The Compiler gives me this only error:
Hangman.java:39: error: unexpected type displayWord.charAt(i)=userInput.charAt(0); ^ required: variable found: value
I understand that charAt returns i as a value and not a variable. What do I use to return a variable as I intended?
public class Class1 extends AbstractClass { //stuff } public class Class2 extends AbstractClass { //stuff }
within another class I have a private variable with the type of the Abstract class, and within one of the methods I assign an object to the the variable like this:
public class Test { private AbstractClass temp; public testMethod(){ Class1 anObject = new Class1(); temp = anObject; } }
I have created an enumerated data type that represents months. In my program I have asked the user for the month. I hold the month entered by user into a string variable. From that string variable I'm trying to display the month using my enumerated data type. I know I can use if statements, but is there another simple way to do it?
import java.util.Scanner; public class demo { //Enum class called month that has constants in it representing months enum month{january,february,march,april,may,june, july,august,september, october,november, december};
I have a Homework that's on Polymorphism and inheritance. The problem is of a triangle class. The super class is GeometricObject and the subclass Triangle. I have the parent and child classes compiling, now it's the test program.
The problem is the examples in the book don't show anything about prompting input for objects. The objects in the Triangle class are: side1, side2, and side3 and so I created objects in the test: Triangle sideOne = new Triangle(); . I've tried to compile but jGrasp doesn't like anything I do and the instructor in my class hasn't shown us examples yet. Can I prompt input for reference type and not just primitive type?
Okay, so I know how to get user input and create a variable from the input, create a basic addition sum etc...
So I have this code:
package calc; import java.util.Scanner; class calc{ private static final double add = 0; private static final double subtract = 0; public static void main(String args[]){
[Code] .....
I'm basically trying to make it so that when the user enters tnum, tnum2, tnum3 and tnum4, their answer turns to the variable which has to be either true or false to make the boolean work. I don't know really how to do this. I want to make it so that if they enter yes, then that makes the boolean true and the numbers will multiply and create the answer variable. If they enter no then the boolean is false and it moves onto the next if statement. How can I do this?
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
I need the user to be able to input a number, and for the program to assign this value to an 'int' variable. I know how to do this with a 'string' variable:
Java Code:
String options = JOptionPane.showInputDialog(null, "In your decision, how many options do you have? " +" (NOTE: The maximum number of options = 5, and you must enter your answer as a numeral.)"); mh_sh_highlight_all('java');
But I need to know how to do this with an 'int' variable.
I'm trying to build a program that will output what will ultimately look like a simple mario level turned on its side. As part of my output I need the user to define what mario looks like. I do this using Scanner and save the input to String mario. When I try to use that variable in another method it gives me troubles.
import java.util.Scanner; public class Mario2 { public static void mario() { //user defines mario String mario = ">->O"; Scanner keys = new Scanner(System.in); System.out.println("What does mario look like?"); mario = keys.next(); System.out.println("Mario now looks like: " + mario);
I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,
class thisA {} class thisB extends thisA { String testString = "test";} public class CastQuestion2 { public static void main(String[] args) { thisA a = new thisA(); thisB b = new thisB();
I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?
I am trying to use a JFrame to open up extra windows that will prompt the user the Log In or Out, but I am getting an error with one of my variables that writes to Excel. There is something wrong with the variable "sheet" contained in the "while(i <= 4)"
private void createLabel(WritableSheet sheet) throws WriteException { WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10); times = new WritableCellFormat(times10pt); times.setWrap(true); WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.SINGLE);
so i'm following a java tutorial from the book and it has a few challenge questions. and i'm stucked on one. i think i just don't understand what is it that its asking me. heres the question, Write a statement that reads a user's input integer into the defined variable, and a second statement that prints the integer. assuming scanner is given, and i checked my heading code is ok.
Scanner scnr = new Scanner(System.in); int userNum = 0; System.out.println("What is the product of 8 time 2"); userNum = scnr.nextInt();
this is my program error occured run time,but compile sucessfully
{error msg-Error: Main method not found in class helloworldapp.HelloWorldApp, please define the main method as: public static void main(String[] args)} class HelloWorldApp { private int empno; private float salary; void setHelloWorldApp() { empno=12; salary=1200;