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 need to force the user to enter an integer between 1 an 11. The code below works great.... It will work fine if the user inputs any letter or character and reprompt for a number. However once a number is entered(a number out of range-or it would just accept the number) and then the user enters a letter or character that isnt an int the program crashes with a input mismatch exception. I see WHY it is doing this but I cant figure out how to put my while loops to fix this! Here is the code. Someone suggested to Catch the exceptions, then prompt for retry if I encounter one.. however I havn't used the try - catch for exception handling before. Here is my code.
public static int getDecadeSelection() { int decadeChoice = 0; System.out.println( " Choose your decade: " ); System.out.println( " 1 - 1900-1909 " ); System.out.println( " 2 - 1910-1919" ); System.out.println( " 3 - 1920-1929" ); System.out.println( " 4 - 1930-1939" ); System.out.println( " 5 - 1940-1949" );
[Code] ....
It is because I am setting decadeChoice to console.nextInt, so once it passes that if the user inputs a letter it will crash, just not sure how to make it work.
The project is to develop the game Translate the Word .... It is asking user to translate a word proposed to and check if the input response is correct. At the end of the game score will be calculated and displayed.
Game Play :
1 - Ask the user to specify , through the console , its name and the number of words to offer . It is up to you to handle exceptions (eg number of words greater than the number you provided ) 2 - Recover user response ( the word translated ) and check whether to continue . (eg you want to continue (y / n)) after each proposal. 3 - compare the response of the user with that which is preset for the word in question . 4 - Show the score at the end ( or at the breakpoint ) . 5 - Save the file in a user name , the score , the number of questions and the start date and end of the game played .
Some notes to consider :
1 - The language (eg, English - French , English - Arabic , etc. . ): It is up to you to specify the language adopted in the game and inform the user of your choice. 2 - The word bank to offer : It is up to you to develop the appropriate means to get the words to propose to the user. That said , the words and their translations can be retrieved :
a. a TXT file b . an XML file . ( Tutorials DOM and SAX ) c . CSV file ( OpenCSV Tutorial ) d. a database ( Tutorial Access) e . through APIs (eg Wordnet and google translate etc . ) . f . a combination of the previous options a, bc , d and / or e . (eg words stored in a txt file and answers retrieved from the api google translate) g . etc. .
Examples of files and databases are attached to the project statement . You will need to add one or more external libraries to your project. Click here for details on adding external libraries to Netbeans .
3 - A user will be associated with the question score if he can translate the word correctly. The score for each question can be calculated based on the number of words / questions to be proposed .
Development : In this project you will need at least a class called Question to encapsulate the word and its translations and provide all necessary methods to manipulate the object type Question.
An interface called IParser to make extensible project. Any class that implements IParser is a parser file (XML , TXT , CSV , etc.). / Database. In your project there will be a single class that implements IParser and will be used to retrieve words and their translations.
Add the ability to store the questions and answers of the user on the hard disk. Make the class Serializable Question
By using FileReader, FileWriter and their constituents, I am creating a file to contain employee information ( name, age, hours, etc. ). The user is to input all of the data on a single line and we were asked to implement StringTokenizer to assign that data to the file. I have never used the StringTokenizer before, but I have a rough idea how it is to function. I used pw.println to test what I have so far, now I would like to let the user build the initial file with the "first employees" of the company, and then view the file, and then go back and append new employee data to that same file. My question is, how can I take the user input as a StringTokenizer and add that to the file?
In the for loop below, I thought I would see if it would work, but it does not. The loop only executes once and does not allow me to enter data.
public class Records { public static void main(String [] args) throws IOException { Scanner input = new Scanner(System.in); FileWriter fw = new FileWriter("dbs3.java"); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); System.out.println("NEW EMPLOYEE DATA SHEET"); System.out.print("Number of new employees: "); int number = input.nextInt();
Every time I input a number for fahrenheit, I always get 273.15. Everything compiles, the only problem is that run-time error. I need getting fahrenheit to exactly change to kelvin.
//my class public class TempConversion { private double fahrenheit; public TempConversion() { fahrenheit = 0;
I am trying to remove a line based on user input. myFile.txt looks like:
Matt Brian John
However when I enter "Brian" (to remove this line), It is deleted on the temp file (myTempFile.txt), but not renamed back to the original file (myFile).
I am doing an assignment where I have to find the price per square inch of a pizza, compare them and display the results. I have everything figured out with the values and stuff. Now when I have to displays the results I have to display which of the two pizzas is more favorable.
I have both values / square inch for both. and I know how to find the minimum value of the two wit the Math.min class. My question is how can I assign the char, PIZZA A to the value that I had so I can display it in the output statement, without writing PIZZA A. It should display after the difference is calculated.
Here's my code so far.
// This programs finds the price per square inch of a pizza
import java.util.Scanner; import java.text.DecimalFormat; public class PizzaSquareInches { public static void main(String[] args) { Scanner input = new Scanner(System.in); DecimalFormat df = new DecimalFormat("#.###");
simple assignment of values to a previously initialized object?
See the method useModel ()
The idea is, assign the values to the temporary object, data
Then plunk it into this statement:
model.addRow ( data );
Simple enough?
I've been putzing with the syntax for multiple hours, over days, now.
With and without
[0];,
Netbeans keeps giving me: Illegal start of expression data is declared as an array of Object, although, in this case, it does not need to be an array. What is the correct syntax?
I've been referencing my text and a few other sites to assist in building this class...and I'm still uncertain of the purpose of a few of the methds: next(), hasNext().
Also, I have not found a clear explanation of the following code example: tail.next = tail; There are several instances of this in the code below....I'm just not sure exactly how this assigns the value to the next object in the other class..?? ??
public class MySinglyLinkedList<T> implements SinglyLinkedList<T>{ protected NodeList<T> head, tail, current, newNode; String name; int size = 0; public MySinglyLinkedList(){ head = null; tail = null;
Can I assign multiple values to one variable? For example I want myNum = 0 thru 9 you see im trying to program a password checker program to verify that the password meets all the criteria 8 char long, 1 upper case, 1 lower case, 1 numeric, 1 special, and don't contain and or end
How do I assign job in the scriplet like how we do <% String job = request.getParameter("job")%> but on the same page? <% String job = request. getParameter("job")%> returns null value.
Let's say I have a loop that loops through objects in an ArrayList then does stuff with them. Is it better for me to store the object in a temporary local variable and do stuff with it, or always use the ".get(arrayindex)" thing?
i am trying to assign unique values to nodes read from XML file.. eg: consider this XML file:
<breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> ...Strong Belgian waffles...</description> <calories>650</calories>
[code]....
now assigning these nodes "a unique value" has to be done following the LSDX labelling pattern i.e:
To the document element we first give an “a”.As there is no parent node for the document element, we assign “0” at the front of that “a” . “0a” is the unique code for the document element (breakfast_menu). For the children nodes of “0a”, we continue with the next level of the XML tree which is “1” then the code of its parent node which is “a” and a concatenation “.” . We then add a letter “b” for the first child, letter “c” for the second child, “d” for the third child and so on.Unique codes for children nodes of “0a” shall be “1a.b”, “1a.c”, “1a.d”, etc.Hence foe the above given XML the mapping would look something like this:
0a breakfast_menu 1a.b food 2ab.b name 2ab.c price 2ab.d description 2ab.e calories 2ab.f chef 3abf.b chef1 3abf.c chef2 1a.c food 2ac.b name 2ac.c price 2ac.d description 2ac.e calories 2ac.f chef 3acf.b chef1 3acf.c chef2
For more samples about LSDX labelling : 1.) Section 3.1 LSDX Labelling on this link: [URL]
2.) Fig 3 on page 1189 on this link:[URL]
right now i am using SAX parser to read xml and get the nodes in their hierarchical order..now the problem is that i have to assign these specific value to their respective nodes using java.
I am trying to create an empty array that has no assigned length as the amount of elements it needs to hold will be dependent on another value. I then want to use a while loop to assign values to it. Here is an example of what im looking for it doesnt work. Iam trying to do:
int x = 12; int i = 1; int k = 0; int[] factors = {} while (i<x) { if (x%i==0) { factors[k] = i; k++; i++;
I am trying to assign random numbers to a deck of cards without repeating the numbers. What am I doing wrong?
package hokm; import java.util.Random; import java.util.Scanner; public class Hokm { public static void main(String[] args) { int [][] number=new int[52][2];
Write a program that reads student scores, gets the best score and then assigns grades based on the following scheme:
Grade is A if score is >= best - 10; Grade is B is score is >= best - 20; Grades is C if score is >= best - 30; Grade is D if score is >= best - 40; Grde is F other wise;
The program prompts the user to enter the total number of studeents, then prompts the user to enter all of the scores, and concludes by displaying the grades.
import java.util.*; public class AssigningGrades { public static void main(String [] args) { Scanner scan = new Scanner(System.in); int studentNumber = 0; int classScore = 0;
[Code] ....
So when I ran into problems when populating the array and I made changes. Then all of a sudden the program doesn't recognize classSize[i] at the System.out.print line.
I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.
here's the code for the shuffler Button
shuffler.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for(int i=0;i<shuffeledMessages.length;i++){ int randoma=(int)(Math.random()*4); int randomb=(int)(Math.random()*4); Collections.swap(myMessages,randoma,randomb); } } });
For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
How would I randomly assign colors and a with a name? Some functionality may require command-line arguments but I am a bit confused on how to structure this. Also, these names must appear on a color coded on a gui
At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day. B. Print the day. C. Return the day. D. Return the next day. E. Return the previous day. F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday. G. Add the appropriate constructors. H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G. I. Write a program to test various operations on the class Day.
import java.util.*; public class Day { static Scanner readinput = new Scanner(System.in); String day; public Day(String day) {
[Code] ....
So right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).
public class CollisionManager<T> { private boolean collision = false; private T mainEntity; public <T extends Entities> void handleCollision(T mainEntity, T secondEntity){ this.mainEntity = mainEntity; // This is illegal. } }
Why "this.mainEntity = mainEntity" is incorrect and also show me the correct way to achieve this?
The error I am getting is "Type mismatch: cannot convert T to T"
1) I'm trying to use a logarithm to determine the length of a user input number. I keep getting an error stating <> indetifier expected. I'm assuming this means that the program is not recognizing the function of a logarithm. I know that normally you can include that information in the method, but my teacher has stated specifically that each of these methods be called something else, as shown in the code.
2) I'm not quite sure I understand how to assign the numbers I obtain from the modular equation to a certain position in the array. As I'm asking the user to input any number these values can change so therefore I can't simply state that first number = this place.Here is my code:
import javax.swing.*; import javax.*; public class getSize { public static void main( String[] args )