Java :: Aufgabe #28

7 Lösungen Lösungen öffentlich

Text abwechselnd in Groß- und Kleinschreibung

Anfänger - Java von Dome - 29.12.2012 um 01:34 Uhr
Schreiben Sie ein Programm, welches einen eingegeben Text so manipuliert, das der Text abwechselnd in Groß- und Kleinschreibung auf den Bildschirm ausgegeben wird.

Konsolenausgabe:

Texteingabe: Beispieltext
Textausgabe: BeIsPiElTeXt

Lösungen:

vote_ok
von 0 (0 Punkte) - 10.08.2013 um 15:26 Uhr
Main.java
Quellcode ausblenden Java-Code
package de.trainyourprogrammer.java28;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Print a requested word in upper and lower case letters alternating.
 * 
 * @author jsb
 */
public class Main {

	/**
	 * Execute the program.
	 * 
	 * @param args
	 *            Is ignored in our case.
	 */
	public static void main(String[] args) {
		BufferedReader buffer = new BufferedReader(new InputStreamReader(
				System.in)); // start reading from the default input

		System.out.print("Texteingabe: "); // request a word

		try { // try to...
			String word = buffer.readLine(); // read word

			System.out.print("Textausgabe: ");

			for (int i = 0; i < word.length(); i++) { // for each character in
														// the word...
				if (i % 2 == 0) { // ...print the even chars...
					System.out.print(word.toUpperCase().charAt(i)); // upper
																	// case
				} else { // print the odd chars...
					System.out.print(word.toLowerCase().charAt(i)); // lower
																	// case
				}
			}
		} catch (IOException e) { // ignore occurring IOExceptions
		} finally { // on quit...
			try { // try to...
				buffer.close(); // stop reading the input
			} catch (IOException e) { // again ignore occurringIOExceptions
			}
		}
	}
}
vote_ok
von ElPapito (2690 Punkte) - 08.05.2015 um 19:38 Uhr
Quellcode ausblenden Java-Code

/**
 * @author ElPapito
 * @date 08.05.2015
 */

import java.util.Scanner;

public class TextAbwechselndInGrossUndKleinschreibung {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Texteingabe: ");
		String input = scanner.nextLine();
		scanner.close();

		System.out.print("Textausgabe: ");
		for (int i = 0; i < input.length(); i++) {
			if (i % 2 == 0) {
				System.out.print(input.toUpperCase().charAt(i));
			} else {
				System.out.print(input.toLowerCase().charAt(i));
			}
		}
	}

}

vote_ok
von Bufkin (1410 Punkte) - 22.08.2017 um 14:22 Uhr
Quellcode ausblenden Java-Code
class grossKlein
{
    public static void umwandeln(String eingabe) {
        String ausgabe = " ";
        int zaehler = 0;
        char aktBuchstabe = ' ';
        String stringBuchstabe = " ";
        
        for(int i = 0; i < eingabe.length(); i++) 
        {
            aktBuchstabe = eingabe.charAt(i);
            stringBuchstabe = Character.toString(aktBuchstabe);
            
            if(zaehler % 2 == 0) 
            {
                ausgabe = ausgabe.concat(stringBuchstabe.toUpperCase());
            } else {
                ausgabe = ausgabe.concat(stringBuchstabe.toLowerCase());
            }
            zaehler++;
        }
        System.out.println(ausgabe);
    }
    
    
    public static void main (String[] args) throws java.lang.Exception
    {
        umwandeln("Beispieltext");
    }
}
vote_ok
von nOrdan (1160 Punkte) - 05.06.2019 um 14:55 Uhr
Anmerkung: Ich arbeite mit dem Programm BlueJ

Quellcode ausblenden Java-Code


import Methodensammlung.Methoden;

/**
 * Ein Text wird so manipuliert das er abwechselnd Groß- und Kleinbuchstaben enthält.
 * 
 * @author (nOrdan) 
 * @version (05.06.2019)
 */
public class Umwandler
{
    
    Methoden m = new Methoden();

    public static void main(String [] args)
    {
        Umwandler u = new Umwandler();
        u.inputs();
    }

    private void inputs()
    {
        boolean valid1 = false;
        String input1 = "";
        while (valid1 == false)
        {
            input1 = m.userInput("Geben sie den Text ein, den sie manipulieren wollen");
            m.errorStringInput(input1);
            valid1 = true;
        }
        output(input1);
    }
    
