Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.
public class problem3 { public static void main(String[]args){ int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5}; for (int i = 0; i<numbers.length;){ if(i>0){ System.out.println(numbers); } else System.out.println(numbers); } } }
Ok so I have my program working for the most part. In this program I am supposed to take input from the user until they enter a negative number and that works, the problem is that it doesn't work if I enter a negative number on the first prompt. For example if I enter a 100 it would prompt me again and if I enter a negative number it would quit and give me the average, but if I enter a negative number the first time I am prompted the program just gives me an error. How do I fix that?
import java.util.List; import java.util.ArrayList; import java.util.Scanner; public class Application { public static void main(String [] args){ Scanner input = new Scanner(System.in); List<Integer> grades;
I have to make a program that prompts the user to enter 10 numbers and at the end it prints out the distinct numbers and then the other numbers that weren't repeated...
I have the part where it prints out the distinct numbers but I stuck on how to make it print out the other numbers that didn't repeat...
import javax.swing.JOptionPane; public class DistinctNumbers { public static void main(String[] args) { String getInput; int input; int[] numbers = new int[10];
I am very new to Java. I have been working for a couple months on a program for school. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative. I have included my current code, the program works perfectly, but what to do about the negative numbers.
Java Code:
package gradplanner; import java.util.Scanner; public class GradPlanner { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numofclasses = 0; int totalCUs = 0;
I am very new to Java. I have been working on a program. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative.I have included my current code, the program works perfectly, but what to do about the negative numbers.
package gradplanner; import java.util.Scanner; public class GradPlanner { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numofclasses = 0;
My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.
The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.
My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.
Also, my verify method always returns false and I can't pinpoint why.
Here's my current code. Some things are commented out for debugging purposes.
import java.util.Scanner; /** * Simple calculator that reads infix expressions and evaluates them. */ public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in);
I'm having some issues getting this code to reject negative numbers. What I'm doing wrong.
import java.util.Random; import java.util.Scanner; public class ForLoop { public static void main (String [] args) { Random randomNumber = new Random();
I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing.
Enter numerator; then denominator. -5 10 -5/10 Enter numerator; then denominator. 3 9 1/3 Sum: -5/30 -0.16666666666666666 Product: -5/30 -0.16666666666666666 Devide: -15/30 -0.5 subtract: -45/90 -0.5 negative: 1/6 0.16666666666666666 Lessthan: 1/6 0.16666666666666666 greaterthan: 1/6 0.16666666666666666
FRACTION CLASS
import java.util.Scanner; public class Fraction { private int numerator; //numerator private int denominator; //denominator
You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.
The program must use a "Number" class which includes at least the following methods:
Number ( ); Number (double n); Number add (Number RHS); Number subtract (Number RHS); String toString ( );
This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method
import java.util.Scanner; public class Number{ private String whole; private String decimal; private String sign; public static void main (String[] args){ System.out.println("Enter two numbers");
You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.
The program must use a "Number" class which includes at least the following methods:
Number ( ); Number (double n); Number add (Number RHS); Number subtract (Number RHS); String toString ( );
The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .
Java Code:
import java.util.Scanner; public class Number{ private String whole; private String decimal; private String sign;
So I have re-written the code but it is still not running correctly. Any number i type in it throws an exception, also i need the program to add the totals that i type in and then once i type -1 into the prompt button list all the number i typed in and give me the average.
import java.awt.*; import java.awt.event.*; import javax.swing .*; import javax.swing.text.*; public class Averages extends JFrame { //construct components JLabel sortPrompt = new JLabel("Sort By:");
The code here I have works fine if I just want to ask the user to enter four digits: //java application that asks user to input binary numbers(1 or 0) and convert them to decimal numbers import java.util.Scanner; //program uses class scanner public class binarynumber{
//main method that executes the java application public static void main(String args[]){ //declares variables
int digit; int base=2; int degree; double decimal; int binary_zero=0; int binary_one=1; //create scanner for object input
[code]....
The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop.
I am trying to write a code that allows you to input 2 lists and it tells you if they are identical or not. However, when I enter my fist list I can only enter two values and then it asks for the next list. How would I fix this to allow me to enter more than two values?
Also, If the second list is different it has me enter more values that list one.
public static void main(String[] args) { Scanner input = new Scanner(System.in); // Enter values for list1 System.out.print("Enter list1: "); int size1 = input.nextInt(); int[] list1 = new int[size1]; for (int i = 0; i < list1.length; i++) list1[i] = input.nextInt(); // Enter values for list2 System.out.print("Enter list 2: ");
This application is supposed to allow a user to enter the names an phone numbers of up to 20 friends, until the user enters "zzz" or 20 names. Then the console is supposed to display the names and have the user enter a name to get the phone number. Here's my code:
import java.util.Scanner; public class PhoneBook{ public static void main(String[] args){ String name; int phoneNumber; final int nameAmount=20;
[Code] ....
I can't seem to get it correct, every time I correct a compiler error, another set of them appear.
How to improve my code. I finally was able to create a program that gives you the corresponding letter grade, when you enter in a numeric grade without using an array. The only issue left is that I have to be able to enter 5 grades at a time, and it give me the letter grade for all 5. I have the programming working, but only am able to enter 1 at a time. I am not sure what kind of loop or if I am supposed to use a loop.
public static void main(String[] args){ { int grade = 0; Scanner input = new Scanner(System.in); System.out.println("Enter : "); grade = input.nextInt(); if (grade >= 90)
I am trying to code using error handling and I am a bit confused on how to go about doing it correctly. My code converts a string that has all numbers into an integer and the error handling should recognize that if it isn't a proper number and ask the user to try again or enter 'q' to quit.Do I place a throw new exception in the try block and put conditionals like if charAt(i) is some letter or a symbol then throw new exception?
Java Code:
import java.util.Scanner; public class practice { public static void main(String[] args) { Integer finalValue = null; boolean validValue = false; Scanner scan = new Scanner(System.in); int result = 0;
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(" Enter the maximum number "); int maxNumber = scanner.nextInt(); int[]numberList = createList (maxNumber);
[code]....
I have a problem with the output. It only get as far as marking the multiples of 2. It does not mark the multiples of 3 and so on.Example output: If the range is from 2 to 15 the output is :2, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0, 15 note: 0 means its marked.As you can see it only marks multiples of 2 but it does not mark multiples of 3 above. I think the problem lies in the for loop inside my main method, because the index does not increment.
I am doing a project to generate three names of fruits. When I take out my switch code the numbers generate randomly, but when I added the switch code back I kept getting three instances of "Bars" as if the random numbers all became 5.
public static void main(String[] args) { System.out.println("Enter the amount of money you would like to gamble."); Scanner keyboard = new Scanner(System.in); int num1; int num2; int num3; int money; money = keyboard.nextInt();
I am having problems with writing a simple program to see if a number is divisible by 6.
public void run() { println("This program will display all numbers divisible by"); println(" 6 between 1 and 100."); int x =1; boolean divide =(x%6==0);
[Code] ....
It is telling that every number is not divisable by 6?
Write a program that will read two numbers and an integer code from the keyboard. The value of the integer code should 1, 2, 3, 4. If the value of the code is 1, compute the sum of the two numbers. If the code is 2, compute the difference (first number minus second). If the code is 3, compute the product of the two numbers. If the code is 4, and the second number is zero, compute the quotient (first divided by second). If the code is not equal to 1,2,3,4, display an error message. The program is then to display two numbers, the integer code and the computed result to the screen
here is the code that I have so far:
public static void main(String[] args) { Scanner read = new Scanner (System.in); int num1, num2, code, sum; System.out.println("Please enter a number"); num1 = read.nextInt();
when i input a positive integer it works but when i input a negative number it doesn't work
my pseudo code:
READ input WHILE( NOT CORRECT INPUT) READ INPUT AGAIN; ENDWHILE DECLARE array arr[input] FOR(i=0 to input-1) arr[i]= Random number from 0 to 100; ENDFOR DISPLAY ARRAY
error message when i input -5 : Exception in thread "main" java.lang.NegativeArraySizeException atPosNeg.main<PosNeg.java:36>
import java.util.*; class PosNeg{ public static void main(String args[]) { Random generator = new Random(); Scanner scan = new Scanner(System.in);