Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Praktische Informatik (http://www.informatikerboard.de/board/board.php?boardid=6)
--- Strings vergleichen (http://www.informatikerboard.de/board/thread.php?threadid=2003)


Geschrieben von gg_ am 14.12.2014 um 15:59:

  Strings vergleichen

Meine Frage:
Ich hab ein Java-Programm entwickelt, das eine Liste von Agentennamen der Form "Agent 001", "Agent 002" usw. bis "Agent 009" in einer for-Schleife durchläuft.
Als Erweiterung habe ich an den Namen jedes 4. Agenten den Zusatz "enttarnt" an, also "Agent 004 enttarnt", "Agent 008 enttarnt", "Agent 012 enttarnt", usw. hinzugefügt. die zweite Erweiterung ist, sobald der String "Agent 007" geschrieben wird, wird der Zusatz "James Bond" mit ausgegeben, das habe ich irgendwie nicht geschafft, ich weiß das ich die Methode equals benutzen muss.

Meine Ideen:
Meine Lösung:

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
public static void main(String[] args) {


	String pattern="000";  //um Agent001 einzustellen
        DecimalFormat form = new DecimalFormat(pattern);
        
        for(int i= 1; i<1000; i++)
	{
		System.out.println();
		System.out.print("Agent"+form.format(i));
			
		if(i%4 == 0)
		{
		      System.out.print("\t entarnt");
		}
			
	
   }	
        
        
}





Danke im vorraus



Geschrieben von gg_ am 14.12.2014 um 16:04:

 

ich habe die Lösung

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:

if("Agent007".equals("Agent"+form.format(i)))
			{
				System.out.print("\t James Bond");
			}





Geschrieben von gg_ am 14.12.2014 um 16:14:

 

Meine komplette Lösung:

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:

public class agent {
	
	public static void main(String[] args) {

	String pattern="000";  //um Agent001 einzustellen
        DecimalFormat form = new DecimalFormat(pattern);
        
        for(int i= 1; i<1000; i++)
		{
			System.out.println();
			System.out.print("Agent"+form.format(i));
			
			if("Agent007".equals("Agent"+form.format(i)))
			{
				System.out.print("\t James Bond");
			}
			
			if(i%4 == 0)
			{
				System.out.print("\t entarnt");
			}
			
	
		}	
        
        
}
}





Forensoftware: Burning Board, entwickelt von WoltLab GmbH