+ 1

Help!

Hi, i'm sorry if that's not the right place to answer this kind of question. I have a problem with my program (a simple one but i'm just starting to code) and i don't know how to resolve it. Is there some forum to post it or does anybody help ? Sorry again for my bad english, that's french stuff ;) Thanks!

19th Nov 2016, 9:57 AM
Eehils
Eehils - avatar
9 Réponses
+ 3
yeah post your code here along with the problem you faced and you'll surely get help :)
19th Nov 2016, 10:26 AM
Sunera Avinash
Sunera Avinash - avatar
+ 1
hey im guessing that Lire is some sort of Scanner object to read user input and Lire.i is to input numbers and Lire.S is for strings why didn't you use the standard scanner object or something add Scanner in=new Scanner(System.in); in the beginning and use in.nestInt() and in.next() instead of Lire and see if it works anyway what did you try to do in for(i=0;i<n;i++){ if(lstNb[i]<lstNb[i+1]){ res=lstNb[i]; lstNb[i]=lstNb[i+1]; lstNb[i+1]=res; } } is it used to sort?
19th Nov 2016, 12:27 PM
Sunera Avinash
Sunera Avinash - avatar
+ 1
I tested your code and there were few bugs anyway if you tried to get a list, sort it and print it then this is the fixed code(main mathod) ps-i assumed you tried to bubble sort pps-sorry if what I typed is not what you intended... I don't know French :P :) output-> How many items do you want in list. N=? 5 What is the 1th element ? 5 What is the 2th element ? 7 What is the 3th element ? 1 What is the 4th element ? 4 What is the 5th element ? 3 Sort : yes or no? yes Your list: Case 1 : 1 Case 2 : 3 Case 3 : 4 Case 4 : 5 Case 5 : 7 //----------------- public static void main (String[] args) throws java.lang.Exception { Scanner in =new Scanner(System.in); int i,n,k,res; String c; String o= "yes"; System.out.println("How many items do you want in list. N=?"); n=in.nextInt(); int[] lstNb = new int[n]; for(i=0;i<n; i++){ System.out.println("What is the "+(i+1)+"th element ?"); k=in.nextInt(); lstNb[i]=k; } System.out.println("Sort : yes or no?"); c=in.next(); if(o.equals(c)){ for(int j=0;j<n-1;j++){ for(i=0;i<n-1;i++){ if(lstNb[i]>lstNb[i+1]){ res=lstNb[i]; lstNb[i]=lstNb[i+1]; lstNb[i+1]=res; } } } System.out.println("Your list: "); for(i=0;i<n;i++){ System.out.println("Case "+(i+1)+" : "+ lstNb[i]); } } else{ System.out.println("Your cases were saved !"); } } }
19th Nov 2016, 12:55 PM
Sunera Avinash
Sunera Avinash - avatar
+ 1
Yeah, thanks, that's what i want ! :) ps- What's the difference between "a bubble sort" and an other one ?
19th Nov 2016, 1:46 PM
Eehils
Eehils - avatar
+ 1
well there are few sorting mechanisms and bubble sort is one of them it isnt much efficient actually I don't know much about sorting algorithms if you want to know more visit https://en.m.wikipedia.org/wiki/Sorting_algorithm
19th Nov 2016, 2:14 PM
Sunera Avinash
Sunera Avinash - avatar
+ 1
for the question about Scanner yes you can define it like a variable Sololearn course contained tutorials about it check them out yes in.nestInt() is for a integer in.next() is for a whole line
19th Nov 2016, 2:19 PM
Sunera Avinash
Sunera Avinash - avatar
+ 1
Alright! Thanks a lot for all your answers, and have a good journey! :)
19th Nov 2016, 3:14 PM
Eehils
Eehils - avatar
0
thanks @Sunera package arraytest; import java.lang.String; public class Liste_Amovible{ public static void main (String[] args) { int i,n,k,res; String c; String o= "oui"; System.out.println("Entrez le nombre d'éléments dans la liste. N = ?"); n=Lire.i(); int[] lstNb = new int[n]; for(i=0;i<n; i++){ System.out.println("Quel entier attribuer à la "+(i+1)+" ème case ?"); k=Lire.i(); lstNb[i]=k; } System.out.println("Ranger les nombres par ordre croissant : oui ou non ?"); c=Lire.S(); if(o.equals(c)){ for(i=0;i<n;i++){ if(lstNb[i]<lstNb[i+1]){ res=lstNb[i]; lstNb[i]=lstNb[i+1]; lstNb[i+1]=res; } } System.out.println("Voici la nouvelle liste :"); for(i=0;i<n;i++){ System.out.println("Case "+(i+1)+" : "+ lstNb[i]); } } else{ System.out.println("L'ordre des cases est conservé !"); } } } So I don't understand why "if(o.equals(c))" doesn't work (it's where the programm fail). I have NeatBeans as environment.
19th Nov 2016, 11:55 AM
Eehils
Eehils - avatar
0
Exactly, the program creat an array and can sort the cases in ascending order if the user wishs it Moreover, i'm not sure to inderstand: Do I add "Scanner in=new Scanner(System.in);" here: public static void main (String[] args) { int i,n,k,res; String c; String o= "oui"; Scanner in=new Scanner(System.in); ? and ist "in.nestlnt()" used to read user input (for numbers) and "in.next()" the same thing but for String ?
19th Nov 2016, 1:06 PM
Eehils
Eehils - avatar