Displaying asterisk depending on the number entered [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
What is the code for making a simple program that displays asterisk depending on the number entered by the user. I want it to go like this
Enter 1st Integer:2
Enter 2nd Integer:3
Enter 3rd Integer:4
Enter 4th Integer:5
and this would be the result
1st Integer:**
2nd Integer:***
3rd Integer:****
4th Integer:*****
Simple as it is, I'm having problems regarding this code, I just need the loop for this.

int inputNumber = 2;
StringBuilder asteriskBuilder = new StringBuilder();
for (int i = 0; i < inputNumber; ++i) {
asteriskBuilder.append("*");
}
// 1st Integer:**
System.out.println("1st Integer:" + asteriskBuilder);

use StringBuilder
StringBuilder builder = new StringBuilder();
for (int i = 0; i < inputNumber; ++i) {
builder.append("*");
}
System.out.println(builder.toString());
if your input number can be big, StringBuilder is better solution for you

try
{
Scanner sc = new Scanner(System.in);
for(int i=1;i<=4;i++)
{
System.out.print(i+"st Integer:");
int input=sc.nextInt();
System.out.print(i+"st Integer:");
for(int j=0;j<input;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
catch(Exception e)
{
}

Related

Java: program reading multiple integers from console and returning evaluated sequence [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 2 years ago.
Improve this question
I would very much appreciate your help.
The prompt: Write a program that reads from the console a sequence of n integer numbers and returns these numbers on a single line with the correct sign (<, > or =) between the numbers.
I have no idea how to construct the answer.
Thanks in advance:
desperate novice
If this is your homework question, these are the hints:
To read from console, you can use these options:
System.console.readLine()
System.in.read()
Scanner class - Scanner myScanner = new Scanner(System.in); then myScanner.nextLine()
You can split the inputs via String.split(",") if they are seperated by commas
You can convert the splitted input strings into Integer by Integer.parseInt(<strings from last step>) by iterating over them
Then you can take any 2-2 input and compare them and add to string with correct sign via concatenating.
Then you can output them into console.
imports:
import java.util.Arrays;
import java.util.Scanner;
Logic:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine()); // No. of input to read
int[] inputs = new int[n];
for (int i = 0; i < n; i++) {
inputs[i] = Integer.parseInt(scanner.nextLine()); // inputs
}
// Arrays.sort(inputs) if you want the output to be sorted
for (int i = 0; i < n; i++) {
System.out.print(inputs[i]);
if (i == n - 1) continue;
String sign;
if (inputs[i] > inputs[i + 1]) {
sign = " > ";
} else if (inputs[i] < inputs[i + 1]) {
sign = " < ";
} else {
sign = " = ";
}
System.out.print(sign);
}
}

Creating a random output from an user input array [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
public class decisionMaker {
public static void main(String args[]) {
String option[] = new String[10];
// Output
for (int i = 0; i <= 9; i++) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the next option:");
option[i] = input.next();
System.out.println(" ");
}
for (int i = 0; i <= 9; i++) {
System.out.println("option: ");
System.out.println("option[i]+" ");
}
// Output
}
I'm trying to figure out how to add a count to the options, exit and end the program after entering a certain letter or number, and how to create a random output from the user input. I want it to give me one option that I had input at random. Can anyone help me with one or a few of these things. I'm trying to learn to code on my own, and I'm stuck on these.
Randomness
You can generate random numbers using java.util.Random;:
import java.util.Random;
public class SomeClass{
static Random rand = new Random();
public static void main(String args[]){
System.out.println(rand.nextInt());
}
}
About some broken code:
If you want to print out the value of a variable with System.out.println() then you need only type the variable without any quotation marks. The code you've written below will not compile:
System.out.println("option: ");
System.out.println("option[i]+" ");
Assuming that's what you want to do, it should instead be written as:
System.out.println("option: ");
System.out.println(option[i]);
Or even System.out.println("option: \n"+option[i]);
(The escape sequence \n when placed inside of quotation marks just indicates to the console to add a new line.)
Scanner:
Additionally, as nick zoum pointed out, your Scanner object should be initialized outside of the for loop, such as right underneath of the main() method.
Please comment below if you need clarification or if I misunderstood what you were looking for. It was very hard to understand your question.
You could try something like this:
public class DecisionMaker {
public static void main(String[] args) {
// output
Scanner scanner = new Scanner(System.in);
int size = getInt(scanner);
String option[] = new String[size];
for (int index = 0; index < size; index++) {
System.out.print("Enter the next option:");
option[index] = scanner.next();
}
int index = (int) (Math.random() * size);
System.out.println(option[index]);
scanner.close();
// output
}
public static int getInt(Scanner scanner) {
int size = 0;
while (size <= 0) {
if (scanner.hasNext()) {
if (scanner.hasNextInt()) {
size = scanner.nextInt();
}
}
if (size <= 0) {
System.out.println("The input: " + scanner.next() + " is not a valid value.");
}
}
return size;
}
}
How the program works:
The Scanner is initialized in the beginning and there is only
one instance of it.
Then the program will wait until the user inserts a valid number for
the size of options.
The next 5 lines were essentially copied from your code.
Finally we get a random Integer in the range of 0 - (size - 1) and print
the String of the array with that index.

How do i extract the first number [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
so basically I want the user to do a input through the console and I want to finger out the first number and give it out on to the console, like for example:
hello465924whats334up // userinput
465924 // console output
this is basically the code that i have till now:
import java.util.Scanner;
public class ZahlZusammenFueger {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter something!");
String e = s.nextLine();
}
}
Since that number is substring, find the indexes where it begins and where it ends. Like this:
int i = 0;
int j = 0;
Scanner s = new Scanner(System.in);
System.out.println("Please enter something!");
String e = s.nextLine();
while (!Character.isDigit(e.charAt(i))) i++; // finding index
// where substring of first number starts
j = i;
while (Character.isDigit(e.charAt(j))) j++; // finding index
// where substring of first number ends
String number = e.substring(i, j));
Now, you can make Integer from it, or Long (depending on size) by doing this:
System.out.println(Integer.parseInt(e.substring(i, j)));
System.out.println(Long.parseInt(e.substring(i, j)));

For loop runs only once [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying a for loop piece of code, where the user inputs the number of words he will write and then afterwards the words itself, and at the end it should check for specific requirements (words that sstart with K increase the count value and also the last word that started with K will get registered). However for some reason unknown to me, it just wont loop, no matter the input it just prints out everything on the code.
package Others;
import java.util.*;
public class StartsWithString
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int wordNumber = 0;
String words = "";
int wordCount = 0;
String lastWord = "";
System.out.println("How many words are you going to write?: ");
wordNumber = sc.nextInt();
System.out.println("Write the desired words: ");
for(int i = 0; i < wordNumber; i++);
{
words = sc.nextLine();
if(words.startsWith("K"))
{
wordCount++;
lastWord = words;
}
}
System.out.println("From a total of " + numriFjaleve + " words typed,);
System.out.println(wordCount + " started with the letter K.");
System.out.println("The last word typed which began with the letter K was: " + lastWord);
}
}
for(int i = 0; i < wordNumber; i++);
You have a semicolon at the end of your loop definition. So your loop isn't actually a loop. It's a standalone code block which runs once. Remove the semicolon.

User input string into string array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to get the user's input, then store the input into an array. I'm going to get a string input and with this code, I thought it would work, but it's not working. Any help would be appreciated!
import java.util.Scanner;
public class NameSorting {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println("Please enter 20 names to sort");
Scanner s1 = new Scanner(System.in);
for (int i = 0; i < 0;){
array[i] = s1.nextLine();
}
//Just to test
System.out.println(array[0]);
}
}
Since you know that you want to have an array of 20 string:
String[] array = new String[20];
Then your for loop should use the length of the array to determine when the loop should stop. Also you loop is missing an incrementer.
Try the following code to get you going:
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
String[] array = new String[20];
System.out.println("Please enter 20 names to sort");
for (int i = 0; i < array.length; i++) {
array[i] = s.nextLine();
}
//Just to test
System.out.println(array[0]);
}
Look at your for-loop, it lacks of increment attribute. Example: for(int i = 0; i < 0; i++)
If you want to debug each loop I recommend you to print assignment inside for-loop
for (int i = 0; i < 0;)
{
array[i] = s.nextLine();
System.out.println(array[i]); // Debug
}
for (int i = 0; i < 0;){
array[i] = s.nextLine();
}
For the first iteration i will be initialised to '0' and since i should be less then '0' as per your condition it wont even go into the loop.change the loop to
for(int i=0;i<20;i++){
array[i]=s.nextLine();
}

Categories