d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Easy Java Program Problems > Help Fix Them?
Add Reply New Topic New Poll
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Mar 19 2013 06:24pm
Here's my code
I'm not sure what I've done wrong. If someone can tell me, and show me how to fix it, that would be great. Thanks in advance.

import java.util.*;
import javax.swing.JOptionPane;

public class ProgramC

{
String accNum;

System.out.print("Enter your account number.");
accNum = console.nextString();


switch(accNum)
{
case 46728:
System.out.println(" Account Number: 46728\n Account Type: Savings\n Minimum Balance: $1,000\n New Balance:$"+2700*0.04);
break;
case 87324:
System.out.println(" Account Number: 87324\n Account Type: Checking\n Minimum Balance: $1,500\n New Balance:$"+7689*0.03);
break;
case 79873:
System.out.println(" Account Number: 79873\n Account Type: Savings\n Minimum Balance: $1,000\n New Balance:$790");
break;
case 89832:
System.out.println(" Account Number: 89832\n Account Type: Checking\n Minimum Balance: $1,000\n New Balance: $725");
break;
default:
System.out.println("Invalid Account Number.");
}
}

Here are the error messages.

ProgramC.java:15: error: <identifier> expected
System.out.print("Enter your account number.");
^
ProgramC.java:15: error: illegal start of type
System.out.print("Enter your account number.");
^
ProgramC.java:16: error: <identifier> expected
accNum = console.nextString();
^
ProgramC.java:19: error: illegal start of type
switch(accNum)
^
ProgramC.java:19: error: <identifier> expected
switch(accNum)
^
ProgramC.java:21: error: orphaned case
case 46728:
^
6 errors
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 19 2013 06:28pm
Save this somewhere else (notepad, etc) and redo this. as soon as you get one compile error, fix it before moving on.

console is not defined, and you need all that inside a method. wouldnt surprise me if there are more problems
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Mar 19 2013 06:44pm
Quote (carteblanche @ Mar 19 2013 08:28pm)
Save this somewhere else (notepad, etc) and redo this. as soon as you get one compile error, fix it before moving on.

console is not defined, and you need all that inside a method. wouldnt surprise me if there are more problems


I'm very new to programming, and if I re-wrote this, I would do it the exact same way. I just don't see where my problem is. I can pay for the help if needed.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 19 2013 06:49pm
Don't write the whole thing then compile when it's done. you get the mess you see. compile it line by line. i recommend using an IDE. when you just have 1 problem at a time, it's much easier to trouble shoot.
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Mar 19 2013 07:55pm
Quote (carteblanche @ Mar 19 2013 08:49pm)
Don't write the whole thing then compile when it's done. you get the mess you see. compile it line by line. i recommend using an IDE. when you just have 1 problem at a time, it's much easier to trouble shoot.


I use an IDE, jGRASP. I'm trying to re-write it, still getting problems though.

....fixed it, thanks. The only problem was that I left out, "static Scanner console = new Scanner(System.in);"

This post was edited by ben_graham7 on Mar 19 2013 08:24pm
Member
Posts: 15,962
Joined: Feb 8 2013
Gold: 22,120.00
Mar 22 2013 09:49am
IMO make this object oriented....

make a class account or client what ever.

attributes:
accountNo
accountType
minBalance
newBalance

with a get/set on each. then instead of making a switch to make a hardcoded display, you just display it dynamically example :
System.out.println(" Account Number: " + client.getAccountNumber + " \n Account Type: " + client.getAccountType + "\n Minimum Balance: $" + client.getMinBalance + "\n New Balance: $" + client.getNewBalance * 0.04 + ")";
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 22 2013 10:22am
imo replace....

Code
String accNum;

System.out.print("Enter your account number.");
accNum = console.nextString();


switch(accNum)
{


with

Code

// there's no function called nextString for Scanner
// and there's no point in declaring variables if you're only going to use them once
switch(new Scanner( System.in ).next)
{


Also, you're using a String for your switch and comparing to integers. You're gonna have problems here. You should put your cases in quotes, so...

This is assuming you're using JRE1.7. If you're using an earlier version of Java, you must use integers for your switch.

Code
case "46728":


Go Back To Programming & Development Topic List
Add Reply New Topic New Poll