I am trying to do a java program but I am having a problem with the output.
Here is the error I get when I input the information.
Enter social security number:12345678
Enter salary3000
Please input next security numbers or -1 to quit:12345666
Enter salary2122
Please input next security numbers or -1 to quit:900000000
Enter salary3000
Please input next security numbers or -1 to quit:-1
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ':'
at java.util.Formatter.checkText(Formatter.java:2547)
at java.util.Formatter.parse(Formatter.java:2533)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Salaries.output(Salaries.java:57)
at Salaries.main(Salaries.java:19)
And here is my code so far..
import java.util.Scanner;
public class Salaries {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
int[] ssNumbers = new int [10];
double[] salaries = new double [10];
double[] nSalaries = new double [10];
int c;
c = inputData (ssNumbers, salaries);
raise (salaries, c);
output (ssNumbers, salaries, nSalaries);
}
public static int inputData (int[]ssn, double[]sals){
int c = 0;
Scanner input = new Scanner (System.in);
int ssNum;
System.out.print("Enter social security number:");
ssNum = input.nextInt();
while (ssNum != -1) //using while loop.
{
ssn[c] = ssNum;
System.out.print("Enter salary");
sals[c] = input.nextDouble();
c++;
System.out.print("Please input next security numbers or -1 to quit:");
ssNum = input.nextInt();
}
return c;
}
public static void raise (double[] salaries, int c)
{
double[] salaryraise = new double [10];
for (int i = 0; i < c; i++ )
salaryraise[i] = salaries[i]*.02;
}
public static void output (int[] ssNumbers, double[] salaries, double[] nSalary )
{
System.out.printf("%-20s%-20s%-20s%:\n", "Social Security Number", "Salaries", "Salary After Raise");
for (int i = 0; i < salaries.length; i++)
System.out.printf("%d %.2f %.2f", ssNumbers[i], salaries[i], nSalary[i]);
return;
}
}
You have a "%:" in this line you'll wanna remove. (because it's not a valid specifier)
System.out.printf("%-20s%-20s%-20s%:\n", "Social Security Number", "Salaries", "Salary After Raise");
:)
Try changing the line:
System.out.printf("the salary after raise is %f\n:", salaryraise);
To:
System.out.println("the salary after raise is: " + salaryraise);
The % character at the end of format make the error.
Should delete it lis below:
System.out.printf("%-20s%-20s%-20s%:\n",
>>System.out.printf("%-20s%-20s%-20s:\n",
Related
This code is supposed to capture 5 user integers, print them out, then print them in reverse. It is capturing the first int only, and printing it 3 times, then printing the first integer again 5 more times without reversing. Test ends with "Process finished with exit code 0" which I think is says the program finished without errors -- which of course is not correct. I assume the issue is in how the user input array is stored. I have it assigning as userNum[i] with a limited array of 5, and int i =0 to begin array storage at userNum[0], so I'm not clear on why all the inputs are not captured up to userNum[4].
Thank you for any insight you can provide. I am very new to java and this is prework for my java class.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[i]);
++j;
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[i] + " ");
}
}
}
You need to work more about on for loops and study how to iterate values in for loop, the problem in your i,j variables.
Here I fix your code.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
//for 5 inputs you need loop
for(;i<NUM_VALS;i++){
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
}
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[j]);
//++j; //no need to increment as you already did in for loop
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[j] + " ");// userNum[0] = your last value which you reverse
}
}
}
Here is a solution using the collections framework:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final List<Integer> numbers = new ArrayList<>();
System.out.println("Enter any number of integers. (whitespace delimited. enter a non-integer to quit.): ");
while (scnr.hasNextBigInteger()) {
final int n = scnr.nextInt();
System.out.println("Parsed: " + n);
numbers.add(n);
}
System.out.println("Done reading user input.");
System.out.println("Your input: " + numbers);
Collections.reverse(numbers);
System.out.println("Your input reversed: " + numbers);
}
}
I have provided you with a solution. This is a clean way of doing it.
nextInt() reads the next integer that the user inputs. Notice that this will throw a InputMismatchExceptionif the user does not input a integer as value.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> input = new ArrayList<Integer>();
//Simple loop that will read 5 user inputs
//and add them to the input list
for(int i = 0; i < 5; i++){
input.add(scanner.nextInt());
}
//print the 5 values
for(Integer val : input){
System.out.println(val);
}
//reverse the 5 values
Collections.reverse(input);
//print the 5 values again, but they are now reversed
for(Integer val : input){
System.out.println(val);
}
}
Hello guys I am having a problem with an array and a .nextInt(); this is causing my output line at the 3rd prompt to shift up instead of under, and seriously cannot figure out what's wrong.
I have tried .hasNextInt(); but nothing, it actually gives me an error, so here is the code:
import java.util.Random;
import java.util.Scanner;
public class birthday {
public static void main(String[] args) {
System.out.println("Welcome to the birthday problem Simulator\n");
String userAnswer="";
Scanner stdIn = new Scanner(System.in);
do {
int [] userInput = promptAndRead(stdIn); //my problem
double probability = compute(userInput[0], userInput[1]);
// Print results
System.out.println("For a group of " + userInput[1] + " people, the probability");
System.out.print("that two people have the same birthday is\n");
System.out.println(probability);
System.out.print("\nDo you want to run another set of simulations(y/n)? :");
//eat or skip empty line
stdIn.nextLine();
userAnswer = stdIn.nextLine();
} while (userAnswer.equals("y"));
System.out.println("Goodbye!");
stdIn.close();
}
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //my problem
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2];
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return promptAndRead(stdIn);
}
// Method for calculations
public static double compute(int numOfSimulation, int numOfPeople)
{
for (int i =0; i < numOfPeople; i++)
{
Random rnd = new Random(1);
//Generates a random number between 0 and 364 exclusive
int num = rnd.nextInt(364);
System.out.println(num);
System.out.println(num / 365 * numOfPeople * numOfSimulation);
}
return numOfPeople;
}
}
Found it!!!!!!!!!!!
do this:
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //CHANGE THIS TO 1?
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2]; //CHANGE THIS TO 1?
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return(userInput);
}
To return the array
Let me know!
I actually don't think you can do it there with that userInput, I am saying this because the methodology of doing this program is quite arcane.
You are then calling 2 arrays at prompting, I wonder if you might change that to one what will change such as:
// User input prompt where you make the simulation. For people and return them as an array
public static int[] promptAndRead(Scanner stdIn)
{
System.out.println("Please enter the number of simulations you want to do: ");
int[] userInput =new int [2]; //CHANGE THIS TO 1?
userInput[0]= stdIn.nextInt(); //my problem
System.out.println("Please enter the size of the group you want : ");
int[] userInput1 = new int [2]; //CHANGE THIS TO 1?
userInput[1] = stdIn.nextInt();
int a = userInput[1];
while (a<2 || a>365)
{
System.out.println("please type the number that is between 2~365");
}
System.out.println();
return promptAndRead(stdIn);
}
As also the return promptAndRead(stdIn); might be part of the problem
Don't know though just trowing suggestions at Markov ;)
I want the user to only be able to type in no more than three number into the console. Is there any way to limit or stop the console from accepting keyboard input?
Thanks
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Type three integers ");
int firstInt = userInput.nextInt();
int secondInt = userInput.nextInt();
int thirdInt = userInput.nextInt();
System.out.println(firstInt + secondInt + thirdInt);
}
}
int count = 3;
Scanner z = new Scanner(System.in);
List<Integer> l = new ArrayList<>();
while(count>0)
{
int n = z.nextInt();
l.add(n);
count--;
}
Runtime.getRuntime().exit(0);
Runtime.getRuntime().exit(0) will allow code to skip next lines in code.
give it a try
Hope this is the solution you are looking for
When I tried to run this code noOfSub() methods executed properly;
but GC() method faces the following problem:
Enter the number of subjects:
2
Enter Your Subject 1 Grade:
s
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at GPA.GC(GPA.java:21)
at GPA.main(GPA.java:35)
Java Result: 1
Here is my code:
import java.util.Scanner;
public class GPA {
public int noOfSubjects;
public int i=1;
Scanner gradeInput = new Scanner(System.in);
String[] grade = new String[noOfSubjects];
int[] credit = new int[noOfSubjects];
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
}
public void GC() {
while(i<=noOfSubjects)
{
System.out.println("Enter Your Subject "+i+" Grade:" );
grade[i] = gradeInput.nextLine();
System.out.println("Enter the Subject "+i+" Credit:");
credit[i] = gradeInput.nextInt();
i++;
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
When you do:
public int noOfSubjects;
noOfSubjects is set to 0 which is its default value
So when you have the following code:
String[] grade = new String[noOfSubjects];
it essentially means,
String[] grade = new String[0]; //create a new String array with size 0
which creates an empty array for you.
So when you do,
grade[i] = gradeInput.nextLine(); //where i is 1
you get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at GPA.GC(GPA.java:21)
at GPA.main(GPA.java:35
because there is no index 1 in String[] grade.
Problem in your array initialization. You can initialize your array after take the input from user.
For example :
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
}
And change your while condition. Instead of this you use
while(i < noOfSubjects)
and set i = 0
If you want to get the size for the array from the user, create the array after getting it from stdin. Otherwise it will create a array with the size of 0 which is the default value for int in java.
Separate your declaration and initalization
String[] grade = null;
int[] credit = null;
...
noOfSubjects = scan.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
Why don't you use ArrayList because the size of array isn't know for you
public class GPA {
public int noOfSubjects;
public int i=0;
Scanner gradeInput = new Scanner(System.in);
List<String> grade = new ArrayList<>();
List<Integer> credit = new ArrayList<>();
public void noOfSub(){
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
}
public void GC(){
while(i<noOfSubjects)
{
System.out.println("Enter Your Subject "+(i+1)+" Grade:" );
grade.add(gradeInput.nextLine());
System.out.println("Enter the Subject "+(i+1)+" Credit:");
credit.add(gradeInput.nextInt());
gradeInput.nextLine();
i++;
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
Note : i added gradeInput.nextLine() after i++ because the Scanner.nextInt() method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner.nextLine() so i fire a blank gradeInput.nextLine() call after gradeInput.nextInt() to consume rest of that line including newline
Since the noOfSubjects has run time value so the code should be:
import java.util.Scanner;
public class GPA {
public int noOfSubjects;
public int i = 0;
Scanner gradeInput = new Scanner(System.in);
String[] grade;
int[] credit;
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
}
public void GC() {
while (i < noOfSubjects) {
System.out.println("Enter Your Subject " + (i + 1) + " Grade:");
grade[i] = gradeInput.next();
System.out.println("Enter the Subject " + (i + 1) + " Credit:");
credit[i] = gradeInput.nextInt();
i++;
}
for (int j = 0; j < grade.length; j++) {
System.out.println(grade[j] + " " + credit[j]);
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
This is assessed work so please don't give a straight answer.
My program is supposed to calculate the users grade: "pass" , "fail" or "pass with compensation". However, it doesn't return the answer. I'm unable to figure out why - can anyone help?
public class MarkCalculator {
static int[] marks = new int[12];
//public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int weighting;
int coursework;
int exammark;
System.out.println("Please enter course work weighting");
marks[0]= kb.nextInt();
System.out.println("Please enter course work mark");
marks[1]= kb.nextInt();
System.out.println("Please enter exam mark");
marks[2]= kb.nextInt();
MarkCalculator mc = new MarkCalculator();
mc.computeMarks(marks);
}
public String[] computeMarks(int[] marks) {
final int[] WEIGHTING = {55,66,55,44,33,44};
String[] results = new String[WEIGHTING.length];
for (int i = 0; i < marks.length / 2; i++) {
int exam = marks[i];
int cw = marks[i];
int weight = WEIGHTING[i];
int formula = ((cw + weight) + (exam * (100 - weight)) / 100);
if (formula >= 40){
results[i]="PASS";
} else if ((formula < 39) && (formula > 35)){
results[i]="COMPENSATION PASS";
}else{
results[i]="FAIL";
}
}
return results;
}
}
final int[] WEIGHTING = {};
String[] results = new String[WEIGHTING.length];
Here's a problem. WEIGHTING has no initial size.
Also: you don't initialize marks with anything. Try this:
System.out.println("Please enter course work weighting");
marks[0]= kb.nextInt();
System.out.println("Please enter course work mark");
marks[1]= kb.nextInt();
System.out.println("Please enter exam mark");
marks[2]= kb.nextInt();
MarkCalculator mc = new MarkCalculator();
mc.computeMarks(marks);
The problem is WEIGHTING is empty
final int[] WEIGHTING = {}; // empty
String[] results = new String[WEIGHTING.length];
for (int i = 0; i < marks.length / 2; i++) {
int exam = marks[i];
int cw = marks[i];
int weight = WEIGHTING[i]; // You cant access elements from an empty array
Also
MarkCalculator mc = new MarkCalculator();
mc.computeMarks(marks);
here you are passing marks which is empty.
EDIT
Reason why your program wasnt working is because you are not catching the result from computeMarks. You should store it in an array inside main like
String[] result = mc.computeMarks(marks);
for(int k=0;k<result.length;k++)
{
System.out.println(result[k]);
}