java programming help

Holy switch statement Batman O.O. Do you HAVE to use that? You really, really, really should use if statements there

if (score >= 90) {

} else if (score >= 80) {

}

etc. It’s cleaner, it’s more self explanatory, it’s harder to make a mistake, and it’s likely more efficient. Imagine if you missed one number in that whole mess of cases and you were desperately trying to debug why the input “62” isn’t working right, it’d be hell to find in a larger program.

Also, do… while loops are evil. Now, in programming “evil” doesn’t mean “never use it” (except for goto, unless you’re deliberately obscuring code never use goto), it means “there better be a damn good reason.” In this case, there isn’t, your problem could easily be solved by pre-declaring exit:



boolean exit = false;
while (!exit) {
[... stuff]
	else if (response.equalsIgnoreCase("q"){
		exit = true;
	} else {
		System.out.println("Enter more data?" /* blah blah blah */);
		exit = response.equalsIgnoreCase("e"); 
	}

}

System.out.println("goodbye");


The principal behind a do while loop is “do this at least once.” You can ALWAYS restructure a do…while loop as a while loop, however, there are (rare) cases where it’s cleaner because otherwise you have to type

/* do stuff /
while (something){
/
do the same stuff */
}

which is cleaned up by:

do {
/* do stuff */
} while (something);

But the cases where this happens is rare, usually what you want to do for such simple conditional while loops is initialize what’s known as a “flag” (like exit) to whatever value will let it “fall through” the while conditional the first time. In most cases, a simple flag is all that’s needed.
Edit: The one case where I’d really use a do…while loop is to clean up a statement where you want a function to meet some parameter only once.

do {
println(counter);
while (counter.increment < 5)

This will print out a counter > 5 exactly once. This specific example isn’t too helpful, but it illustrates a case where you MAY want to use a do…while loop. Honestly, I almost never see them in practice.

Sorry, my code won’t work, you have to change the else block to “if (!exit)”.

You could probably make the code more efficient if you were willing to use break; and do…while in conjunction, but that’s unnecessary and probably (barely) too advanced for your level.

sigh…

well a new problem has arisen


import java.util.Scanner;
public class Times  
{  
  
	public static void main(String[] args)  
	{  
		Scanner keyboard = new Scanner(System.in);
		int i,j;
		
		System.out.println("Enter the limit of the table.");
		i = keyboard.nextInt();
		
		System.out.print("   ");  
		for (j = 0; j <= i; j++)  
		System.out.print("  " + j);  
		System.out.print('
');   
  
		for (i = 0; i <= i; i++)  
   
		{  
			System.out.print("  " + i);  
			for (j = 0; j <= i; j++)  
			{  
				String display = "";  
  
				if (i * j < 10)  
				{  
					display += "  " + i * j;  
					System.out.print(display);  
				}  
				else   
				{  
					display += " " + i * j;  
					System.out.print(display);  
				}  
			}  
			System.out.print('
');  
		}  
	}    
} 

anyone want to guess what happened?

Well among other things:

  1. you didn’t check your input. You don’t know what “i” is going to be, other than an int. It may be really small, zero, or huge. I suggest you check what it is and provide feedback if it is outside the bounds of what you want it to be. Don’t trust humans.

  2. Your first “for” loop doesn’t use brackets so I’m not sure if you intend it to just print spaces or not. It may or may not do anything though (see #1)

  3. You erase your user input in the next “for” loop ( "for (i = 0; i <= i; i++) " ) so any work you’ve started is now gone. See what you did? i = 0. User input gone. i <= i.. well, zero is less than or equal to zero so.. run it once.

I didn’t run the code but I’m guessing i and j stay at zero permanently.

After you fix all that, I highly recommend you step it through a debugger, or put in some other print statements (eg. right after you start each “for” loop) so you know what your values are. Example: print("i is now " + i); You have a little of that but apparently not enough to catch your mistakes.

A variable will always equal itself, so that’s an infinite loop right there.

I’m not entirely certain what you are trying to accomplish, but if you change your first i to another variable name, you will at least get rid of the infinite loop and be able to work from there. Just so there is no confusion, below is your code with the variable I am talking about changed to “userInput”.



import java.util.Scanner;

public class Times  
{  
  
	public static void main(String[] args)  
	{  
		Scanner keyboard = new Scanner(System.in);
		int i,j, userInput;
		
		System.out.println("Enter the limit of the table.");
		userInput= keyboard.nextInt();
		
		System.out.print("   ");  
		for (j = 0; j <= userInput; j++)  
		System.out.print("  " + j);  
		System.out.print('
');   
  
		for (i = 0; i <= userInput; i++)  
   
		{  
			System.out.print("  " + i);  
			for (j = 0; j <= i; j++)  
			{  
				String display = "";  
  
				if (i * j < 10)  
				{  
					display += "  " + i * j;  
					System.out.print(display);  
				}  
				else   
				{  
					display += " " + i * j;  
					System.out.print(display);  
				}  
			}  
			System.out.print('
');  
		}  
	}    
}


On that note, you should name your variables something useful, otherwise they have no meaning to anyone except you and they only have that meaning as you are developing. Imagine you are coming back to this code a year from now.. What do “i” and “j” represent?

The if statement is also unnecessary. The code is the same no matter if it passes or fails the evaluation. But that doesn’t effect the function of the program.

oh sorry, ugh… I thought it was simpler to use the “i” and “j” as variables. what I was trying to make was a multiplication table based on the userInput. So if the user put “4” as the limit, the table would show the multiplication table up to the number “4.”
There is a problem that bugs me in that the table is only partially complete. The table displays only half the variables, but I think i fixed it. On a side note, are there any suggestions for good debuggers?


import java.util.Scanner;

public class Times  
{  
  
	public static void main(String[] args)  
	{  
		Scanner keyboard = new Scanner(System.in);
		int i,j, userInput;
		
		System.out.println("Enter the limit of the table.");
		userInput= keyboard.nextInt();
		
		System.out.print("   ");  
		for (j = 0; j <= userInput; j++)  
		System.out.print("  " + j);  
		System.out.print('
');
System.out.print(" " + i *j);  
 
  
		for (i = 0; i <= userInput; i++)  
   
		{  
			System.out.print("  " + i);  
			for (j = 0; j <= i; j++)  
			{  
				String display = "";  
  
				if (i * j < 10)  
				{  
					display += "  " + i * j;  
					System.out.print(display);  
				}  
				else   
				{  
					display += " " + i * j;  
					System.out.print(display);  
				}  
			}  
			System.out.print('
');  
		}  
	}    
}

You can use System.out.printf() or String.format() to handle setting the output to a proper width rather than having to do the “if i*j < 10, add this many spaces, else add one fewer space” part of the program.

Also, you could try Eclipse for an IDE to write this in. It’s got a built-in debugger that will let you step through your program and see what’s going on, and there are versions available for Windows, Linux, and Mac.