How to generate a random font in java? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm completely new to java and coding in general and I need to make a program display certain text using different fonts each time randomly.

This question should be split into two parts. How can I list all available font family names in Java? and How to pick a random index of an array?. You should always try to split your problems into separate smaller sub-problems.
Here are the answers to both problems: A and B.
I made a neat function out of it which picks you the name of a random font family. If the language of the text that should be rendered is not English, replace Locale.ENGLISH with your language.
private static final Random RANDOM = new Random();
private static String pickRandomFontFamily() {
String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.ENGLISH);
return availableFonts[RANDOM.nextInt(availableFonts.length)];
}

Related

I have a string with a number and a letter, is there a way to move the integer into a separate int variable? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So I'm working on a school project and it includes a Combined Gas Law calculator where the user may input the temperature in °celsius (e.g. 1°C) and the code converts it to kelvin; if the user's input is in kelvin already, it does not do that and continues with the equation. So does anyone know how I can separate the two data types into two different variables in Java?
You could split over a specific string (degree character for example), store in a String array and parse the first element. Something like this:
String str = "47°C"
String[] strArray = str.split("°");
int number = Integer.parseInt(strArray[0]);
Congratulations, you are working on something but not writing it's code. I can tell you a few tips about how to implement it's code.
You have a string that has some numbers in it, and also has unit. Try searching 'How to extract numbers from a string'. Now you have a number.
Find the unit in the string by looking the last character in the string.
If the condition is ok for your homework, calculate the new value.
Print the result.

How to Name Painted Graphics? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
So I was wondering if you could give a name to something that was painted.
Example:
`public void paint(Graphics g) {
g.drawRect(int,int,int,int);
}`
Is it possible to give that rectangle a name so I can reference it in later code.
I am not sure for what exactly your are asking, but here's how you can save a 2 dimensional grid of variables:
String[][] rectangles = new String[100][100];
This above creates a field of Strings, 100*100 Strings big.
If you want to set the value for the field at x and y you do:
String[x][y] = "Hello World";
I did this with a String field, but you can use your own classes or something like int[][] as well. Depending on if you choose String[][], int[][] or yourClassName[][] you can store Strings, ints or yourClassNames in the field.

how to get values from a Text view android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store values from text view for example i am performing the following plus function like 5+6.Now i want to store 5 as value-1 and 6 as value-2.I only have one text view where i am displying like 5+6 in my project.I search a lot about my issue but could not found any proper solution. thanks
try this:
String string =text.getText().toString();
String[] separated = string.split("+");
String firstValue=separated[0];
String secondValue=separated[1];
You can try this and you will able to get the addition of two numbers at once. .
String[] getText = textview.getText().split("\\+");
int total=0;
for(String s:getText){
total+=Integer.parseInt(s);
}
in the end of iteration total will give the final result.From this method it gives the total of any numbers of numbers.

Get a Java string of form (num/num), to become two double values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For example, if I have a string (24/25), how would I go about being left with two double values of 24 and 25. My objective to to eventually, given a set of number of that form, divide each of them, and add to get the average. I'm fairly new to Java and I honestly am so confused as where to even begin. Thank you!
As Andreas said. First you need to take the substring between brackets. Use String.substr
Now split 24/25 to 24 and 25. You can use String.split
Then you can parse them with Double.parse

Creating an array from a text file in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I know how to create arrays like so:
int[] myIntArray = new int[]{1,2,3};
My question is this. What if I had a file, titled Lab11Input.txt, and filled it with integer values. How would I go about creating a method that opens the file, counts the number of numbers in the file, creates the array, then fills the array with the values from the file?
For example, if I pass in Lab11Input.txt as an argument, could I do it that way?
A simple way using Scanner:
Scanner sc = new Scanner(new File ("Lab11Input.txt"));
List<Integer> ints = new ArrayList<>();
while(sc.hasNextInt()) {
ints.add(sc.nextInt());
}
// then you can convert ints to an array

Categories