How can I change a string input to integer-
for example-
read_line_to_codes(user_input,L),
atom_codes(C,L).
In this C is storing a string.Suppose the user entered 18.So I want to use this 18 as an integer so that I can use operations like >= with C.Is this possible in Prolog?
Prolog datatypes don't include 'strings'. SWI-prolog added it recently, but you can stay on ISO safety lane with number_codes
You can forcefully assigned an int datatype to the user input, you are letting the JVM know that you know what you are doing and this will let the program compile in case if the int data type is smaller than the user input string size. Here is an example code to help you.
public class stringToInt{
public static void main(String []args){
string C = 18; //User input, make sure to use the scanner class to get the user input value
int int_C = (int) C;
/**Your new value is now in int datatype and you can go ahead and use int_C for your arithmetic
opreation **/
}
}
Related
This code actually works fine, the question I have with my code is how do you store the enum constant in any variable, and why do we use enum? and what does the statement mean HouseType houseType;? Thank you so much in advance.
import java.util.Scanner;
public class HomeBuying {
public enum HouseType{UNKNOWN,SINGLEFAMILY,TOWNHOUSE,CONDOMINIUM};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the type of house you want to purchase"); //1.Single Family/n" " 2. Townhouse/n" " 3. Condominium/n");
int choice = input.nextInt();
HouseType houseType;
switch(choice) {
case 1:
houseType = HouseType.SINGLEFAMILY;
break;
case 2:
houseType = HouseType.TOWNHOUSE;
break;
case 3:
houseType = HouseType.CONDOMINIUM;
break;
default:
houseType = HouseType.UNKNOWN;
break;
}
System.out.println(houseType);
}
The code snippet you provided already stores an Enum value in a variable.
HouseType houseType; //declaration of variable of type HouseType (means it can store values of the HouseType enum)
houseType = HouseType.UNKNOWN; //put value into the houseType variable
We use enums whenever we need to represent values from some known and finite set. For example if you want your program to keep track of whether it is day or night, you could just make up some rule for yourself and use integers, say 1 represents day and 0 represents night. But then what the other numbers mean? Or you could just use boolean for that (again, with some arbitrary meaning attached to false and true).
enum TimePeriod{
DAY,
NIGHT
}
Enums represent a better alternative by letting you to be explicit about what values mean. This is not just a convenience - being explicit in your intentions is what makes your program readable by others.
The comments on the stemStatic method of the Morphology class state that it will:
return a new WordTag which has the lemma as the value of word().
The default is to lowercase non-proper-nouns, unless options have
been set.
(https://github.com/evandrix/stanford-corenlp/blob/master/src/edu/stanford/nlp/process/Morphology.java)
How/where can I set those options, to disable the lowercase conversion?
I've looked through the source but can't see how I can set options that will affect this static method. Frustratingly, the related static lemmatise method -- lemmaStatic -- includes a boolean parameter to do exactly this...
I'm using v3.3.1 via Maven...
thanks!
Ok after looking at this for a bit, it seems the right track might be to not use the static method, but instead build a Morphology instance with:
public Morphology(Reader in, int flags) {
The int flags will set the lexer.options.
Here are the lexer options (from Morpha.java) :
/** If this option is set, print the word affix after a + character */
private final static int print_affixes = 0;
/** If this option is set, lowercase all tokens */
private final static int change_case = 1;
/** Return the tags on the input words if present?? */
private final static int tag_output= 2;
The int flags is the bit string for the 3 options, so 7 = 111 , meaning all options will be set to true , 0 = 000 , all options false, 5 = 101 would set print_affixes and tag_output, etc...
Then you can use apply in Morphology.java
public Object apply(Object in) {
Object in should be a WordTag built with the original word and tag.
Please let me know if you need any further assistance!
We could also change Morphology.java to have the kind of method you want! The above is if you don't want to play around with customizing Stanford CoreNLP.
I'm struggling with this very simple code: I'm trying to print off "_ " marks, with one _ mark for each letter in a word inputted by a user. Whenever I try to compile the code, however, I get "error: method makeLine in class game_3 cannot be applied to given types; reason: actual and formal argument lists differ in length."
That seems like pretty clear feedback, but I don't think I really understand it - At first, I thought it was because I hadn't assigned a value to stringNumber, but assigning it a value didn't help. What's wrong?
/*Assignment:
Write a reverse Hangman game in which the user thinks of a word and the computer tries
to guess the letters in that word. Your program must output what the computer guessed
on each turn, and show the partially completed word. It also must use pseudorandom
functions to make guesses. That is, it should not simply try all the letters in order,
nor should it use the user’s input to its advantage.
*/
import java.util.*;
public class game_3 {
public static void main(String[] args) {
getIntroduction();
playGame();
}
public static void getIntroduction(){
System.out.println();
System.out.println();
System.out.println("*************************");
System.out.println("Welcome to Hangman");
System.out.println("In this game, you'll provide a word for the computer to guess.");
System.out.println();
System.out.println("The computer will guess letters randomly, and assess whether");
System.out.println("they can be used to complete your word.");
System.out.println();
System.out.println("Let's play!");
System.out.println();
System.out.println("*************************");
System.out.println();
}
public static void playGame(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a word: ");
String hangWord = input.next();
int stringNum = hangWord.length();
makeLine();
}
public static void makeLine(int stringNum){
for (int i = 0; i < stringNum; i++){
System.out.print("_ ");
}
}
}
The method makeline expects an argument of int:
public static void makeLine(int stringNum){
You're calling it with no arguments:
makeLine();
What it looks like you want is:
makeLine(stringNum);
Edit: To be clear, that's what the error message is referring to by formal argument list(expected) and the actual argument list(what you gave it). The other common error message that happens when what you give a method doesn't match what it expects is "The method methodName(expected args) is not applicable for the arguments (given args). This occurs when the types don't match up: if you pass in a String when it expects an int, or if you pass in the right types, but out of order.
I've done some elementary coding in the past, and I'm now learning Unity and trying some things with C#.
My problem:
I have a list of objects that have their own id number in the range of 1-50. I want my game to pick one object at random instead of going over the list in order. The first step would be to pick the initial id to be some random number, but I only get the error: "error CS1729: The type UnityEngine.Random' does not contain a constructor that takes1' arguments". I understand that I should give more argument for the constructor, but I need help in seeing how, since the code looks fine (if simple) to me.
Anyway, it goes like this at the moment:
public int id;
public int randomid;
public void RandId(int id)
{
Random randomid = new Random(Random.Range(1, 51));
id = randomid;
return id;
}
Here id is the identification number of the objects, randomid is for randomizing it, and I use Random.Range to create the wanted range (1-50). It seems I need to give more arguments to Random.Range, but it already has both min and max.
Can you give me some advice?
There are some other things wrong w/ your code, but this should be what you need.
public void SetIDToRandom(out int id)
{
id = (int)Random.Range(1, 51);
}
Addendum:
Random is a static class, you don't directly instantiate it.
I am trying to convert from processing to processingjs and have something I just can't understand.
In processing the following code returns whichever letter you type in, though in processingjs it just comes back with the keycode value but I need the letter not the code.
String name="";
void setup(){
size(200,200);
}
void draw(){
}
void keyPressed() {
if(key==ENTER) {
name="";
}
else {
name+=key;
println(name);
}
}
After hours of searching and the above answer I've found the answer here peepproject.com/forums/thread/266/view more eloquently than I. Basically the solution is to convert the int to a char() before constructing a String and putting it into an array.
Instead of name += key, try name += key.toString().
Processing's println automatically does type conversion for you, so the char value of PApplet.key gets printed as a letter. JavaScript string concatenation works differently; the integer value of PApplet.key will be appended to the string as an integer and will not automatically be converted to a string. You have to do it manually.
You need to use the char conversion function in Processing and Processing.js:
http://processingjs.org/reference/char_/
The reason why it's displaying as a number is this line:
char x = 97; //supposed to be an 'a'
Does a cast in Java (may require char x = (char)97).
However in processing.js it is executed as:
var x = 97;
Since javascript has dynamic typing. You therefore need to explicitly force type casts such as from int->char.