English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

public class InOrder{
public static void main(String [] args){
String word1, word2, word3, word1Upper, word2Upper, word3Upper;
do{
word1=IO.getString("Enter first String");
word2=IO.getString("Enter second String");
word3=IO.getString("Enter the third word");
word1Upper=word1.toUpperCase();
word2Upper=word2.toUpperCase();
word3Upper=word3.toUpperCase();
if( word1Upper.compareTo(word2Upper) < 0){
if(word1Upper.compareTo(word3Upper) < 0){
System.out.println(word1Upper+", "+word2Upper+", "+word3Upper);
}else{
System.out.println(word1Upper+", "+word3Upper+", "+ word2Upper);
}
}else if(word2.compareTo(word1Upper) < 0){
if(word2.compareTo(word3Upper) < 0){
System.out.println(word2Upper+", "+word1Upper+", "+word3Upper);
}else{
System.out.println(word2Upper+", "+word3Upper+", "+word1Upper);
}
}else{

2007-12-20 03:17:17 · 3 answers · asked by kjb281 1 in Computers & Internet Programming & Design

System.out.println(word3Upper+", "+word2Upper+", "+word1Upper);
}
}
}while(Doagain.ask());
}
}

its supposed to be like this
if the input were: Hello, goodbye, CIAO
The output would be :CIAO GOODBYE HELLO

how can i fix it to do that?

2007-12-20 03:19:30 · update #1

3 answers

Couple problems. You do not define "doAgain" and also your if logic seems to have holes. I would rewrite as follows:

public class InOrder{
public static void main(String [] args){
String word1, word2, word3, word1Upper, word2Upper, word3Upper;

boolean stopLoop = false;

do{
System.out.println("Type END to exit");
System.out.println();
word1=IO.getString("Enter first String");
if (word1.equalsIgnoreCase("END")) {
stopLoop = true;
} else {
word2=IO.getString("Enter second String");
word3=IO.getString("Enter the third word");
word1Upper=word1.toUpperCase();
word2Upper=word2.toUpperCase();
word3Upper=word3.toUpperCase();

if(word1Upper.compareTo(word2Upper) > 0){
String temp = word1Upper;
word1Upper = word2Upper;
word2Upper = temp;
}

if(word1Upper.compareTo(word3Upper) > 0){
String temp = word1Upper;
word1Upper = word3Upper;
word3Upper = temp;
}

if(word2Upper.compareTo(word3Upper) > 0){
String temp = word2Upper;
word2Upper = word3Upper;
word3Upper = temp;
}

System.out.println(word1Upper+", "+word2Upper+", "+word3Upper);
}
} while(stopLoop == false);
}
}


Where I imagine your IO class looks like:

class IO {
public static String getString(String prompt) {
String response = null;

System.out.print(prompt + ": ");

try {
BufferedReader buffy = new BufferedReader(new InputStreamReader(System.in));
response = buffy.readLine();
} catch (IOException e) {}

return response;
}
}

2007-12-20 03:56:04 · answer #1 · answered by Jake 3 · 0 0

i think u want to arrange the words as they appear in the dictionary. There is a logical mistake in ur program.

use this logic:
if word1 _{
___if word2 ____o/p : word1,word2,word3
___else if word1 ____o/p : word1,word3,word2
___else o/p: word3, word1, word2
_}
else
_{
___if word1 ____o/p : word2,word1,word3
___else if word2 ____o/p : word2,word3,word1
___else o/p: word3, word2, word1
_}

2007-12-20 03:51:11 · answer #2 · answered by ashphim 2 · 0 1

Why not something like this:


String[] s = new String[100]; // default values: null
String t1, t2;
int n = 0;
int bDone = 0;

do {
s[n] = IO.getString("Enter string number " + n);
n = n+ 1;
} while ((n < s.length()) && ( s[s.length() - 1] != ""))

sortArray(s);

n = 0;
System.out.println("Sorted array");
do {
System.out.println(s[n]);
n = n + 1;
} while (n < s.length())

NOTE: The sortArray() function would have to be written by you if it isn't available already (I know that long arrays can use the sort() function).

2007-12-20 03:48:24 · answer #3 · answered by Chris C 7 · 0 2

fedest.com, questions and answers