DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2006
    Posts
    1

    help with java programming

    Hey all, im really new to programming and i need help on my assignment.
    I have a task where i ask the user if they want to pick a number between 1-100 or if they want the computer to do it. Now my probem is i want to direct the user to a different class where they input to some questions but im not sure how to do it. Here is an example of my programming where i want the user to be directed to the the UserPick class if they anwsered no.

    class Main
    if(answer.equalsIgnoreCase("y")){
    myRndPick=new RndPick(answer); <----here is my problem
    }else if(answer.equalsIgnoreCase("n")){
    myUserPick=new UserPick(answer); <----here is my problem
    }else{
    System.out.print("Please enter y or n");
    }

    class UserPick{
    double userNum, total;
    String dataIn;
    BufferedReader br;
    UserPick(String answer) throws IOException{

    System.out.println("Please enter a quantity between 1 and 100: ");
    dataIn=br.readLine();
    userNum=Double.parseDouble(dataIn);
    total=userNum*5;
    }
    }

    Any help is greatly appreciated thanks.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    I'm really confused... Take a look at this:
    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Main {
        private static Scanner scanner = new Scanner(System.in);
        
        public static void main(String[] args) {
            System.out.println("Would you like to pick the number? (y/n)");
            char pickNum = scanner.next().charAt(0);
            
            int randNum;
            if (pickNum == 'y') {
                randNum = scanner.nextInt();
            } else {
                randNum = new Random().nextInt(100);
            }
        }
    }
    Last edited by destin; 03-13-2006 at 04:28 PM.

  3. #3
    Join Date
    Mar 2006
    Location
    England
    Posts
    10

    Post

    I'm also a bit confused about what you're trying to do, including why you're forwarding the answer to e.g. new UserPick(). Also don't understand why these are where u have problems. in any case, check out the code below - perhaps it does something similar to what u want to do. note that since class UserPick is inside class Program, it can 'see' variable stdin.

    Code:
    public class Program
    {
        private BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        
        public static void main(String[] args)
          throws IOException
        {
            new Main();
        }
        
        public Program()
          throws IOException
        {
            System.out.print("Do you want computer to pick number? y/n: ");
            String answer = stdin.readLine();
            if (answer.equalsIgnoreCase("y")) {
                Object myRndPick = new RndPick();
            }
            else if (answer.equalsIgnoreCase("n")) {
                UserPick myUserPick = new UserPick();
            }
            else {
                System.out.print("Please enter y or n");
            }
        }
    
        class UserPick
        {
            double userNum, total;
            String dataIn;
            UserPick()
              throws IOException
            {
    
                System.out.print("Please enter a quantity between 1 and 100: ");
                dataIn = stdin.readLine();
                userNum = Double.parseDouble(dataIn);
                total = userNum * 5;
            }
        }
    }

Similar Threads

  1. initial Java programming
    By Kallahan in forum Java
    Replies: 1
    Last Post: 01-20-2003, 08:28 AM
  2. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 02:57 PM
  3. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 06:01 AM
  4. Replies: 2
    Last Post: 11-04-2000, 05:21 AM
  5. Replies: 2
    Last Post: 08-07-2000, 07:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links