Results 1 to 16 of 16
Like Tree3Likes
  • 1 Post By allaudin
  • 1 Post By Junky
  • 1 Post By DarrylBurke

Thread: can someone help me to run this program caus' im havin hardtime to run this!

  1. #1
    blut.vene is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default 3rd looping statement problem

    import java.io.*;
    import java.lang.*;

    public class Conversion {


    public static void main(String args[])throws IOException
    { BufferedReader input;
    input = new BufferedReader(new InputStreamReader(System.in));

    String a, x, y;
    int select = 0, select2 = 0;

    while(select < 3)
    {
    System.out.println("You want to proceed? [y/n]? ");
    a = input.readLine();

    if(a.equals("y"))
    {
    while(select2 < 3)
    {
    select2 = 0;

    System.out.println("List of Conversions:");
    System.out.println("");
    System.out.println("Select[a]To Convert Decimal to Binary.");
    System.out.println("Select[b]To Convert Decimal to Octal.");
    System.out.println("Select[c]To Convert Decimal to Hexadecimal.");
    System.out.println("Choose wisely!: ");
    x = input.readLine();
    if(x.equals("a"))
    { select2 = 1;
    if(select2 == 1)
    {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a decimal number!:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String strs = Integer.toString(value,2);
    System.out.println("decimal:" + strs);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();
    if(y.equals("y"))
    {
    select2 = 0;
    }
    else if(y.equals("n"))
    {
    select2 = 2;
    select = 1;
    }

    }
    }


    else if(x.equals("b"))
    {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,8);
    System.out.println("octal:" + str);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();
    if(y.equals("y"))
    {
    select2 = 0;
    }
    else if(y.equals("n"))
    {
    select2 = 2;
    select = 1;
    }
    }
    }

    }
    else if(x.equals("c"))
    {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,16);
    System.out.println("hexa:" + str);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();
    if(y.equals("y"))
    {
    select2 = 0;
    }
    else if(y.equals("n"))
    {
    select2 = 2;
    select = 1;
    {
    }
    }

    else if(a.equals("n"))
    {
    select = 1;
    }

    if(select ==1)
    {
    System.out.println("Thanks");
    }
    }





    }
    }
    }
    having hard time to fix the 3rd looping statement!
    Last edited by blut.vene; 08-26-2013 at 07:44 AM.

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    It will not even compile without initialization of "x" cz in the start of the loop you are taking input to "a" . If "(a.equals("y"))" fails then how this " (x.equals("b")) " expression will execute as you haven't initialized "x".One more thing, edit your post and place your code between tags and go through forum rules ...

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    you are having problem with scope of while loops,just cut
    Java Code:
     if(x.equals("c"))
    {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,16);
    System.out.println("hexa:" + str);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();
    if(y.equals("y"))
    {
    select2 = 0; 
    }
    else if(y.equals("n"))
    {
    select2 = 2;
    select = 1;
    {
    }
    }
    
    else if(a.equals("n"))
    {
    select = 1;
    } 
    
    if(select ==1)
    {
    System.out.println("Thanks");
    }
    }
    part of your code and copy it to two braces up and in last else; write code for (a.equals("n")) .. and you are done ...
    Last edited by allaudin; 08-26-2013 at 06:12 AM.
    blut.vene likes this.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    Java Code:
    if(x.equals("a")) {
        select2 = 1;
        if(select2 == 1) {
    When will the inner if statement ever be false? Seems pointless to me.

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    Quote Originally Posted by Junky View Post
    Java Code:
    if(x.equals("a")) {
        select2 = 1;
        if(select2 == 1) {
    When will the inner if statement ever be false? Seems pointless to me.
    exactly I was thinking the same .. I am even unable to understand the purpose of these select variables as the condition for both while loops will always true .
    while(select < 3)
    {
    System.out.println("You want to proceed? [y/n]? ");
    a = input.readLine();

    if(a.equals("y"))
    {
    while(select2 < 3)
    {
    Last edited by allaudin; 08-26-2013 at 06:07 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    More ugly code: why create a new BufferedReader all the time. Just create one and use it to gather ALL user input. Just a waste of resources if you create more than one.
    allaudin likes this.

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    yup ... it seems he is a sheer beginner

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    Quote Originally Posted by blut.vene View Post
    can someone help me to run this program caus' im havin hardtime to run this!
    Please go through the Forum Rules with special reference to the third paragrpah. Then go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    blut.vene is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    Output:
    [a] for Decimal to Binary
    [b] for Decimal to Octal
    [c] for Decimal to Hexadecimal
    Choose one:

    ========================================
    example A:
    i.
    [a] for Decimal to Binary
    [b] for Decimal to Octal
    [c] for Decimal to Hexadecimal
    Choose one: a

    ii.
    Input value to convert: 100
    Decimal is: 1100100


    example B:
    i.
    [a] for Decimal to Binary
    [b] for Decimal to Octal
    [c] for Decimal to Hexadecimal
    Choose one: b

    ii.
    Input value to convert: 100
    Octal is: 144


    example C:
    i.
    [a] for Decimal to Binary
    [b] for Decimal to Octal
    [c] for Decimal to Hexadecimal
    Choose one: c

    ii.
    Input value to convert: 255
    Hexa is: FF
    this should be the output of that program,
    and im havin trouble with the third looping statement.
    Yep sirs im just a newbie at programming

  10. #10
    blut.vene is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    i'm sorry for the inconvinience btw how to change the thread name so that I can stick with the rule.

  11. #11
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    I think you should have separate functions for Binary, Octal and Hexadecimal ... and call these methods from main ...

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    Quote Originally Posted by blut.vene View Post
    i'm sorry for the inconvinience btw how to change the thread name so that I can stick with the rule.
    Let it be for this thread, but please keep the rules in mind when you have another question.

    db
    blut.vene likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    blut.vene is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    How to do that mr allaudin im really confused and having hard time.

  14. #14
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!


  15. #15
    blut.vene is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!

    thanks mr allaudin!
    I've already fixed the program

    here it goes
    import java.io.*;
    import java.lang.*;

    public class Conversion {


    public static void main(String args[])throws IOException
    { BufferedReader input;
    input = new BufferedReader(new InputStreamReader(System.in));

    String a, x, y;
    int select = 0, select2 = 0;


    while(select < 1)
    {
    System.out.println("You want to proceed? [y/n]? ");
    a = input.readLine();

    if(a.equals("y"))
    { //2 < 2
    while(select2 < 2)
    {
    select2 = 0;
    System.out.println("CONVERSION");
    System.out.println("");
    System.out.println("List of Conversions:");
    System.out.println("");
    System.out.println("[a] Decimal to Binary");
    System.out.println("[b] Decimal to Octal");
    System.out.println("[c] Decimal to Hexadecimal");
    System.out.println("Choose one: ");
    x = input.readLine();
    if(x.equals("a"))
    { select2 = 1;
    if(select2 == 1)
    {

    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,8);
    System.out.println("octal:=" + str);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();
    if(y.equals("y"))
    {
    select2 = 0;
    }
    else if(y.equals("n"))
    {
    select2 = 2;
    select = 1;
    }

    }//terminates IF for select2 variable
    }//terminates IF for x variable


    else if(x.equals("b"))
    {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,8);
    System.out.println("octal:=" + str);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();
    if(y.equals("y"))
    {
    select2 = 0;
    }
    else if(y.equals("n"))
    {
    select2 = 2;
    select = 1;
    }
    }

    else if(x.equals("c"))
    { select2 = 3;
    if(select2 == 3)
    {
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the decimal number:");
    String deci = buff.readLine();
    int value = Integer.parseInt(deci);
    String str = Integer.toString(value,16);
    System.out.println("octal:=" + str);
    System.out.println("do you want to go back? [y/n]?");
    y = input.readLine();

    switch (y) {
    case "y":
    select2 = 0;
    break;
    case "n":
    select2 = 2;
    select = 1;
    if(y.equals("n"))
    {
    select = 1;
    }if(select ==1)
    {
    System.out.println("Thanks");
    }
    break;
    }
    }
    }
    }
    }
    }
    }
    }

  16. #16
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: can someone help me to run this program caus' im havin hardtime to run this!


Similar Threads

  1. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  2. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  3. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •