Results 1 to 18 of 18

Thread: Starting java; script problems

  1. #1
    Join Date
    Jun 2006
    Posts
    1,492
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Starting java; script problems

    I am starting to learn java (from sean greasly ), and I encountered a problem in this script. Can anyone show me how to fix it?

    Code:
    package helloworld;
    
    import java.util.*;
    
    /**
     *
     * @author Kevin
     */
    public class Arrays {
    
        public static void main(String[] args) { 
            
            int[] studentAge = new int[5];
            Scanner theInput = new Scanner(System.in);
            
            for (int x = 0; x < studentAge.length; x++) {
                System.out.print("Enter age for student " + (x + 1) + ": ");
                studentAge[x] = theInput.nextInt();
            }
            
            System.out.println("The student ages are: ");
            
            for (int x = 0; x < studentAge.length; x++) {
                if (x == studentAge.length) {
                    System.out.println(studentAge);
                } else {
                    System.out.print(studentAge + ",");
                }            
            }
            
            theInput.close();
        }
        
    }

  2. #2
    Join Date
    Dec 2006
    Location
    Third rock from the sun.
    Posts
    2,510
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    What error are you getting? I see nothing wrong with it.

  3. #3
    Join Date
    Feb 2006
    Posts
    582
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I think he needs a try-catch clause around his Scanner object methods/declaration. I forget if you need it, but I'm pretty sure it can throw an IOException.
    Free File Hosting
    No download timers!

    Rifkwtf.com

  4. #4
    Join Date
    Jun 2006
    Posts
    1,492
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Haha, I didnt either, Jason.

    Well, after I start to run it, the script asks me for an input, before anything is appears. Then, if I input anything, it either

    a). Terminates and gives a bunch of scanner errors.
    b). Continues with the script after 5 inputs, but doesn't allow me to input anything when it asks for the ages.

    I probably do need a try catch, but I wouldn't know how to do that

  5. #5
    Join Date
    Dec 2006
    Location
    .̿̂̔͋͗̎̆ͥ̍̒ͤ͂̾̌̀̅
    Posts
    3,012
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    Lol @ sean greasley

    Hm, I can't see anything wrong with it.. and no errors for me either.

  6. #6
    Join Date
    Nov 2006
    Posts
    1,103
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    try using eclipse, its really usefull
    Infractions, reputation, reflection, the dark side of scripting, they are.

  7. #7
    Join Date
    Jun 2006
    Posts
    1,492
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Im using netbeans. Maybe i should try running it w/ command prompt?

  8. #8
    Join Date
    Dec 2006
    Location
    .̿̂̔͋͗̎̆ͥ̍̒ͤ͂̾̌̀̅
    Posts
    3,012
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    Hm, try that. I suggest eclipse though, pwns netbeans anytime anywhere.

  9. #9
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    Meh? Whats the difference between the two?

    Anyway, I didn't see any errors... But thats me, the newb.
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

  10. #10
    Join Date
    Feb 2006
    Posts
    582
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by GoF View Post
    Hm, try that. I suggest eclipse though, pwns netbeans anytime anywhere.
    You should probably know that IDEs are user preferred. I'd say Eclipse can lick my grundle because IntelliJ IDEA owns it, but some people wouldn't think so.

    Anyways, try using batch scripts to run it; perhaps you'll get different results.
    Free File Hosting
    No download timers!

    Rifkwtf.com

  11. #11
    Join Date
    May 2006
    Location
    Qld, Australia
    Posts
    223
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I just use Textpad, I don't like IDE's

    If I wanna Jar something I'll do it myself.

    Oh, Btw:
    if (x == studentAge.length - 1) {
    and
    studentAge[x]
    Since you had studentAge which is an object so print/println would use toString and that would return a hex


    EDIT: Oops sorry, I didn't see that this topic was old
    Haven't scripted in a while, Willing to proofread, test and provide feedback.

  12. #12
    Join Date
    Jun 2006
    Posts
    1,492
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    thanks

    Nah, its no problem, no one posts in these forums anyways lol

  13. #13
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    replace new Scanner(System.in) with this
    in = new BufferedReader(new InputStreamReader(System.in));

    then call
    in.readLine() to get the user to type in something and press enter
    Join the Official SRL IRC channel. Learn how to Here.

  14. #14
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    I also noticed a little thing in your for loop?

    for (int x = 0; x < studentAge.length; x++) {
    Im pretty sure you dont want to make x bigger until it is smaller than studentAge.length?

  15. #15
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by Infintry001 View Post
    Im using netbeans. Maybe i should try running it w/ command prompt?
    Netbeans rocks.

    Quote Originally Posted by n3ss3s View Post
    I also noticed a little thing in your for loop?



    Im pretty sure you dont want to make x bigger until it is smaller than studentAge.length?
    For (int i = 0; i < 10; i++) does the loop 10 times.
    Set/Int Number; WhileCommand, increasecommand



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  16. #16
    Join Date
    Feb 2006
    Location
    London, England
    Posts
    2,045
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by n3ss3s View Post
    I also noticed a little thing in your for loop?



    Im pretty sure you dont want to make x bigger until it is smaller than studentAge.length?
    Thats the same as saying

    PHP Code:
    for := 0 to Length(StudentAge)-do
    begin
    end

    in SCAR
    SRL Wiki | SRL Rules | SRL Stats
    Ultimate SCAR Scripting Tutorial | Starblaster100's Auth System | Join the official SRL IRC now!


    Help Keep SRL Alive! Please disable Advert Blockers on SRL! Help Keep SRL Alive!


  17. #17
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Sorry for double post!!



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  18. #18
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    My bad pl0x

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. script not starting?
    By sundeep125 in forum OSR Help
    Replies: 5
    Last Post: 11-17-2008, 06:15 PM
  2. Starting a Funorb style website - Need a Java Programmer
    By Squigglyo in forum Java Help and Tutorials
    Replies: 0
    Last Post: 10-03-2008, 02:29 PM
  3. Starting to Script
    By Suyeda in forum News and General
    Replies: 5
    Last Post: 10-31-2006, 04:02 AM

Posting Permissions

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