Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to translate Javascript to Java - help fix?
    primarykey
    data
    text
    <p>I'm trying to learn Java, but I'm struggling a little bit. I'm trying to do an assignment from a textbook, so I did it in javascript and was using my limited knowledge of Java to convert it over. (Here's the original instructions - <a href="http://i518.photobucket.com/albums/u341/ACrippledFerret/5ea8ec0e-02aa-4b96-b207-a83c52f8db48_zps2cd2ab7f.jpg" rel="nofollow">http://i518.photobucket.com/albums/u341/ACrippledFerret/5ea8ec0e-02aa-4b96-b207-a83c52f8db48_zps2cd2ab7f.jpg</a>)</p> <p>I think I've got most of it correct, but I'm running into a couple of errors. Almost all of it is "(variable) cannot be resolved to a variable". It happens to lots of variables in my last method. I also seem to have an issue with a bracket somewhere... "Syntax error on token "}", { expected after this token".</p> <p>If anyone can help fix this code, I would be very grateful. I am new to Java so this is a bit tough for me to translate. The first set of code is the javascript, the second set of code is my translated java (That's not working). Thanks for any help.</p> <p>JAVASCRIPT</p> <pre><code>window.onload=function(){ var difficulty = 1, operators = ['plus', 'minus', 'times', 'divided by'], selectedOperator = 1, correctAnswers = 0, answeredTyped = 0; var difficultyInput = parseInt(prompt('Please choose the difficulty. Enter the number of digits to use in each problem.'), 10); if(difficultyInput &gt; 0) { difficulty = difficultyInput; } var arithmeticMethod = parseInt(prompt('Choose an arithmetic problem to study:\n1 = Addition Only\n2 = Subtraction Only\n3 = Multiplication Only\n4 = Division Only\n5 = Random Problems'), 10); if(arithmeticMethod == 5 || operators[arithmeticMethod - 1]) { selectedOperator = arithmeticMethod; } function checkResponse(primaryInt, secondaryInt, operatorText, suggestedAnswer) { var result = false; switch (operatorText) { case 'plus': return (primaryInt + secondaryInt) == suggestedAnswer; case 'minus': return (primaryInt - secondaryInt) == suggestedAnswer; case 'times': return (primaryInt * secondaryInt) == suggestedAnswer; case 'divided by': return (primaryInt / secondaryInt) == suggestedAnswer; default: return false; } } function displayResponse(isCorrect) { var randomIndex = Math.floor(Math.random() * (4 - 1 + 1)) + 1; switch (randomIndex) { case 1: return isCorrect ? 'Very good!' : 'No. Please try again.'; case 2: return isCorrect ? 'Excellent!' : 'Wrong. Try once more.'; case 3: return isCorrect ? 'Nice Work!' : 'Don\'t give up!'; case 4: return isCorrect ? 'Keep up the good work!' : 'No. Keep trying.'; default: return 'Woops...'; } } function askQuestion() { var correctAnswer = false; var primaryInt = Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1)); secondaryInt = Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1)); operatorText = (selectedOperator == 5) ? operators[Math.floor(Math.random() * operators.length)] : operators[selectedOperator - 1]; while(!correctAnswer &amp;&amp; answeredTyped &lt; 10) { var response = parseFloat(prompt('How much is ' + primaryInt + ' ' + operatorText + ' ' + secondaryInt + '?')); correctAnswer = checkResponse(primaryInt, secondaryInt, operatorText, response); alert(displayResponse(correctAnswer)); answeredTyped++; if(correctAnswer) correctAnswers++; } } while(answeredTyped &lt; 10) { askQuestion(); } if((correctAnswers / answeredTyped) &gt;= 0.75) { alert('Congratulations, you are ready to go on the next level!'); } else { alert('Please ask your teacher for extra help.'); } } </code></pre> <p>JAVA</p> <pre><code>import java.util.*; import javax.swing.JOptionPane; /** * */ /** * @author Tyler * */ public class Assignment2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int difficulty = 1; String[] operators = {"plus", "minus", "times", "divided by"}; int selectedOperator = 1; int correctAnswers = 0; int answeredTyped = 0; int difficultyInput = Integer.parseInt(JOptionPane.showInputDialog("Please choose the difficulty. Enter the number of digits to use in each problem.")); if (difficultyInput &gt; 0) { difficulty = difficultyInput; } int arithmeticMethod = Integer.parseInt(JOptionPane.showInputDialog("Choose an arithmetic problem to study: 1 = Addition Only, 2 = Subtraction Only, 3 = Multiplication Only, 4 = Division Only, 5 = Random Problems" )); selectedOperator = arithmeticMethod; } public static boolean checkResponse (double primaryInt, double secondaryInt, String operatorText, float response){ boolean result = false; switch (operatorText){ case "1": return (primaryInt + secondaryInt) == response; case "2": return (primaryInt - secondaryInt) == response; case "3": return (primaryInt * secondaryInt) == response; case "4": return (primaryInt / secondaryInt) == response; } return false; } public static String displayResponse (boolean isCorrect){ int randomIndex = (int) (Math.floor(Math.random() * (4 - 1 + 1)) + 1); switch (randomIndex){ case 1: return isCorrect ? "Very Good!" : "No. Please try again."; case 2: return isCorrect ? "Excellent!" : "Wrong. Try once more."; case 3: return isCorrect ? "Nice Work!" : "Don\'t give up!"; case 4: return isCorrect ? "Keep up the good work!" : "No. Keep trying."; } return "Oops..."; } public static void askQuestion(int difficulty, String operatorText, int selectedOperator, int answeredTyped, String[] operators, int correctAnswers){ boolean correctAnswer = false; double primaryInt = Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1)); double secondaryInt = Math.floor(Math.pow(10, difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1)); operatorText = (selectedOperator == 5) ? operators[(int) Math.floor(Math.random() * operators.length)] : operators[selectedOperator - 1]; while(!correctAnswer &amp;&amp; answeredTyped &lt; 10) { float response = Float.parseFloat (JOptionPane.showInputDialog("How much is " + primaryInt + " " + operatorText + " " + secondaryInt + "?")); correctAnswer = checkResponse (primaryInt, secondaryInt, operatorText, response); JOptionPane.showMessageDialog(null, displayResponse(correctAnswer)); answeredTyped++; if(correctAnswer) correctAnswers++; } { while(answeredTyped &lt; 10){ askQuestion(0, null, 0, 0, null, 0); } if((correctAnswers / answeredTyped) &gt;= 0.75) { JOptionPane.showMessageDialog(null, "Congratulations, you are ready to go on to the next level!"); } else{ JOptionPane.showMessageDialog(null, "Please ask your teacher for extra help."); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI would urge you not to do this since you must change mind sets when you use a different programming language. I think that 1) You would likely do best if you just start from Java and skip your JavaScript intermediate step. 2) Don't try to add good code to bad. 3) If you can't use an IDE to immediately recognize compiler errors, then compile often and fix the errors before trying to add new code.
      singulars
    2. COIf you're using an IDE, they can often help you find the {} pairs. Eclipse will highlight the corresponding { if you put the cursor immediately after a }. Or you can hit ctrl-shift-f to format the full class, which will do all of the indenting, and then you can look for where something doesn't indent where you expect it. Some non-IDE editors such as Notepad++ can also help with {} pairs by allowing you to collapse the code within them, and you can look for where that doesn't do what you expect and zero in from there.
      singulars
    3. COI'd urge you to try the techniques I mentioned to find the extra '}' (so stop reading now and give it a try; practicing this is way better than being given the answer!), but if you don't have any luck, there's an extra '}' at the end of your first while loop in askQuestion(). (You can actually see it in the formatting of the code you posted; the other whiles don't line up with the first one.)
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload