Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

n00b java question

Options
  • 30-11-2010 11:43am
    #1
    Registered Users Posts: 1,148 ✭✭✭


    Hi, I'm starting to teach myself Java. Having a bit of trouble with some code I wrote. I'm using an online compiler. My problem is when I run the code the time limit gets exceeded and there is no output as planned.The code is supposed to allow you to input characters, store them in an array and then output the characters.Is it a simple error or am I going about this the wrong way?

    This is the code I'm using:
    class Character{
     
    public static void main(String[] args){
       char a;
       char[] charArray;
       charArray = new char[10];
       for(int i = 0; i<10; i++){
       try{
          char ch = (char)System.in.read();
          while(ch!=' '){
             a = ch;
             charArray[i] = a;
             }
          }
          
       catch(java.io.IOException exp){ exp.printStackTrace();}
       }
       
       for(int i = 0; i<10; i++){
          System.out.println("The elements in the array are: "+charArray[i]);
       }
      }
    }
    

    Thanks in advance.


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Go and download the Sun JDK from Sun/Oracles site: http://java.sun.com. That will provide you with a free compiler without having to use the online version.

    Nothing stands out as a problem in the code, it should definitely compile for you.
    while(ch!=' '){
      a = ch;
      charArray[i] = a;
    }
    
    Remember that this will keep looping until you enter a space, and overwrite the character at the current position in the array. Not really the most efficient way of doing things. How about looping up till 10 elements or until the user enters a space.
    i<10;
    
    You can replace this with
    i < charArray.length
    


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    I would suggest the problem is with the online compiler.

    *Ah, answered above!


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    Thanks for the help. Once I got rid of the while loop it solved my problem :). I did download the Sun JDK, but I was at a bit of a loss with how to set up the path and classpath so I could actually compile my programs.


  • Registered Users Posts: 558 ✭✭✭wobbles-grogan


    I'm assuming you are using windows? In which case its pretty easy.

    Install the SDK into the default directory.
    C:/Program Files/Java/

    Then go to the control panel, double click on System and go to the "Advanced" tab.

    Click on Environment Variables down the bottom. Im the System Variables part, find the variable labelled "PATH". Edit that, and add the path to your java bin directories in there. Note that you need to put a semi-colon ";" after each entry. So add one onto the current path, then add

    "C:\Program Files\Java\jdk1.6.0_21\bin\;"
    and
    "C:\Program Files\Java\jdk1.6.0_21\jre\bin\;"
    to the end

    Note that the folder "jdk1.6.0_21" will change depending on what version of java you have...

    You should now be able to use javac and java on the command line...

    Hope that helps!


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    Thanks, I'll try that on windows whenever I get home from work. However, I'm dual booting my laptop with opensolaris and windows, and I was trying to do it on opensolaris. I may be changing from opensolaris to Ubuntu though, so it actually makes more sense to do it on Windows for now. Thanks again for the help :)


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 1,335 Mod ✭✭✭✭croo


    At a first quick glance, won't
    [B]while(ch!=' ')[/B]{
     a = ch;
     charArray[i] = a;
    }
    
    Mean it loops forever?
    ch won't be changed .. if it is not blank it will enter this loop and never end since it is never changed in that loop!?
    Seems a little obvious and many say it should be ok...??
    So perhaps I am just too optimistic! :)


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    I think you're right. I removed the while loop from my code and got the program to run. It works fine now.


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    croo wrote: »
    At a first quick glance, won't
    [B]while(ch!=' ')[/B]{
     a = ch;
     charArray[i] = a;
    }
    
    Mean it loops forever?
    ch won't be changed .. if it is not blank it will enter this loop and never end since it is never changed in that loop!?
    Seems a little obvious and many say it should be ok...??
    So perhaps I am just too optimistic! :)

    Yeah if the first character isn't a space, it will loop forever.


  • Registered Users Posts: 3,735 ✭✭✭Stuxnet


    try BlueJ, its great for beginners, it'll compile for you and highlight errors in your code,

    http://www.bluej.org/about/what.html


Advertisement