New java student need help with code
Hi everyone this is my first post here and i need help with my java program. Im a freshman in college and joined in the middle of the year so im pretty far behind. My class also only meets once a week and my teacher is rather unavailable so can someone please help me with this program? The objective is to ask the user how many times they would like to see the figure X.
import javax.swing.*; //Gives the windows where questions are asked
public class MiniLab4 // class Name will begin with a Capital Letter.
{
public static void main( String[] args )
{
body();
closingMessage();
system.exit(0); <-- says right here ';' expected
} // end main
public static void closingMessage()
{
JOptionPane.showMessageDialog (null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
}
public static void openingMessage()
{
JOptionPane.showMessageDialog (null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);
}//opening message
public static void body()
{
openingMessage();
getInt();
}
public static int getInt(String[] args)
{
int x;
x = getInt ();
System.out.println ("You entered " + x + ".");
} // main
public static int getInt()
{
int val;
while (true) // a seemingly endless loop - uses break to get out
{ // loop until we get a valid int
String s = JOptionPane.showInputDialog ( "Enter a whole number:" );
if (s == null)
{
JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
System.exit (0);
} // cancel if
else
if (s.equals(""))
{
JOptionPane.showMessageDialog
(null, "You must make an entry in the InputBox");
} // entered nothing
try
{
val = Integer.parseInt(s);
break; // exit loop with valid int
// could have had a variable change here to get out of loop
}// try
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Enter a whole number");
}// catch
while (number <= times)
{
xFig();
System.out.println
number = number++1; <-- Not a valid declaration
}
} // while
return val; //return the number to the calling method
} //end getInt
public static void bottom()
{
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" *******");
System.out.println (" *****");
System.out.println (" ***");
}//top figure
public static void top()
{
System.out.println (" ***");
System.out.println (" *****");
System.out.println (" *******");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
}// bottom figure
public static void blank()
{
System.out.println ("");
} // blank
public static void xFig()
{
bottom();
top();
}
}
Whenever i try to run this program it never asks how many X's you would like to see
Re: New java student need help with code
It would help if you could tell us the current problem you're having with your code ;)
Re: New java student need help with code
oh sorry xD, well whenever i run the program it doesnt even ask for the amount of x's you would like to see.
Re: New java student need help with code
Theres a few things wrong with your code. I'll blacken the lines i modified so you understand:
import javax.swing.*; //Gives the windows where questions are asked
public class MiniLab4 // class Name will begin with a Capital Letter.
{
public static void main( String[] args )
{
body();
closingMessage();
System.exit(0); // <-- originally system.exit(0) (no mayus)
} // end main
public static void closingMessage()
{
JOptionPane.showMessageDialog (null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
}
public static void openingMessage()
{
JOptionPane.showMessageDialog (null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);
}//opening message
public static void body()
{
openingMessage();
getInt();
}
public static int getInt(String[] args)
{
int x;
x = getInt ();
System.out.println ("You entered " + x + ".");
} // main
public static int getInt()
{
int val;
while (true) // a seemingly endless loop - uses break to get out
{ // loop until we get a valid int
String s = JOptionPane.showInputDialog ( "Enter a whole number:" );
if (s == null)
{
JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
System.exit (0);
} // cancel if
else
if (s.equals(""))
{
JOptionPane.showMessageDialog
(null, "You must make an entry in the InputBox");
} // entered nothing
try
{
val = Integer.parseInt(s);
break; // exit loop with valid int
// could have had a variable change here to get out of loop
}// try
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Enter a whole number");
}// catch
while (number <= times)
{
xFig();
System.out.println("??"); // originally System.out.println no ";" and no parameter.
number = number+1; // originally number = number++1;
}
} // while
return val; //return the number to the calling method
} //end getInt
public static void bottom()
{
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" *******");
System.out.println (" *****");
System.out.println (" ***");
}//top figure
public static void top()
{
System.out.println (" ***");
System.out.println (" *****");
System.out.println (" *******");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
System.out.println (" **** ****");
}// bottom figure
public static void blank()
{
System.out.println ("");
} // blank
public static void xFig()
{
bottom();
top();
}
}
* Also, the "public static int getInt(String[] args)" method doesn't return anything, so you might wanna change that to a "void" method
* The variables "number" and "times" are not declared, do that.
* At the "number = number++1;" part, if you're trying to increase the variable "number" by one, you can try using "number++;" instead of "number = number + 1". It's the same function, but i find that simpler ^^
Keep me posted. :P
Re: New java student need help with code
I tried fixing the code, let me know if that's how it is supposed to work, i commented the lines i changed. The changes i made are the same i suggested before. Except for a loop that was in another loop (the one with "number <= times"... you'll see :P):
Code Java:
import javax.swing.*; //Gives the windows where questions are asked
public class MiniLab4 // class Name will begin with a Capital Letter.
{
static int number = 0; //variables declared
static int times = 0;
public static void main(String[] args) {
body();
closingMessage();
System.exit(0);
} // end main
public static void closingMessage() {
JOptionPane.showMessageDialog(null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
}
public static void openingMessage() {
JOptionPane.showMessageDialog(null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);
}//opening message
public static void body() {
openingMessage();
getInt();
}
public static void getInt(String[] args) {
int x;
x = getInt();
times = x; // times will be the number of times you want the figure to be printed. (Is reduntant as you can use "x" instead)
System.out.println("You entered " + x + ".");
} // main
public static int getInt() {
//Got rid of the variable "val". Using "times" instead.
while (true) // a seemingly endless loop - uses break to get out
{ // loop until we get a valid int
String s = JOptionPane.showInputDialog("Enter a whole number:");
if (s == null) {
JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
System.exit(0);
} // cancel if
else if (s.equals("")) {
JOptionPane.showMessageDialog(null, "You must make an entry in the InputBox");
} // entered nothing
try {
times = Integer.parseInt(s);
break; // exit loop with valid int
// could have had a variable change here to get out of loop
}// try
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Enter a whole number");
}// catch
} // while
while (number <= times) { // I took this loop out of the other loop above, the "break;" would've never let this loop occur
xFig();
number = number + 1; // Got rid of the unused System.out.println();
}
return times; //return the number to the calling method
} //end getInt
public static void bottom() {
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" *******");
System.out.println(" *****");
System.out.println(" ***");
}//top figure
public static void top() {
System.out.println(" ***");
System.out.println(" *****");
System.out.println(" *******");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
}// bottom figure
public static void blank() {
System.out.println("");
} // blank
public static void xFig() {
bottom();
top();
}
}
Re: New java student need help with code
well first I would like to say well come to java programming.
secondly can every one use the
Code Java:
reply with quotes to see me
third here is your code back with some line edits it does not work yet but if you see what i am doing
use your output statements
Code Java:
System.out.println("I got to this spot"); // semi colon =)
then you should be able to finish nicely.
Code Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package junkcode;
import javax.swing.JOptionPane;
/**
*
* @author William
*/
public class MiniLab4 {// class Name will begin with a Capital Letter.
public static void main(String[] args) {
body();
closingMessage();
/****** you don't need this at all ****/
//system.exit(0); //<-- says right here ';' expected
} // end main
public static void closingMessage() {
JOptionPane.showMessageDialog(null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
}
public static void openingMessage() {
JOptionPane.showMessageDialog(null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);
}//opening message
public static void body() {
openingMessage();
getInt();
}
/**
* what is the difference between getInt(String[] args) and getInt() ???
* @param args
* @return
*/
public static int getInt(String[] args) {
int x;
x = getInt();
System.out.println("You entered " + x + ".");
//missing return statement
return x;
} // main
public static int getInt() {
int val;
//very bad way to start a while loop it forced use do while loop
// do things then while its true continue it removed the break statement
while (true) // a seemingly endless loop - uses break to get out
{ // loop until we get a valid int
String s = JOptionPane.showInputDialog("Enter a whole number:");
if (s == null) {
JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
System.exit(0);
} // cancel if
else if (s.equals("")) {
JOptionPane.showMessageDialog(null, "You must make an entry in the InputBox");
} // entered nothing
try {
val = Integer.parseInt(s);
break; // exit loop with valid int
// could have had a variable change here to get out of loop
}// try
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Enter a whole number");
}// catch
// well first we need to figure what number and times are then we can decide wheather we can
// create a argument for it.
//I want number to be 0 and times 1 then its always true
int number = 0;
int times = 1;
while (number <= times) {
xFig();
System.out.println("Hey do I work??? ");//<--- finish this statement was --> System.out.println
number = number++ /* guess you don't like semi colons */ ;
// 1; <---- what is this??
}
} // while
return val; //return the number to the calling method
} //end getInt
public static void bottom() {
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" *******");
System.out.println(" *****");
System.out.println(" ***");
}//top figure
public static void top() {
System.out.println(" ***");
System.out.println(" *****");
System.out.println(" *******");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
System.out.println(" **** ****");
}// bottom figure
public static void blank() {
System.out.println("");
} // blank
public static void xFig() {
bottom();
top();
}
}
good luck if you get stuck throw a reply