Your Ad Here Your Ad Here
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Java Programming

  1. #1
    I am a noob in programming. I need a bit help.

    How would i

    -Allow the user to input any string and output it completely reveresed.
    -Code required to bubble sort a list of 10 names.


    Thnx.

    Also does anybody know what are the advantages of parmater passing and what does it mean when an IDE says "null pointer exception out of bounds exception"

  2. Software & Hardware   -   #2
    BANNED
    Join Date
    Jul 2003
    Location
    Guatemala
    Posts
    4,097
    You insert what you read into an array.

    Then go from the last position of the array to the first, printing every letter/thing in each

  3. Software & Hardware   -   #3
    eh i learned intro java for a month during the summer

    your missing a throws IOException in the beginning... and try searching google for a bubble sort... i dont remember how to write a bubble sort at this moment but i can try looking for my old one.

    basicly its an array, for loop, and some if statements

  4. Software & Hardware   -   #4
    Originally posted by DWk@27 January 2004 - 01:28
    You insert what you read into an array.

    Then go from the last position of the array to the first, printing every letter/thing in each
    So here is the program.

    import java.awt.*;
    import hsa.Console;
    public class project-v1

    {
    static Console c = new Console ();
    static Delay d = new Delay ();
    public static void main (String[] args)
    {
    String name;
    c.println ("plz enter a name") //lets suppose a user enter Adam.
    name=c.readLine ();



    What do i do next. I dont need to declare an array for a single name.
    }


    }
    -----------------------------------------------------------------------------------

    @soul814

    I know Bubble sort alogrithm but i just dont know how to use it.

  5. Software & Hardware   -   #5
    Originally posted by ultimatejester@26 January 2004 - 18:07
    "null pointer exception out of bounds exception"
    This occurs when you are trying to refer to a location that doesnt exist. Usually occurs if you have an array of say n items and you try to access (n+1)th item. Might happen when you are sorting so your limits are not set right.
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  6. Software & Hardware   -   #6
    BANNED
    Join Date
    Jul 2003
    Location
    Guatemala
    Posts
    4,097
    Yea remember the array&#39;s positions are 0,1,2,3.... and not 1,2,3,4....

  7. Software & Hardware   -   #7
    What do i do next. I dont need to declare an array for a single name.
    }


    }
    It is fairly simple. You already have the string in name.
    Then do something like this
    Code:
    for &#40;int i=name.length&#40;&#41;;i&#60;0;i--&#41;{
    
    System.out.println&#40;mystr.charAt&#40;i&#41;&#41;; 
    
    }
    This will print out the whole string backwards character by character. There are other ways too.

    Hope this helps,
    I.am
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

  8. Software & Hardware   -   #8
    Poster
    Join Date
    Feb 2003
    Location
    Boston, MA
    Posts
    333
    http://java.sun.com/j2se/1.4.2/docs/api/

    The Java API, gives you everything you need to know about current methods, classes subclasses blah blah blah all that good stuff.

    ALSO

    Just a correction to I.Am&#39;s code. The way he had it would have made the for loop be false the first evaluation and then the index would have been out of bounds and a couple errors. He had the right idea tho.

    I also added some other things, and they nicely had a vb tag for code yay

    Code:
    for &#40;int i=name.length&#40;&#41;-1;i&#62;=0;i--&#41;{
    
    System.out.print&#40;name.charAt&#40;i&#41;&#41;; *//your choice whether you want a new line for each letter or all on the same line
    
    }
    As for the bubble sort I need a little more info on the strings, as in how do you want to compare them etc etc.

    first you would need your array say

    Code:
    String &#91;&#93; names = new String&#91;10&#93;;
    
    //you can throw in a loop to add the names to the array upon entry
    
    for &#40;int x=0;x&#60;10;x++&#41; //something like this
    {
    c.println &#40;&#34;plz enter a name&#34;&#41; //lets suppose a user enter Adam.
    name=c.readLine &#40;&#41;;
    names&#91;x&#93;=name; //adds the name to the array
    }
    Now your names are all added.

    As for the bubble sort I&#39;m assuming you&#39;re going to want to compare the strings lexigraphically probably ignoring case I would assume so you would need to use compareToIgnoreCase()

    and...something like this, don&#39;t kill me if I&#39;m wrong though I&#39;m always rusty on my algorithms
    Code:
    //this bubble sort will sort the names in ASCENDING order
    String temp = new String;
    for &#40;int x=0;x&#60;names.length-1; x++&#41; //outer for loop
    { * * for &#40;int y=0; y&#60;names.length-1-x; y++&#41;//inner for loop
     * * *{
     * * * * * * *if &#40;names&#91;y&#93;.compareToIgnoreCase&#40;names&#91;y+1&#93;&#41;==1&#41;
     * * * * * { 
     * * * * * * * * * * * *temp = names&#91;y&#93;;
     * * * * * * * * * * * *names&#91;y&#93; = names&#91;y+1&#93;;
     * * * * * * * * * * * *names&#91;y+1&#93; = temp;
     * * * * * } //end if
     * * *} //end inner for
    } //end outer for
    I hope this is all formatted nicely and I made few mistakes or no mistakes =P

  9. Software & Hardware   -   #9
    h1
    Guest
    Learn from this class (don&#39;t steal it). If you get any good ideas, use them.

    A real program would have classes to handle I/O, but this isn&#39;t a real program.

    File: Reverse.java
    Code:
    import javax.swing.JOptionPane // class for creating simple windowed interfaces
    public class Reverse // class definition
    {
     &nbsp; &nbsp; public static void main&#40;String&#91;&#93; args&#41; // main function
     &nbsp; &nbsp; {
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String output = &#34;&#34;, input = JOptionPane.showInputDialog&#40;&#34;Please enter your name.&#34;&#41;; // user input
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;long i = 0; // simple definition
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for &#40;i = input.length&#40;&#41;; i &#62; 0; i--&#41; // count down by 1 from the length of the input to 0
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output += input.charAt&#40;i&#41;; // adds character at i to output
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JOptionPane.showMessageDialog&#40;null, &#34;Your name reversed is &#34; + output + &#34;.&#34;&#41;; // output
     &nbsp; &nbsp; }
    }
    File: Entry.java
    Code:
    import javax.swing.JOptionPane; // class for creating simple windowed interfaces
    public class Entry // class definition
    {
     &nbsp; &nbsp; public static void main&#40;String&#91;&#93; args&#41; // main function
     &nbsp; &nbsp; {
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String&#91;&#93; input = new String&#91;9&#93;; // new string array with 10 values
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String output = &#34;&#34;;
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;long i = 0, j = 0; // simple definitions
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for &#40;i = 0; i &#60; 10; i++&#41; // from 0 to 10, increasing by 1
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input&#91;i&#93; = JOptionPane.showInputDialog&#40;&#34;Please enter name number &#34; + i + &#34;.&#34;&#41;; // user input
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for &#40;j = 0; j &#60; 10; j++&#41; // from 0 to 10, increasing by 1
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output += &#34;Name number &#34; + &#40;i + 1&#41; + &#34; is &#34; + input&#91;i&#93; + &#34;.&#092;n&#34;; // adds to output
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JOptionPane.showMessageDialog&#40;output&#41;; // output
     &nbsp; &nbsp; }
    }
    Hope this helps.

  10. Software & Hardware   -   #10
    You are right Schuler, i forgot to subtract one from the length as the index starts at 0. This is what happens when you write in 30 secs after seeing the problem and get excited about it
    <span style='color:black'> I am a part of all that I have met - Lord Tennyson</span>
    <span style='color:blue'>Try not to let your mind wander...it is too small and fragile to be out by itself</span>

Page 1 of 2 12 LastLast

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
  •