• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

bubble sort for string trouble  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble with a java bubble sort, I want to get the output showing the unsorted list how it is before it also shows me the sorted list in the output, but I'm only able to show the sorted list in ascending order. Also how do i add in the output printout text showing the user text? i can't find out how to do this in a bubble sort program. I looked for hours to find a solution but there's a lot only showing with int bubble sort but very little on string bubble sort, and if there are any examples, they don't explain how it works, also I would like to add prices beside the items, but idk how to, can someone spoon feed me the answer and also explain it to me how it works, here is my code so far

 
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this should be
I would make your sort into its own method, then you could call it from main().
 
lowercase baba
Posts: 12737
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't put all your code in a single method.  You want to separate responsibility.  For example, you could create a method that is passed an array, and then it prints all the elements.  Once you have that, you can then use it like a lego block to build a bigger program.  If you also create a method that sorts the array, then your main method becomes very simple:

print_array
sort_array
print_array

aside: one could argue you are not printing the sorted array.  You are printing out an element of the array as you work through sorting it
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:You shouldn't put all your code in a single method.  You want to separate responsibility.  For example, you could create a method that is passed an array, and then it prints all the elements.  Once you have that, you can then use it like a lego block to build a bigger program.  If you also create a method that sorts the array, then your main method becomes very simple:

print_array
sort_array
print_array

aside: one could argue you are not printing the sorted array.  You are printing out an element of the array as you work through sorting it





If i knew how to do that I would haven't asked for help, that's why I'm asking for someone to show me the code and how it's done, i need to be spoon fed
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I believe this should be
I would make your sort into its own method, then you could call it from main().




how do i make them into my own method? i need see some coding on how it works
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Belford wrote:If i knew how to do that I would haven't asked for help, that's why I'm asking for someone to show me the code and how it's done, i need to be spoon fed

What are you stuck on?
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Jordan Belford wrote:If i knew how to do that I would haven't asked for help, that's why I'm asking for someone to show me the code and how it's done, i need to be spoon fed

What are you stuck on?



when I run the program I want to be shown the list how it currently is and turn it into alphabetical ascending order, when I run it I only get the alphabetical ascending order list, but i want to be able to also see the order it was originally in,in the output i get when i  java run the program, but i don't know how to do it, also once I finish this problem, i would also like to add some prices to the items listed without affecting the alphabetical ascending order of the words.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Belford wrote:when I run the program I want to be shown the list how it currently is and turn it into alphabetical ascending order, when I run it I only get the alphabetical ascending order list, but i want to be able to also see the order it was originally in,in the output i get when i  java run the program, but i don't know how to do it, also once I finish this problem, i would also like to add some prices to the items listed without affecting the alphabetical ascending order of the words.

I've already given you the pieces, now put them together.
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put the pieces together but it doesn't work, what did you do to my code??
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Belford wrote:I put the pieces together but it doesn't work, what did you do to my code??

Funny, it works for me in Eclipse. Did you add the appropriate imports?

Please cut and paste any errors you get into a post.
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Jordan Belford wrote:I put the pieces together but it doesn't work, what did you do to my code??

Funny, it works for me in Eclipse. Did you add the appropriate imports?

Please cut and paste any errors you get into a post.



No, I'm using notepad ++, I'm required to use Notepad++
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Belford wrote:

Carey Brown wrote:

Jordan Belford wrote:I put the pieces together but it doesn't work, what did you do to my code??

Funny, it works for me in Eclipse. Did you add the appropriate imports?

Please cut and paste any errors you get into a post.



No, I'm using notepad ++, I'm required to use Notepad++


You didn't post any errors, does that mean it runs ok?
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Jordan Belford wrote:I put the pieces together but it doesn't work, what did you do to my code??

Funny, it works for me in Eclipse. Did you add the appropriate imports?

Please cut and paste any errors you get into a post.



These are the errors that I'm getting:


 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errors also need to be enclosed in Code tags.

See what the carets (^) are pointing to? See where it says what symbol it can't find? Both of these refer to "Arrays". And because class names begin with an upper case letter, we can deduce that we probably need to import "Arrays" (you'll have to import the fully qualified name, i.e. java.util.Arrays).
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, if you don't like using  "Arrays", you can create your own "print( String[] arr )" method and loop through the array.
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Alternatively, if you don't like using  "Arrays", you can create your own "print( String[] arr )" method and loop through the array.




it worked thanks,
this is the results I got:

[Bed-Camera, Coffee-Table, PC, Smartphone, Smartphone-Case, Smartphone-Charger, Shelf, Tablet, Action-Figure]
[Action-Figure, Bed-Camera, Coffee-Table, PC, Shelf, Smartphone, Smartphone-Case, Smartphone-Charger, Tablet]

But i would like if they came out like this:

Unsorted items list:

Bed
Camera
Coffee-Table
PC
Smartphone
Smartphone-Case
Smartphone-Charger
Shelf
Tablet
Action-Figure

Sorted items list:

Action-Figure
Bed
Camera
Coffee-Table
PC, Shelf
Smartphone
Smartphone-Case
Smartphone-Charger
Tablet


also how do i add some price numbers also like this:

unsorted items list:

Bed 200 dollars
Camera 500 dollars
Coffee-Table 150 dollars
PC 600 dollars
Smartphone 600 dollars
Smartphone-Case 50 dollars
Smartphone-Charger 10 dollars
Shelf  150 dollars
Tablet 650 dollars
Action-Figure 100 dollars


Sorted items list:

Action-Figure  100 dollars
Bed 150 dollars
Camera 500 dollars
Coffee-Table 150 dollars
PC 600 dollars
Shelf 150 dollars
Smartphone 600 dollars
Smartphone-Case 50 dollars
Smartphone-Charger 10 dollars
Tablet 650 dollars

So this is what i want to see in as the output, every thing below this text:

unsorted items list:

Bed 200 dollars
Camera 500 dollars
Coffee-Table 150 dollars
PC 600 dollars
Smartphone 600 dollars
Smartphone-Case 50 dollars
Smartphone-Charger 10 dollars
Shelf  150 dollars
Tablet 650 dollars
Action-Figure 100 dollars


Sorted items list:

Action-Figure  100 dollars
Bed 150 dollars
Camera 500 dollars
Coffee-Table 150 dollars
PC 600 dollars
Shelf 150 dollars
Smartphone 600 dollars
Smartphone-Case 50 dollars
Smartphone-Charger 10 dollars
Tablet 650 dollars


So what do I have to do to get the same above format and prices together?? thanks for the help so far btw

also this is how my code looks like right now



 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want the standard array print formatting then you'll have to write your own print() method.

If you wan to print a dollar amount, now you're into new territory. You have two basic choices:
1) You can create a parallel array "dollarArr" with the dollar amounts and then modify the swap portion of your sort to swap the members of arr and dollarArr in parallel.
2) You can create a very simple class, e.g. Item, that has a String and an int (or double) as fields and then have an array of Items. Sort will need to be modified to sort on an Item field.

1) Maybe easier for you at your point in your learning curve but it is NOT object oriented programming and can get messy to debug.
2) This is the object oriented way but you will probably need to google tutorials on how to create Java classes.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you working from a book? Do you have any reference materials? Do you know how to get to Javadocs for the Java API online?
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!