How do i extract the first number [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 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)));

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);
}
}

Merge letters to form a string and compare with already existing string? I'm very new to java, how can i write the logic? [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 2 years ago.
Improve this question
Sample input 1:
Enter Ist letter:J
Enter 2nd letter:A
Enter 3rd letter :V
Enter 4th letter:A
Sample output 1:
JAVA
Sample input 2:
Enter Ist letter:J
Enter 2nd letter:A
Enter 3rd letter :A
Enter 4th letter:V
Sample output 2:
Wrong spelling
Try the following code.
public class Test {
public static void main(String[] args) {
String finalString = "";
Scanner sc = new Scanner(System.in);
for (int i = 1; i < 5; i++) {
System.out.println("Enter " + i + "letter");
String letter = sc.nextLine();
finalString = finalString + letter;
}
if (!finalString.equals("JAVA")) {// you can test it with any string by replacing java
System.out.println("Wrong spelling");
} else {
System.out.println(finalString);
}
}
}

need to determine the positive, negative and zero numbers in program and add all the positive and negative numbers separately [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 4 years ago.
Improve this question
I need to determine the positive, negative and zero numbers in a program and add all the positive and negative numbers separately. I'm using while loop (can use do-while) because for loop and array is not allowed. Badly need your help. Here's my code. The code should allow entering 10 numbers before determining.
public class Mix22 {
public static void main(String[] args) {
Scanner ety = new Scanner(System.in);
int count=0;
int positive=0;
int negative =0;
int num=0;
System.out.println("Enter a number: ");
num = ety.nextInt();
while(num!=10){
if(num<0)
negative++;
if (num>0)
positive++;
System.out.println("Enter a number: ");
num = ety.nextInt();
}
System.out.println("Negative numbers in the program: " + negative);
System.out.println("Positive numbers in the program: " + positive);
}
}
Is the problem that you want to run the loop 10 times? You've got a count variable that you are not otherwise using. The loop should look something like:
int count=0;
while (count != 10) {
...
++count;
}
Conventionally a for loop is used for this (if allowed):
for (int count=0; count<10; ++count) {
...
}

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.

Displaying asterisk depending on the number entered [closed]

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)
{
}

Categories