Curly Brackets Placed Incorrectly

Oct 22, 2014

At this moment in time my program compiles and runs as I want it to, but when I look at my code I see that my While Loops braces are placed inside my Try Block. When I move these braces outside of the Try Block, next to the WhileLoop, the program doesn't run like I want it to.

Likewise, when I place the While(answerIsCorrect) inside the Try Block, it doesn't work like I want it to either.

Also, my problem is on Lines 17 and 18.

import javax.swing.*;
import java.util.Random;
public class SwingInputExample
{
public static void main(String args[])
{
int firstNumber=0;
int secondNumber=0;
int correctAnswer=0;
String studentGuess = null;

[Code] ....

View Replies


ADVERTISEMENT

Tortoise / Hare Applet Displaying Incorrectly

Sep 2, 2014

I've almost completed a Java class, but I've hit a snag on the final project. I have to design a code that will display an applet of a tortoise and hare racing each other. There are certain outcomes when a random integer between 1 and 10 is selected that either cause the animal to move forward or fall back. My code compiles correctly, but when it runs, only one of the two animals is displayed. How to fix it such that both animals will be displayed at the same time so I can view how they are in comparison to each other.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.applet.*;
import java.net.URL;
import java.util.*;

public class Project2A extends Applet
{
Random gen = new Random();
private int harePosition;
private int tortoisePosition;

[Code] ....

View Replies View Related

Curly Braces In If And Else Statements

Nov 18, 2014

I have read some on this but I'm trying to understand a code and the following part confuses me

if( thisCount == bestCount )
bestCandidates.add( candidate );
else if( thisCount > bestCount ) {
bestCount = thisCount;
bestCandidates.clear();
bestCandidates.add( candidate );
}
}

What I find confusing is this: If I am not mistaken the need for curly braces occurs when using more than one statement and if you use else statements.

So I wonder if I have missunderstood about the else statement and that you dont need curly braces around a one statement if statement that is followed by an else statement...

View Replies View Related

What Empty Curly Braces Will Do In If Statement

Oct 29, 2014

i created a class and a constructor for the class. then i used getters and setters.in the setters i'm trying to write a line that will be -

if (num < 0|| num>120) {
dont change value and do nothing
}
else{
num1 = num
}

how can i do this ? i tried to put an empty curly braces but it gives me an error.

View Replies View Related

Meaning Of Double Curly Brace In Set

Feb 14, 2014

I saw code like this.

Set<String> set = new HashSet<String>(){{ add("Hello"); }};

I can understand that it is a HashSet with one element "Hello" in it. I don't understand this syntax. Why there are double curly braces?

View Replies View Related

How Curly Braces Follow Assignment Statement

Apr 7, 2015

@Override
@SuppressWarnings("unchecked")
public TableCell<S, T> call(TableColumn<S, T> p) {
TableCell<S, T> cell = new TableCell<S, T>() {
@Override
public void updateItem(Object item, boolean empty) {
if (item == getItem()) {
return;
}

The line in the code above TableCell<S, T> cell = new TableCell<S, T>() {

I don't understand how curly braces follow an assignment statement?What does the line of code mean?

View Replies View Related

Evaluating Expression Without Brackets

Feb 3, 2015

I am working on a program to evaluate an expression without brackets using stacks. I am getting the following error :

3+1*2+4
-2.0

Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at java.util.Stack.pop(Stack.java:84)
at assignment2.Evaluate.main(Evaluate.java:42)

I imported the Stack from java.util.Stack

package assignment2;
import java.util.Scanner;
import java.util.Stack;
public class Evaluate {
public static void main(String[] args) {
Stack<String> ops = new Stack<String>();

[Code] ....

What is causing this error? Does it look like the program should function as intended?

View Replies View Related

ArrayList Output Display Without Brackets?

Feb 9, 2015

I'm trying to get my output to be displayed without the brackets.The output looks like this

22 ([1, 2, 11])
33 ([1, 3, 11])
44 ([1, 2, 4, 11, 22])
55 ([1, 5, 11])
66 ([1, 2, 3, 6, 11, 22, 33])
77 ([1, 7, 11])

But I want it to have no brackets displayed so it looks like this

22 (1, 2, 11)
33 (1, 3, 11)
44 (1, 2, 4, 11, 22)
55 (1, 5, 11)
66 (1, 2, 3, 6, 11, 22, 33)
77 (1, 7, 11)

import java.util.ArrayList;
public class Palindrome {
public static void main(String[] args) {
int e = 0;
// palindromic composite number
int drome = 0;

[Code] ....

View Replies View Related

Create A Program That Will Calculate Different Tax Brackets

Oct 3, 2014

I have spent the past 12 hours on it and have gotten no where. I need to create a program that will calculate different tax brackets. I have tried all combinations I can think of and can't get it to work!!! I am literally about to go insane! I need the program to calculate tax will be 1% for anything up to $50000. If the tax is over $50000, you keep that 1% of $50000 and then add the remaining. So for an income in the second range, it would be 1% of $50000, and then 2% of (income - $50000). This then continues for each range.

The problem I'm having is getting it to display the correct interest rates. I am not asking for you to write it for me. I want to learn. But at this point I have exhausted all resources available to me.

import java.util.Scanner;
public class Project3taxinfo {
public static void main(String[] args) {
final double RATE1 = 0.01; //1% tax on the first $50,000
final double RATE2 = 0.02; //2% tax on the amount over $50,000 up to $75,000
final double RATE3 = 0.03; //3% tax on the amount over $75,000 up to $100,000
final double RATE4 = 0.04; //4% tax on the amount over $100,000 up to $250,000
final double RATE5 = 0.05; //5% tax on the amount over $250,000 up to $500,000
final double RATE6 = 0.06; //6% tax on the amount over $500,000

[code]....

View Replies View Related

Removing Brackets From Array List Printout

Apr 10, 2009

When you "system.out.print(arraylist)" an arraylist, it will give you something like [item1, item2].

Im wondering how I can remove the "[" and "]" brackets fromt he printout, or even if i pass it in as another variable.

View Replies View Related

Inserting Else Statement And Multiple Brackets At Various Places

Jan 16, 2014

I'm using this try statement to check for errors in input in my Tic Tac Toe game. The problem I have having is I don't understand why this error is happening. It's preventing my code from compiling. I've tried inserting an else statement and multiple brackets at various places with no success.

Java Code:
public void play(){
Scanner input = new Scanner(System.in);
int row, col;
char currPlayer = 'X';

[code]....

View Replies View Related

Balanced Brackets Execution - Keep Getting Empty Stack Exception

Oct 21, 2014

Im trying to do this program but I keep getting Empty Stack Exception when I execute balancedBrackets. I know that push and pop methods works, but I'm not so sure if my logic for balancedBrackets is correct.

public void push(T t){
if(this.top+1==this.size){
duplicateCapacity();
}
else{
top++;
arr[top]=t;

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved