I need to accept user input immediately upon running my program [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 4 years ago.
Improve this question
First of all, I'm extremely new to Java, so simple answers would be much appreciated.
I'm trying to write a program so that the user can either input the following:
java howManyFiles 10
and get the output:
You selected 10 files.
Or, the user can input the following:
java howManyFiles
and get the output:
How many files would you like?
I can't seem to figure out a way to create a scanner that reads the initial input. The nextLine() scanner method looks for a new user input, rather than checking to see if one already exists. Any help would be a life saver. Thanks

java howManyFiles 10 seems like 10 is a command-line argument. Those arguments appear in the String[] argument to main. Here's a simple example:
public static void main(String[] args) {
if (args.length == 0) {
// User didn't provide number of files.
// You need to prompt the user and read their input.
} else if (args.length == 1) {
int numFiles = Integer.parseInt(args[0]);
System.out.println("You selected " + numFiles + " files.");
} else {
// User provided two or more command-line arguments.
// You might want to print an error message in this case.
}
}

Related

Recursions in Java [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 10 days ago.
This post was edited and submitted for review 10 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm starting with recursion in Java, and I'm having a problem with a counter, where I want to print all numbers from 1 to number, but it only prints the value of i. i was defined at the start of the class as:
int i = 1;
I've tried putting
i>=number
number>=i
i>=1
number>=0
number>=1
number>0
number>1
After getting rid of i, I also tried:
public static void ContadorCreciente(int number) {
if (i <= number) {
System.out.println(i);
ContadorCreciente(i++);
}
}
But now, this is what I've written so far:
public static void ContadorCreciente(int number) {
if (number > 1) {
ContadorDecreciente(number - 1);
System.out.println(number);
}
}
Ok, you're implementing ContadorCreciente with one number argument. Recursive, so you're going to call it again with either number+1 or number-1. For the +1 variant you have no way to stop it, that would keep going up forever, so that's no good, the recursive call has to be with -1.
The -1 recursion can stop e.g. at ContadorCreciente(0) which is expected to do nothing. I'm sure you can figure out how to do nothing and return immediately.
Inductive approach:
Assuming we already have ContadorCreciente(number-1) which prints all numbers 1..number-1. How would you use such a function to implement the case ContadorCreciente(number)?

Java error how (Cannot convert double to bolean) [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 1 year ago.
Improve this question
I have been getting back in to Java after not using it for a while and tried to make a calculator one that has more then just +. However, I don't have a lot of experience coding, so I don't understand what I did wrong. It says I cant turn a double into a boolean, but that's not what I'm doing. Here is the code I get an error on.
if (One = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Plus +"!");
}else if (Two = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Sub +"!");
}else (Three = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Multi +"!");
You should use "==" for comparison in if blocks.
Single = sign is an assignment operator and does not return the boolean (logic) value that is necesery to check condition in the if ... else statement. Insteed = you should use == operator to compare values or Objects.equals method.

Function to be used by others - how to export so that code isn't exposed [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
I wrote some code in java and I want to have it to be an exe file or any other form of user interface for other developers to use it
EDIT
what I have used in the end was exporting to jar as user user85421 suggested.
The default GUI classes of Java are called Swing. You should buy a book about Swing or take a look at any tutorial like this: https://www.guru99.com/java-swing-gui.html
If text-only input and output is OK, then just use the classes Scanner and System:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter two numbers:");
int number1=scanner.nextInt();
int number2=scanner.nextInt();
System.out.printf("%d + %d = %d\n", number1, number2, number1+number2);
}
}
Outputs:
Enter two numbers:
3
4
3 + 4 = 7

Java Basic I want to prompt user 9 digit zip code [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 8 years ago.
Improve this question
I am very new to Java, but I am really enthusiastic to learn it. I would like to solve the problem by myself step by step
I tried to do my homework piece by piece.
I want to ask users to put 9 digit zip code, for example 701152014, not for 5 digit like 70115.
I wanted to keep asking users until they type 9 digits like 701152014
if they put 5 digits I want to keep asking please type 9 digit.
I use NetBean.
System.out.println(zipNew); This part, something wrong with it.it says error.
so I wanted to prompt user until users would type 9 digit zip code.
how can I do that? Thank you so much.
Thank you so much for teaching me. I really would like to learn Java. Thank you.
package week7;
import javax.swing.*;
public class test {
public static void main(String[] args){
//Scanner scanner = new Scanner(System.in);
String zip=JOptionPane.showInputDialog(null,"Enter your zip");
String zipNew;
int ziplength = (zip.length());
if (ziplength == 9){
zipNew = zip;
}
else if (ziplength !=9)
{
String zip = JOptionPane.showInputDialog(null,"please type 9 digit zip code not 5 digit"); //----this part is wrong
}
System.out.println(zipNew); //----this part is wrong,
}
Normally, I'd use a JFormattedField and/or DocumentFilter, but lets keep it simple...
The basic idea is, you need to loop until you get what you need, for example...
String zip = null;
do {
zip = JOptionPane.showInputDialog(null, "Enter your zip");
} while (zip != null && zip.length() < 9);
System.out.println("zip = " + zip);
This will loop until the user presses [Cancel] or the value they enter has 9 characters. You need to beware, this can result in zip been equal to null and you will need to check for this. Also, there is nothing stopping the user from entering non-numeric values...
Take a closer look at The while and do-while Statements for more details
First off, the formatting is terrible. Not sure if it occurred when posting it here and you didn't actually use that formatting, but you might wanna make it more readable.
You declare the String zip twice. You can't have two variables of the same name in the same class, so in your else if condition (you don't need the if ziplength != 9 as the else guarantees that condition is true) change the name of the String or don't declare the variable again (e.g. say zip = blahblah not String zip = blahblah in the else).
To answer your question, use a do-while loop. Here's an example:
String zip;
do {
zip = blahblah
} while (zip.length != 9)
My assumption is that in the line :
int ziplength = (zip.length());
You are simply not getting the length of zipNew, but instead the length of zip, which doesn't appear to have been declared anywhere.
I would suggest changing this line to :
int ziplength = (zipNew.length());
I have very little experience with java, but just from broad understanding of other languages, I believe you've just made a simple mistake.
Hope this works!
Cheers,
Mike

Java Input Empty show alert [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm doing a small project in Java whilst I'm learning the language.
Basically what I'd like is for a user to input a string via JOptionPane.showInputDialog();, if that string is empty, I want it to make them re-enter a valid string and then the program will continue.
What I did consider doing is using goto but I read up on it and it said it's not good practice.
Any help would be greatly appreciated.
Thanks :)
I would do it in an infinite loop asking for input (using OptionPane.showInputDialog()).
Here you have some pseudo code:
message = ""
while message.equals(""):
message = ask_for_input() // (.trim() if needed)
end
You could do it in a loop. So you take a while-loop where the condition is that the input is empty. Then the message will pop up until the user entered something.
Despite goto is a reserved keyword in Java, it's actually (implicitly) meant for not being used.
JOptionPane.showInputDialog(...) returns a String.
I suggest using an infinite while loop and comparing it to empty, through String.isEmpty().
For instance:
String input = null;
while (true) {
input = JOptionPane.showInputDialog("Your input (not empty):");
if (!input.trim().isEmpty()) {
// TODO something with the input variable
break;
}
}
This is what do ... while loops are for. You could also use the Apache commons StringUtils class.
String input;
do {
input = JOptionPane.showInputDialog("Please enter your string");
} while (StringUtils.isBlank(input));

Categories

Resources