    private void output(String input)
    {
        System.out.print("Der manipulierte Text ist: ");
        for (int i = 0; i < input.length(); i++)
        {
            if (i % 2 == 0)
            {
                System.out.print(input.toUpperCase().charAt(i));
            }
            else
            {
                System.out.print(input.toLowerCase().charAt(i));
            }
        }
    }
}




Die Methoden, welche ich aus meinem eigenem Methodensammlung package benutzt habe

Quellcode ausblenden Java-Code

 public String userInput(String message)
    {
        return JOptionPane.showInputDialog(message);
    }

 public void errorStringInput(String input)
    {
        if (input == null) 
        {
            System.exit(0); //Drückt der User auf abbrechen wird null zurück gegeben und das Programm wird beendet
        }
        else if (input.isEmpty() == true)
        {
            informationMessage("Da sie nichts eingegeben haben wird auch nichts ausgegeben","No input");
            System.exit(0);
        }
    }

public void konsoleLeeren()
    {
        System.out.print('\u000C');
    }


vote_ok
von paddlboot (3970 Punkte) - 09.07.2019 um 09:31 Uhr
Quellcode ausblenden Java-Code
import java.util.*;

public class GroßKlein {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String eingabe;
		
		System.out.print("Texteingabe:\t");
		eingabe = scanner.nextLine();
		
		System.out.print("Textausgabe:\t");

		for(int i = 0; i < eingabe.length(); i++) {
			if(i % 2 == 0) System.out.print(eingabe.toUpperCase().charAt(i));
			else System.out.print(eingabe.toLowerCase().charAt(i));
		}
		
		scanner.close();
	}
}
vote_ok
von kollar (340 Punkte) - 10.12.2019 um 10:23 Uhr
Quellcode ausblenden Java-Code
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

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

		FrGrossKleinBuchstaben frGrossKleinBuchstaben = new FrGrossKleinBuchstaben(
				"Text abwechselnd in Groß- und Kleinschreibung");
	}
}

class FrGrossKleinBuchstaben extends JFrame implements ActionListener {
	JLabel lblEingabe = new JLabel("Texteingabe:");
	JTextField tfEingabe = new JTextField(20);
	JLabel lblAusgabe = new JLabel("Textausgabe:");
	JTextField tfAusgabe = new JTextField(20);
	JPanel pEingabe = new JPanel();
	JPanel pAusgabe = new JPanel();

	FrGrossKleinBuchstaben(String titel) {
		super(titel);
		setVisible(true);
		setLocation(600, 400);
		setSize(380, 240);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout(FlowLayout.RIGHT, 30, 30));

		tfAusgabe.setEditable(false);
		tfEingabe.addActionListener(this);

		pEingabe.add(lblEingabe);
		pEingabe.add(tfEingabe);
		pAusgabe.add(lblAusgabe);
		pAusgabe.add(tfAusgabe);

		add(pEingabe);
		add(pAusgabe);
	}

	public String abwechselnd() {
		String stAusgabe = "";
		String stEingabe = tfEingabe.getText();

		for (int i = 0; i < stEingabe.length(); i++) {
			if (i % 2 == 0) {
				stAusgabe = stAusgabe + Character.toUpperCase(stEingabe.charAt(i));
			} else {
				stAusgabe = stAusgabe + Character.toLowerCase(stEingabe.charAt(i));
			}
		}

		return stAusgabe;
	}

	@Override
	public void actionPerformed(ActionEvent evt) {

		tfAusgabe.setText(abwechselnd());

	}

}
vote_ok
von 0 (0 Punkte) - 11.04.2021 um 12:06 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;

public class TextAbwechselndInGroßUndKleinschreibung {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Texteingabe: ");
        String text = scanner.nextLine().toLowerCase();

        char[] chars = new char[]{};
        chars = text.toCharArray();

        for (int i = 0; i < chars.length; i++) {

            if ((i % 2) == 0) {
                chars[i] = Character.toUpperCase(chars[i]);
            } else {
                chars[i] = Character.toLowerCase(chars[i]);
            }

        }

        text = String.copyValueOf(chars);

        System.out.println("Textausgabe: " + text);

    }

}