I'm trying to do a really simple code, but whenever I try to make it print the first statement to get an input information from the user, nothing shows up on the output screen.
Here's the code:
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];
for(int i = 0; i < grade[i]; grade[i]++){
System.out.println("Input the student's degree");
grade[i] = sc.nextInt();
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
I can't really see the problem here. If anyone is wondering, I am using Eclipse Neon 0.2 IDE. Thanks for reading
grade[] is empty, so the for-loop never runs. An int-array is initialized with 0's.
Grade array is empty. It has 0 in all indices. grade[0]=0, grade[1]=0 and so on...
Therefore the loop never runs and nothing happens.
This will help you understand:
int grade[] = new int[3];
Add this line under the above code.
System.out.print(grade[0]);
The Value of grade[i] is zero as has not been set. Thus you are not entering the loop.
for(int i = 0; i < grade[i]; grade[i]++){
// ^^^^^^^^ here
Try this:
for(int i = 0; i < 3; i++){
maybe like this:
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];
for(int i = 0; i < grade.length; i++){
System.out.println("Input the student's degree");
grade[i] = sc.nextInt();
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
Try with this before for loop
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];
System.out.println("Input the student's degree");
grade[0] = sc.nextInt();
grade[1] = sc.nextInt();
grade[2] = sc.nextInt();
you must rewrite your code, this is a sample code similar to yours with some modifications.
import java.util.Scanner; // import scanner package
public class demo {
public static void main(String args[]){
// create a scanner
Scanner sc = new Scanner(System.in);
// double data type array
double grade[] = new double[3];
for(int i = 0; i < grade.length; i++){
System.out.println("Input the student's degree");
// input double data type value
grade[i] = sc.nextDouble();
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
}
here is your code with a little modifications.
import java.util.Scanner; // import scanner package
public class demo{
public static void main(String args[]){
// create a scanner
Scanner sc = new Scanner(System.in);
//create a double data type array
double grade[] = new double[3];
// create a for loop with modified condition
for(int i = 0; i <= grade[i]; grade[i]++){
// prompt input message
System.out.println("Input the student's degree");
// assign a double data type value
grade[i] = sc.nextDouble();
// test if grade is less than 10
if(grade[i] < 10 ){
grade[i] += 0.5;
}
System.out.println(grade[i]);
}
}
}
Related
So I tried looking up this error (Exception in thread "main" java.lang.ArithmeticException: / by zero)
and figured that my arraylist size in the code below is zero. I can't understand why it is zero or how to solve it. Does it have to do something with the capacity of the array? I can't seem to figure out what's wrong with my code.
Btw, this code is for computing the average of all the elements in an arraylist and letting the user know what the average is.
I'm still a beginner at Java so I apologize if this may seem silly. Any help is appreciated!
import java.util.*;
public class ClassStuff {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList <Integer> myArray = new ArrayList <Integer> ();
int userInput = 0;
String userConf = "";
while ((!userConf.equalsIgnoreCase("y"))) {
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
for (int i = 1; i <= myArray.size(); i++) {
userInput = scan.nextInt();
myArray.add(userInput);
}
scan.nextLine();
System.out.println("Are you done entering numbers? (y/n): ");
userConf = scan.nextLine();
}
int result = computeAvg(myArray);
System.out.println("Average is: " + result);
}
public static int computeAvg(List <Integer> myArray) {
int sum = 0;
int avg = 0;
for (int i = 0; i < myArray.size(); i++) {
sum = sum + myArray.get(i);
}
return avg = sum/myArray.size();
}
}
I'm assuming the lines:
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
gets the amount of elements you want to add to the arraylist, which we later add to the list through the for loop. In that case keep it in another variable called list_length, since userInput constantly changes in the for loop.
System.out.println("Please enter a number: ");
list_length = scan.nextInt();
Then change the for loop after this input to something like:
for(int i = 1; i <= list_length; i++) {
userInput = scan.nextInt();
myArray.add(userInput);
}
This is because you changed the end of the for loop to myArray.size(), but remember that it was already 0, so the for loop ends since 1 >= 0. What you probably wanted to do was to add list_length amount of numbers into the arraylist
I found your problem.
The problem was your for loop because somehow the arrayList didn't catch any of the elements during the loop.
I also made some adjustment for the method so it counts the average correct.
Here's an example
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList <Integer> myArray = new ArrayList<>();
int userInput = 0;
String userConf = "";
while ((!userConf.equalsIgnoreCase("y"))) {
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
myArray.add(userInput);
scan.nextLine();
System.out.println("Are you done entering numbers? (y/n): ");
userConf = scan.nextLine();
}
System.out.println(myArray);
double result = computeAvg(myArray);
System.out.println("Average is: " + result);
}
public static double computeAvg(List <Integer> myArray) {
double sum = 0;
double avg = 0;
for (int i = 0; i < myArray.size(); i++) {
sum = sum + myArray.get(i);
}
avg = sum / myArray.size();
return avg;
}
Output
myList = [4,5]
average = (4 + 5) / 2 (myArray.size())= 4.5
Hope it was useful!
I have to write a program that reads in a sequence of integers until 'stop' is entered, store the integers in an array and then display the average of the numbers entered. I'm getting an input mismatch exception when entering 'stop' so it doesn't really work, but I have no idea why. Help would be greatly appreciated.
import java.util.Scanner;
public class MeanUsingList {
public void mean() {
String s;
int n = 0;
int i = 1;
int[] array = { n };
do {
System.out.println("Enter an integer");
Scanner in = new Scanner(System.in);
n = in.nextInt();
s = in.nextLine();
} while (s != "stop");
{
System.out.println("Enter an integer");
Scanner in2 = new Scanner(System.in);
int x = in2.nextInt();
array[i] = x;
i++;
}
int av = 0;
for (int y = 0; y < array.length; y++) {
av += array[y];
}
System.out.println(av);
}
public static void main(String[] args) {
MeanUsingList obj = new MeanUsingList();
obj.mean();
}
}
First int[] array = { n }; just creates an array with 0 as the element in it.
the logic inside it will never execute while (s != "stop");
You want something like this
List list = new ArrayList();
//instead of array used arraylist because of dynamic size
do {
System.out.println("Enter an integer");
Scanner in = new Scanner(System.in);
n = in.nextInt(); //get the input
list.add(n); // add to the list
Scanner ina = new Scanner(System.in); // need new scanner object
s = ina.nextLine(); //ask if want to stop
} while (!s.equals("stop")); // if input matches stop exit the loop
System.out.println(list); // print the list
I recommend you to learn the basics
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]);
}
I know this is simple. How would I take input from my console and store the input into a Set that can later be used to be returned on a Method. This is what I have so far.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class InputConsole {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(6);
int[] numbers = new int[6];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.print("Please enter number ");
numbers[i] = input.nextInt();
{
}
}
}
}
I am using and Array just to test with. The Array is set to 6 so if I type 6 numbers in the console it will stop. I have instantiated the HashSet but I don't know how to go about storing the numbers from the console into it.
Use method Set::add()
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Please enter number ");
s.add(input.nextInt());
}
You don't need int[] array
EDIT:
Whole main()
public static void main(final String ... args)
{
final int inputs = 6;
final Set<Integer> s = new HashSet<Integer>(6);
final Scanner input = new Scanner(System.in);
for (int i = 0; i < inputs; i++)
{
System.out.print("Please enter number #" + (i + 1) + ":");
s.add(input.nextInt());
}
System.out.println("Well done!");
System.out.println(s);
}
import java.util.*;
class Hashsetdemo
{
public static void main(String args[])
{
HashSet h=new HashSet(6);
int [] no = new int[6];
Scanner s=new Scanner(System.in);
for (int i=0;i<no.length;i++)
{
System.out.println("please enter number");
h.add(s.nextInt());
}
System.out.println(h);
}
}
What I am trying to do is have multiple inputs that all have different variables. Each variable will be part of different equations. I am looking for a way to do this, and I think I have an idea. I just want to know if this would be legal, and if there is a better way to do this.
import java.util.*;
public class Example{
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Enter number.");
int a = dd.nextInt();
System.out.println("Enter number.");
int b = dd.nextInt();
System.out.println("Enter number.");
int c = dd.nextInt();
}
}
If every input asks the same question, you should use a for loop and an array of inputs:
Scanner dd = new Scanner(System.in);
int[] vars = new int[3];
for(int i = 0; i < vars.length; i++) {
System.out.println("Enter next var: ");
vars[i] = dd.nextInt();
}
Or as Chip suggested, you can parse the input from one line:
Scanner in = new Scanner(System.in);
int[] vars = new int[3];
System.out.println("Enter "+vars.length+" vars: ");
for(int i = 0; i < vars.length; i++)
vars[i] = in.nextInt();
You were on the right track, and what you did works. This is just a nicer and more flexible way of doing things.