PDA

View Full Version : Java Programming



ultimatejester
01-27-2004, 01:07 AM
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" :) :)

DWk
01-27-2004, 01:28 AM
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 :)

ooo
01-27-2004, 01:29 AM
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

ultimatejester
01-27-2004, 01:35 AM
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.

I.am
01-27-2004, 02:43 AM
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.

DWk
01-27-2004, 02:46 AM
Yea remember the array's positions are 0,1,2,3.... and not 1,2,3,4....

I.am
01-27-2004, 03:03 AM
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


for (int i=name.length();i<0;i--){

System.out.println(mystr.charAt(i));

}

This will print out the whole string backwards character by character. There are other ways too.

Hope this helps,
I.am

Schuler
01-27-2004, 03:40 AM
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'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


for (int i=name.length()-1;i>=0;i--){

System.out.print(name.charAt(i)); //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


String [] names = new String[10];

//you can throw in a loop to add the names to the array upon entry

for (int x=0;x<10;x++) //something like this
{
c.println ("plz enter a name") //lets suppose a user enter Adam.
name=c.readLine ();
names[x]=name; //adds the name to the array
}

Now your names are all added.

As for the bubble sort I'm assuming you'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't kill me if I'm wrong though I'm always rusty on my algorithms :P


//this bubble sort will sort the names in ASCENDING order
String temp = new String;
for (int x=0;x<names.length-1; x++) //outer for loop
{ for (int y=0; y<names.length-1-x; y++)//inner for loop
{
if (names[y].compareToIgnoreCase(names[y+1])==1)
{
temp = names[y];
names[y] = names[y+1];
names[y+1] = 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

h1
01-27-2004, 05:24 AM
Learn from this class (don't steal it). If you get any good ideas, use them.

A real program would have classes to handle I/O, but this isn't a real program. :D

File: Reverse.java
import javax.swing.JOptionPane // class for creating simple windowed interfaces
public class Reverse // class definition
{
    public static void main(String[] args) // main function
    {
         String output = "", input = JOptionPane.showInputDialog("Please enter your name."); // user input
         long i = 0; // simple definition
         for (i = input.length(); i > 0; i--) // count down by 1 from the length of the input to 0
         {
              output += input.charAt(i); // adds character at i to output
         }
         JOptionPane.showMessageDialog(null, "Your name reversed is " + output + "."); // output
    }
}
File: Entry.java
import javax.swing.JOptionPane; // class for creating simple windowed interfaces
public class Entry // class definition
{
    public static void main(String[] args) // main function
    {
         String[] input = new String[9]; // new string array with 10 values
         String output = "";
         long i = 0, j = 0; // simple definitions
         for (i = 0; i < 10; i++) // from 0 to 10, increasing by 1
         {
              input[i] = JOptionPane.showInputDialog("Please enter name number " + i + "."); // user input
         }
         for (j = 0; j < 10; j++) // from 0 to 10, increasing by 1
         {
              output += "Name number " + (i + 1) + " is " + input[i] + ".\n"; // adds to output
         }
         JOptionPane.showMessageDialog(output); // output
    }
}
Hope this helps.

I.am
01-27-2004, 05:28 AM
:lol: 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 :lol: :lol:

Schuler
01-27-2004, 05:38 AM
lol I know the feeling I.Am.

and a quick scan of your code haxor

String[] input = new String[9]; // new string array with 10 values

need to change that 9 to a 10. But yeah JOptionPane is my prefered way to do things to, although cin and cout from c++ seemed so much simpler

I.am
01-27-2004, 05:44 AM
Originally posted by Schuler@26 January 2004 - 22:38
need to change that 9 to a 10.
Nah it seems right. It will create a String with 10 characters (0-9)

Edit:

cin and cout from c++ seemed so much simpler

tell me about it. I hate the whole thing of system.out.print. Also there is nothing like cin. One has to do buffer reader and datainput stream. Too bulky for me when entering simple things for debugging.

Schuler
01-27-2004, 09:03 PM
Originally posted by I.am+27 January 2004 - 06:44--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (I.am @ 27 January 2004 - 06:44)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-Schuler@26 January 2004 - 22:38
need to change that 9 to a 10.
Nah it seems right. It will create a String with 10 characters (0-9) [/b][/quote]
No it will create an array with indexes from 0-8 making 9 values.

I.am
01-27-2004, 09:50 PM
Originally posted by Schuler+27 January 2004 - 14:03--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (Schuler @ 27 January 2004 - 14:03)</td></tr><tr><td id='QUOTE'>
Originally posted by I.am@27 January 2004 - 06:44
<!--QuoteBegin-Schuler@26 January 2004 - 22:38
need to change that 9 to a 10.
Nah it seems right. It will create a String with 10 characters (0-9)
No it will create an array with indexes from 0-8 making 9 values. [/b][/quote]
you are right again :frusty: Next time remind me to spend more than 10 secs while thinking about something.