Question

Please help, Array does not print properly. I need to print out the 8 class numbers...

Please help, Array does not print properly. I need to print out the 8 class numbers entered by the user !

Java only using J option pane, you cannot use ulti or anything else only J option pane

Program compiles but does not print the array correctly!

import javax.swing.JOptionPane;

public class programtrial {

   public static void main(String[] args) {
   int newclass = 0;
   int countclass = 0;
   final int class_Max = 8;
   int[] classarray = new int[class_Max];
  
  
   do {
  
for(int i = 0; i < classarray.length; i++){
   do {
   newclass = Integer.parseInt(JOptionPane.showInputDialog("Enter the class number "));
   try {
  
   }
   catch (NumberFormatException e) {
newclass =-1;
}

if( newclass <0) {
JOptionPane.showMessageDialog(null, "Invalid try again");
}

} while(newclass <0);

classarray[countclass] = newclass;
   ++countclass;
   }
  
}while(countclass != class_Max);


   JOptionPane.showMessageDialog(null, classarray);

}}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import javax.swing.JOptionPane;

public class programtrial {

    public static void main(String[] args) {
        int newclass = 0;
        int countclass = 0;
        final int class_Max = 8;
        int[] classarray = new int[class_Max];
        do {
            for (int i = 0; i < classarray.length; i++) {
                do {
                    try {
                        newclass = Integer.parseInt(JOptionPane.showInputDialog("Enter the class number "));
                    } catch (NumberFormatException e) {
                        newclass = -1;
                    }
                    if (newclass < 0) {
                        JOptionPane.showMessageDialog(null, "Invalid try again");
                    }
                } while (newclass < 0);
                classarray[countclass] = newclass;
                ++countclass;
            }
        } while (countclass != class_Max);
        String result = "";
        for (int i = 0; i < classarray.length; i++) {
            result += classarray[i] + " ";
        }
        JOptionPane.showMessageDialog(null, result);
    }
}
Add a comment
Know the answer?
Add Answer to:
Please help, Array does not print properly. I need to print out the 8 class numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[]...

    I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[] = new double[ch.length]; int p = 0; for (int i = 0; i < ch.length; i += 3) { p = i + 1; for(int j = 0;j } } error message:items.java:16: error: cannot find symbol for(int j = 0;j ^ symbol: variable length location: class Object items.java:17: error: array required, but Object found salePrice[j] = (double full program import java.io.File; import java.util.Scanner; import...

  • This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class...

    This is the source code: Please incorporate the Exception Handling import javax.swing.JOptionPane; import java.awt.*; public class GuessingGame { public static void main(String[] args) { int computerNumber = (int) (Math.random() * 10 + 1); System.out.println("The correct guess would be " + computerNumber); int userAnswer = 0; int count = 0; while (computerNumber != userAnswer) { count++; String response = JOptionPane.showInputDialog(null, "Enter a guess between 1 and 10"); userAnswer = Integer.parseInt(response); String result = null; if (userAnswer == computerNumber) { result =...

  • Hi, for my Java class I have built a number guessing game. I need to separate...

    Hi, for my Java class I have built a number guessing game. I need to separate my code into two classes and incorporate a try-catch statement. Any help would be appreciated! Here is my code. import javax.swing.JOptionPane; import javax.swing.UIManager; import java.awt.Color; import java.awt.color.*; import java.util.Random; public class game { public static void main (String [] args) { UIManager.put("OptionPane.backround", Color.white); UIManager.put("Pandelbackround", Color.white); UIManager.put("Button.background", Color.white); Random nextRandom = new Random(); int randomNum = nextRandom.nextInt(1000); boolean playerCorrect = false; String keyboardInput; int playerGuess...

  • i need the same code but using inheritance and abstract class at least build a super...

    i need the same code but using inheritance and abstract class at least build a super abstract class , a sub class and build a main to run the code with javafx JOptionpane please if you can not answer the write code don't answer (Thank you) import javax.swing.JOptionPane; public class CollegeAdmission{ public static void main(String args[]) { String testScoreString; String classRankString; int testScore; int classRank; testScoreString = JOptionPane.showInputDialog("Enter test score: "); testScore = Integer.parseInt(testScoreString); classRankString = JOptionPane.showInputDialog("Enter class rank: ");...

  • I tried to add exception handling to my program so that when the user puts in...

    I tried to add exception handling to my program so that when the user puts in an amount of change that is not the integer data type, the code will catch it and tell them to try again, but my try/catch isnt working. I'm not sure how to fix this problem. JAVA code pls package Chapter9; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIlab extends JFrame implements ActionListener {    private static final int WIDTH = 300;    private...

  • I need to return a dynamic array from a class member function and print out the...

    I need to return a dynamic array from a class member function and print out the results in main. using pointers and dynamic allocation is required. class example{ int *test public: example(int a){ test = new int[a]; for(int i=0;i<a;i++) test[i]=i; } int *send(){ return test;} } int main(){ example a(3); int *practice = a.send(); int psize = sizeof(a)/sizeof(a[0]); for(int i=0;i<psize;i++) cout <<practice[i] << endl; return 0; }

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • How can i print the array in VehicleTest.java with out the hashcode behind the array element?...

    How can i print the array in VehicleTest.java with out the hashcode behind the array element? Vehicle.java public abstract class Vehicle { // class that can describe many vehicle types abstract public double GetCarbonFootPrint(); } Car.java public class Car extends Vehicle{ private double gallon; public Car() { this.gallon = 0;// set gallon to 0 } public Car(double gallon) { this.gallon = gallon; } @Override public double GetCarbonFootPrint() { return (this.gallon * 20);// carbon footprint is #gallons*20 } } Bicycle.java public...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT