I am having a problem with code that I've written, the problem occurs when I try to prompt for user input at the end of the first loop. What happens is the println fires but the prompt never does and it exits the loop.
Java Code:
import java.util.Scanner;
public class Lab3test
{
public static void main( String[] arguments )
{
//Variable declaration
public static void getInput(int [] student, int []test1,int []test2,int []test3, int [] test4, int [] test5) { int stuId = 0; int t1 = 0; int t2 = 0; int t3 = 0; int t4 = 0; int t5 = 0; int h = 0; char answer = '@';
I am still relatively new to java and am working on a lab program. I have already met the requirements and am trying to spice my code up with a loop that prompts the user to say if the program should run again. This is what I have so far:
package lab2; import java.util.Scanner; public class OrderedPairTest{ public static void main(String[] args){ boolean retry = true; while(retry == true){
[code]....
However, it is giving me an error after the user inputs "y" in for the answer to run again. What did I do wrong?
Error code is: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at lab2.OrderedPairTest.main(OrderedPairTest.java:11)
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
Spacing code: import java.util.*; public class SumoftheIntegers { static Scanner console=new Scanner(System.in); public static void main(String []args) { int num1, test, rem; int counter = 0;
[Code]...
Now the sum of the integers code:
import java.util.Scanner; public class sum { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Enter amount System.out.print("Enter an integer: "); int integer = input.nextInt();
Write a program that prompts the user for an input number and checks to see if that input number is greater than zero. If the input number is greater than 0, the program does a multiplication of all the numbers up to the input number starting with the input number. For example if the user inputs the number 9, then the program displays the following sum:
9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 362880
That's the question I'm getting and so far all I've got is
import java.util.Scanner; public class Lab4Q3 { public static void main (String[] args) { int keyboard;
Basically I need to make a program prompts the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. Then I need to convert from base e to base 2 then I need to print the tick marks of a ruler based of the value of the length.
I got the whole converting thing working and the check for a power of 2, that wasn't an issue because it didn't require any recursion. I just don't know how to print out the proper tick mark values.
The way it is supposed to work is that it needs to look like this. Say the user enters 8;
012131210 012345678
Or if the user enters 16;
01213121412131210 01234567890123456
The bottom row is pretty much just the index value, that I print fine.
The top row though is supposed to be the length of the ticks on a ruler, with the middle most value being the value of the conversion from base e to base 2 from above. I can get that printed and what I get just looks like this.
For 8;
000030000 012345678
For 16;
00000000400000000 01234567890123456
As you can see I can get the middle value and the index values below but I don't know how to use recursion to get the right numbers.
Here's my current code.
import java.util.*; public class TickMarks { public static void main (String args[]){ Scanner input = new Scanner(System.in); boolean looping = true; while(looping == true){ System.out.print("Please enter a length: ");
[Code]...
Now the methods isPowerOfTwo() and printLength() both work fine. However, I'm not sure how to get the left and right side working.
My thoughts were to split the length in half and get a left and right side. I gave both of them arrays so I can keep track of the values. Now as you've seen above I get all zeros and I know it's because that's the default value in an array, I just don't know how to get the proper values.
I am wanting this program to prompt the user to enter a maximum value and a minimum value and the program should in theory generate a random number for the output. Here is my logic:
int maxRange1; int minRange1; static int range; static Random gen;
Then in my mail argument:
gen = new Random(); Scanner input = new Scanner(System.in); System.out.print("Enter the maximum number: "); int maxRange1 = input.nextInt();
[Code] ....
I am able to return both the user prompts and get the inputted answer to appear but I am not getting a response for the prompt "The random number is: ".
I am suppose to design a problem the prompts the user to enter a String. Based on the input from the user, have the program prompt with a full description of the playing card entered. Below is a table of the notation and meaning:
Notation Meaning A Ace 2...10 Card Values J Jack Q Queen K King D Diamonds H Hearts S Spades C Clubs
Use the .charAt(...) to extract each character from the user's input and build a switch statement to determine the full description.
import java.util.Scanner; public class PlayingCardIdentifier { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a card notation: "); String s = input.nextLine();
Write a method named diceSum that prompts the user for a desired sum, then repeatedly rolls two six-sided dice until their sum is the desired sum. Here is the expected dialogue with the user:
Desired dice sum: 9 4 and 3 = 7 3 and 5 = 8 5 and 6 = 11 5 and 6 = 11 1 and 5 = 6 6 and 3 = 9
This is my code:
import java.util.Scanner; import java.util.Random; public class mod { public static void main(String[] args) { diceSum();
[Code] .....
it works for numbers 2 - 4, but any number from 5 - 12 just prints one output, which is not the correct desired sum.
The exercise sounds like this : Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid , as shown in the following sample run (my code displays correctly the first 9 lines):
The problem is when i input a number greater then 9 as it requires 2 spaces . I m pritty sure i solved it incorrectly or at lost not optimal as i m using a string that decreases on each line to create the pyramid effect.
import java.util.*; public class C5_17 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter the number of lines: ");
My full instructions are to Design a Java interface called Priority that includes two methods and two constants. The methods are setPriority and getPriority. The constants are MIN_PRIORITY with value 1 and MAX_PRIORITY with value 6. The interface should define a way to establish numeric priority among a set of objects. Design and implement a class called Task that represents a task (such as on a to-do list) that implements the Priority interface. Assume that Task has two attributes (instance data): name and priority. Define the Task constructor to accept and initialize the name of the task. Each newly created Task should have priority equal to MIN_PRIORITY (the constructor should initialize priority to MIN_PRIORITY). Priorities can be changed using setPriority method. Create a driver class TaskDriver that should do the following: prompt the user to enter three tasks and their priorities, and then print the tasks in the order of decreasing priorities. If the user enters an invalid priority that is less than MIN_PRIORITY or greater than MAX_PRIORITY, then the user should be asked to enter priority again until a valid priority value is entered.
So far I have:
public interface Priority { public void setPriority(int level); public int getPriority();
So I have this program where i have to prompt user to enter a the value of their savings for n number of weeks (quarters dimes nickels pennies) and the program outputs the total of savings an average of saving per week and an estimation of saving per year.
I managed to do the first step but the problem is that when they enter the value of their savings i don't know how to save the value of each(quarters dimes nickels and pennies) to make the calculations? Here is what i have so far
import java.util.Scanner; public class yearlySavings { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int numberOfWeeks = 0; // initialize value of numberOfWeeks
So it is the question: This program implements a simple "decoder ring". It will prompt the user for a message to decode and decode the message based on the following substitution rules:
ABCDEFGHIJKLMNOPQRSTUVWXYZ will map to ZEBRASCDFGHIJKLMNOPQTUVWXY
abcdefghijklmnopqrstuvwxyz will also map to ZEBRASCDFGHIJKLMNOPQTUVWXY
any other characters (like space, punctuation, will map to themselves)
so if it sees an 'A' (or an 'a'), it will map to 'Z', etc... All i could think of is this but can't figure out the rest.
import java.util.Scanner; public class DecoderRing { public static void main(String[] args) { String1[] = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'; String2[] = 'Z','E','B','R','A','S','C','D','F','G','H','I','J','K','L','M','N','O','P','Q','T','U','V','W','X','Y'; } }
Not sure, what things are necessary such as switch statements or if and else or for loops and such...
I have to create a program that asks a user to enter an amount for rows and an amount for columns. After they input both amounts, they must fill in the matrix with values (values of double data type). After they enter the values, the program is suppose to search the matrix for the element with the highest value. Once it is found, the user will be prompted with the location of the highest value within the array. I got it to work as to where the user inputs the values and he/she will receive a location of an element. However what they receive is incorrect.
For example
"Enter number of rows: 3 Enter number of columns: 4 Enter values: 23.5 6 13 2 1 5 6 3 7 5 9 2"
Once they enter the last value, they will be prompted with
"The highest element is located in 34".
As you can tell, the highest element is located in 01. I think when the user is prompted with the values that they enter for the rows and columns. How can I modify my code as to where it shows the actual location of the highest value?
I did a problem from my book which is "Write a method that displays an n-by-n matrix. Each element is 0 or 1, which is generate randomly. Write a test program that prompts the user to enter n and displays the n-by-b matrix".
So if a user would enter 3, and output could be 0 1 1 0 1 0 1 0 1
So here's my question... I was able to get the program to work in the way my book describes (by just writing the code in the class with the main), but for practice, I want to learn how to do this OOP-style...
I'm not sure how to do this, though, since the method is a void method, so I can't seem to call it within a toString method.
import java.util.Scanner; public class MatrixTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an int"); int number = input.nextInt(); MatrixClass mc = new MatrixClass(number);
This program simply adds integers to an ArrayList and then prompt the user if they would like to delete specific numbers that they don't want.
The output that I get when I want to delete numbers like 2 and 4 from 1 2 3 4 5 is 1 2 3 4 5 instead of 1 3 5.
Java Code:
import java.util.ArrayList; import java.util.Scanner; public class AL { // A regular array like int arr[] has to have its size declare before run-time and it's not dynamic which mean it can not grow or expand on its own. static Scanner input = new Scanner(System.in); // You have to use reference types(Integer, Double, Float); not primitive types(int).
I've got a nasty nullpointer that I have tried to resolve to no avail as of yet. The program should prompt for a listings.txt file and take its info and write to a report file. Here's the stacktrace:
run:
Input file: listings Exception in thread "main" java.lang.NullPointerException at java.io.Writer.<init>(Writer.java:88) at java.io.PrintWriter.<init>(PrintWriter.java:113) at java.io.PrintWriter.<init>(PrintWriter.java:100) at kettask2b.PropertyListingsReport.main(PropertyListingsReport.java:34) Java Result: 1
Some adjustments that I have attempted are:
BufferedWriter pwfo = null; for (int i = 0; i < args.length; i++) { String string = args[i]; pwfo = null;
import java.util.Scanner; public class Project_5 { public static void main (String[] args) { Scanner input= new Scanner (System.in);
[code]....
So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. If the user types in "6" then presses enter and types in "3" , the output is:
There were 3 valid numbers entered. The sum of the valid numbers was --- and the average was ---. There were 0 invalid numbers entered.
It then continues on allowing the user to enter more values. Here is the example output for correct code"
Enter a positive value (EOF to quit): 4 Enter a positive value (EOF to quit): 7 Enter a positive value (EOF to quit): 8 Enter a positive value (EOF to quit): 2 Enter a positive value (EOF to quit): -1 The number "-1" is invalid.
Enter a positive value (EOF to quit): 8 Enter a positive value (EOF to quit): 0 Enter a positive value (EOF to quit): -4 The number "-4" is invalid.
Enter a positive value (EOF to quit): CTRL-D
There were 6 valid numbers entered. The sum of the valid numbers was 29 and the average was 4.83. There were 2 invalid numbers.
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
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()); } }
Here is my problem to solve : Find the smallest value Write an application that finds the smallest of several integers. Assume that the first value read specifies the number of values to input from the user.
I do not even know where to begin other than creating a scanner for the user input. But how do I take that user inputed number to create the loop?
I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:
public static void main(String[] args)throws IOException { // TODO code application logic here BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int num1=(int)(Math.random()*(9-0)); int num2=(int)(Math.random()*(9-0)); int num3=(int)(Math.random()*(9-0));
I'm making a program to demonstrate Exception Handling. (Note: the code was written inside towards out). The user is prompted to input two values (integers) and then the program will divide those values and output the result.
I then added the "try" and "catch keywords for exception handling-instead of the program shutting down if an error occurs, the words "you cant do that" will appear
I then added a while loop to allow the user another chance to input usable values. Of course, I made sure to make a termination to the loop as well.
Everything works as expected if the user inputs e.g 8 divided by 0. in that case exception occurse, the user is told "you cant do that", is then prompted to start over and so forth until he gives usable integers. then the program continues to the termination. However, if the user inputs a String , e.g 9 divided by xyz, then the program goes into an endless loop
My question is:
1) why does a String cause an endless loop ( I have a theory)
2)why does the loop occur as soon as I put in an incorrect value- is the while loop somehow in extant? somehow always waiting? (hard to picture it)
3) lastly, what would be the correct code to handle and exception caused by a String (as opposed to being caused by impossible to execute math)
Here is the program
package inschool; /* *GOAL: to allow for an exception to occur without causing the entire program to stop. *NOTE: the coding was written from the center outwards (e.g first "System.out.println", then "try", then "do") */ import java.util.*; public class L82_ExceptionHandling { public static void main(String[] args) { Scanner input = new Scanner(System.in);
Here is my code so far. I am trying to get the WHILE LOOP to work so the user inputs a number, the if statement prints the output and then it returns to ask for another number and goes again and again looping :
import java.util.Scanner; public class ifwhileloop { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { double nmbr;
I have this program I have to write(attached). I am having problems with what the structure will look like. The following what I have so far. The questions I have are in bold.
>get userInput of how many observations
>for(int i = 1; i <= userInput; i++) >for(int j = 1; j <= 1; j++) >use a switch(case) method to ask user to select an option(1,2,3) >example, user chooses option 1: >ask to input time >print displacement >ask user to stop application(Y/N) >IF "No" is selected --------->>
How do I continue with the loop if user decides to not quit? -- And do I need to put this in each 'case'?