I am trying to make sure that the input is a float but not a string or character. I tried using hasNextFloat in a do-while loop but when a character is entered it won't re-prompt the user for input. It kind of just stops.
public class LabAssignment1 {
public static void main(String[] args) {
float num1, num2, result;
int choice;
Scanner scan = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.###");
My project is a program that uses methods to display strings in a variety of different ways. I already have all of that working, my problem is checking for user inputs.For example on the menu portion of my program i ONLY want the user to input 1-5.here is the method i developed for checking user input IT DOES CONTAIN ERRORS this is where i am having troubles.
public static int checkInput(String prompt, int lowerBound, int upperBound) { int input = 0; boolean done = false; while ( !done ) { System.out.println(prompt); if (input.hasNextInt())
[code]....
here is the rest of my program which is working fine besides the checking input. i would like to implement this method for checking inputs into all places where the user is asked to input something.
import java.util.Scanner; public class DisplayingStrings { public static void main(String[] args) { Scanner input = new Scanner(System.in); int getNum = checkInput("Enter #(1-5)", 1, 5); System.out.println("--String Writer--"); System.out.print("Enter a string: "); String word = input.next(); displayMenu(word);
I'm trying to write a program that calculates the factorials of the numbers 1 through 10, based on user input... My problem is that I don't know how to address the possibility of the user entering something other than a number. When I test the following code by entering a letter, I get an Input Mismatch exception. I'd like to be able to inform the user that the entry is invalid, and ask for another response. Here is my program thus far:
Java Code:
import java.util.Scanner; public class Factorial { public static String entryString; public static char entryChar; public static Scanner input = new Scanner(System.in);
I am trying to write a program that will generate a QR Code from an input text and also display some information about the input/output bits. So far I have created the frame and what to do next. And I'm not sure if I am on the right track since my level of programming is not that great. By the way, I am using zxing libraries from GitHub. I know, there are plenty of generators online for the QR Code, but that is not what I am looking for. As you can see on the attached image, I am more interested in the efficiency of encoding 2D data. Also, I noticed that almost all the online projects regarding 2D codes are for Android. Which is not very useful.
So I was going to try to create a program that prompts input and creates a file (That didn't exist before) with that input as name.Then, the program prompts inputs after stating questions such as 1 + 1, then if the user inputs an answer, put "Question # = Correct "or" Wrong.Code SO Far:
Java Code:
import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; public class File_Read { public File_Read() {//File_Read is the Interactive object
[code]....
So that it puts the Correct or Wrong into the file.
class Client{ public static void main(String []args){ Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params Bike R2=new Bike();//without params System.out.println(R1.increaseSpeed());//calling methods System.out.println(R1.maxDistance()); System.out.println(R2.increaseSpeed()); System.out.println(R2.maxDistance()); } }
I have been coding in my class at school (Grade 11 Computer science) and i just downloaded the program on my computer at home, unfortunately i cannot access my computer notes at home and i also dont remember certian specifics of coding, so my question is how would i get user input to create a program. The comments are the parts i dont remember. (I am trying to slowly build my memory with this stuff)
Here is my code so far:
import java.util; [highlight=java] public class hello_world { public static void main(String[] args) { string name; //WHAT DO I PUT HERE????
I have it so it gives me the sum of any digit, i just cant figure out how to get only the odd digits to add up. for example if I were to type 123. The odd numbers = 4 but using this program i get 6
class SumOfDigits { public static void main(String args[]) { int sum = 0; System.out.println("Enter multi digit number:");
Im trying to get the user to input the rows one by one but my input stops after the first. the examples i looked at in my book had this way i used as well. i are my rows and j are my columns. What is the correct way to do it?
import java.util.Scanner; public class Exercise08_01 { public static void main(String[] args) {
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object Scanner keys = new Scanner(System.in); //Get chips
[Code]....
*****This is what I get when I run it
run:
How much money would you like to change? 50
You now have $50 in chips.
What game do you want to play? You did not pick dice.
I'm trying to get the input of several JComboBoxes and execute a statement depending on which option the user chose for example if destination combo Box = japan duration combo box = 10 days and hotel combox = hotel then print line "welcome" I have tried many options but not getting the desired outcome. at the moment I'm using an if statement with only 2 of 3 of the J combo boxes but it's not working here's my code:
String[] Dest = new String[] {"Select Destination","Crete", "Paris", "Croatia"}; String[] Accomodation = new String[] {"Select your Accomodation","Hotel", "Villa", "Bed in Breakfast","Youth Hostel"}; JComboBox<String> comboDest1 = new JComboBox<String>(Dest); JComboBox<String> comboAccom1 = new JComboBox<String>(Accomodation);
So I'm currently self learning java. Currently I'm trying to write my own code with an else-if ladder to spit out the data on different types of ships. For some reason, when I put in the input, it always puts in my final else statement instead of the other ones. I've ran this code manually inputting a char (in the code the variable is ch) to see if the else-if ladder runs properly, and it does. But for some reason the input isn't running the ladder right.
class Ships { public static void main (String arg[]) throws java.io.IOException { char ch; int length, beam, draft, min, max;
I need to write an input validation while using the do-while statement. I feel like most of it is good except that it gets stuck inside the brackets of the do statement. After I enter an input, it just keeps asking me over and over for an input. Then I have to make it s if you enter an input that is out of range, you have to keep entering an input until it is in range.
Java Code:
do { System.out.print("Please enter the amount of spaces the letters will shift... "); shift = uInput.nextInt(); } mh_sh_highlight_all('java'); Java Code: public class ShiftEncoderDecoderDriver { public static void main(String[] args)
I think the problem in this is that the variable max is initialised as 0. Afterwards it remains in the while loop only, so the output is always 0. I dont know how to bring the last max value out of loop and print it.
Here is my code, basically I'll tell you what my program is suppose to do, I want to be able to leave the text box's empty if I like I want to skip 1 or more or all if I like skipping just basically means 0 but getting this error, it's forcing me to type in them all.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unk nown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at StockControl.addToStock(StockControl.java:86)
how I can change my code so that somebody can actually input the specified file path, rather than having it fixed in the code. I previously used
Scanner in = new Scanner(System.in); System.out.println("What is the filename?"); String input = in.nextLine(); File file = new File(input);
But then the program would not display the frequencies.
import java.io.File; import java.util.*; /* This program will allow the user to enter in a text file name, when prompted the program will anaylser the text and display the frequencies */ public class AssP { public static void main(String[] args ) { Scanner scan; try { Scanner scanner = scan = new Scanner(new File("C:/Users/Mary/workspace/Assingment/src/test.txt"));
[code]....
This is my current code and I need to so the user can load their own files.
I have been given a piece of work to do for College that requires me to format user input. For example: This is an example of text that will have straight left and right margins after formatting
The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.
This.is.an.example.o f.text.that.will.hav e.straight.left.and. right.margins.after. formatting.......... public class Main { public static void main ( String args[])
Create a one dimensional array which holds 10 values. Ask the user to input an index value between 0 and 9. Print the value the user selected. Be sure to explain the output to the user. That is my assignment, and here is my code:
import java.util.Scanner; public class Array { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a[]= new int[9]; a[0] = 10; a[1] = 20;
[code]....
I don't know how to use the scanner to get someone's input properly.
At the end of my main method, I want it to print out the input given that should have been stored in my sandwich class fields, but it didn't.
import java.util.*; import java.lang.*; public class SandwichBuilder { public static void main(String[] args) { Scanner inputDevice = new Scanner(System.in);
I just can't find a way to check if user puts any input or not? The line is Employee Name and I need to validate that he puts something there.
import java.util.Scanner; public class JavaMartInventorySystem { public static void main(String[] args) { String empName; Scanner keyboard = new Scanner(System.in);