Java changing upper or lower case by entering CAPS - java

i want to write a program which at first it gets an integer number which is the number of strings of characters that are supposed to be entered including CAPS and i want that after each CAPS that was entered the rest of characters change in upper or lower case (the default is lower case at first) and finally the program outputs the final string. here is an example:
input:
8
s
f
k
CAPS
h
j
CAPS
p
output:
sfkHJp
here is what i tried so far first i wanted to check out whether this algorithm will work out or not but it actually did not because it does not print the result at all !
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int CAPScounter = 0;
String result = "";
String temp = "";
String singles = "";
boolean CAPSval = false;
if (CAPSval) {
singles = "CAPS";
}
Scanner input = new Scanner(System.in);
int n = input.nextInt();
// System.out.print(n);
while (true) {
if (result.length() == n - CAPScounter) {
break;
}
//temp += input.next();
singles = input.next();
if (CAPSval) {
CAPScounter += 1;
result += temp;
temp = "";
}
}
System.out.print(result);
}
}

This could be a simple solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String result = "";
String singles = "";
boolean isCAPSEnabled = false;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while (n-- > 0) {
singles = input.next();
if (singles.equals("CAPS")) {
isCAPSEnabled = !isCAPSEnabled; // reverse
} else {
result += isCAPSEnabled ? singles.toUpperCase() : singles.toLowerCase();
}
}
System.out.print(result);
}
}

you can set limit for while
CAPSval if first button second it value equal false or true
if CAPSval == true single change to Upper else toLower
if n == 0 while end and output result
Sorry I update solution you nofify first time button caps and If not button caps you enter upper or lower character then output same with enter
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int CAPScounter = 0;
String result = "";
String singles = "";
boolean CAPSval = false, fistButton = false;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while (n-- > 0) {
singles = input.next();
if (singles.equals("CAPS")) {
fistButton = true;
CAPSval = !CAPSval;
CAPScounter += 1;
} else {
if (fistButton) {
if (CAPSval)
singles = singles.toUpperCase();
else singles = singles.toLowerCase();
}
result += singles;
}
}
System.out.print(result);
}
}

Related

How to find a number with no digit occurring more than once (J3 CCC 2013 from 1987 to 2013)

I'm a grade 11 student and have been given the task to complete this question for homework:
Problem J3/S1: From 1987 to 2013
You might be surprised to know that 2013 is the first year since 1987
with distinct digits.
The years 2014, 2015, 2016, 2017, 2018, 2019
each have distinct digits.
2012 does not have distinct digits,
since the digit 2 is repeated.
Given a year, what is the next year with distinct digits?
Input
The input consists of one integer Y (0 ≤ Y ≤ 10000),
representing the starting year.
Output
The output will be the single integer D,
which is the next year after Y with distinct digits.
Sample Input 1
1987
Sample Output 1
2013
Sample Input 2
999
Sample Output 2
1023
I usually answer these types of questions rather quickly but I am stumped when it comes to this one. I have spent several hours and cannot figure it out. I found out How to identify if a number is distinct or not, but I can't figure out how to add on years and check again, I keep getting errors. I would really appreciate someone's help.
Please keep in mind that I am in grade 11 and this is my first year of working with Java, so please do not use advanced coding, and methods because I won't understand. If you can, please answer it in a class and not the main method.
here is what I tried:
import java.util.*;
import java.io.*;
public class Leavemealone
{
public static void main(String[] args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in));
int ctr = 0;
String inputStr = "";
int input = 0;
int inputCheck = 0;
System.out.println("Enter somthin: ");
input = Integer.parseInt (objReader.readLine ());
while(ctr == 0)
{
inputStr += input;
Scanner sc = new Scanner(inputStr);
int n = sc.nextInt(); // get year
String s = String.valueOf(n);
int[] num = new int[4];
for (int i = 0; i < s.length(); i++)
{
int x = Integer.parseInt(s.substring(i, i + 1)); // integer at this part in the string
num[i] += x;
}
String apple = (num[0] + "" + num[1] + "" + num[2] + "" + num[3]);
if (num[0] != num[1] &&
num[1] != num[2] &&
num[2] != num[3] &&
num[0] != num[2] &&
num[0] != num[3] &&
num[1] != num[3])
{
ctr++;
//distinct
}
else
{
input++;
//not distinct
}
}
}
}
Thanks in advance!
this is the other code I found online, I just don't know how to put it in a class
import java.util.Scanner;
import java.io.*;
public class Thegoodone
{
public static void main(String[] args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
int ctr = 0;
String input = "";
int inputCheck = 0;
while (ctr == 0)
{
System.out.println("Enter somthin: ");
inputCheck = Integer.parseInt (objReader.readLine ());
if (inputCheck > 0 && inputCheck < 10000)
{
input += inputCheck;
ctr += 1;
}
else
{
System.out.println("invalid input ");
}
}
Scanner sc = new Scanner(input);
int n = sc.nextInt(); // get year
n++; // start from the next year
while (!hasDistinctDidgets(n)) //if there is repeating digits
{
n++;// next year
}
System.out.println(n);// prints year
}
public static boolean hasDistinctDidgets(int n)
{
//System.out.println("a" + n);
String s = String.valueOf(n); // converts the year from int to String
int[] numbers = new int[10]; // index position represents number, element value represents occurrence of that number
for (int i = 0; i < s.length(); i++)
{
int x = Integer.parseInt(s.substring(i, i + 1)); // integer at this part in the string
numbers[x]++; //increase occurrence of this integer in the array
}
//check if any digit occurred more than once in the array
for (int i = 0; i < numbers.length; i ++)
{
if (numbers[i] > 1) //digit occurred more than once
{
return false; //not distinct
}
}
return true; // hasn't returned false yet, so the integer has distinct digits
}
}
so this is how I tried to put it in a class:
import java.util.Scanner;
import java.io.*;
public class Danny3
{
public static void main(String[] args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
int ctr = 0;
String input = "";
int inputCheck = 0;
while (ctr == 0)
{
System.out.println("Enter somthin: ");
inputCheck = Integer.parseInt (objReader.readLine ());
if (inputCheck > 0 && inputCheck < 10000)
{
input += inputCheck;
ctr += 1;
}
else
{
System.out.println("invalid input ");
}
}
Scanner sc = new Scanner(input);
// System.out.println(output);
int n = sc.nextInt(); // get year
n++; // start from the next year
DistinctCheck processing = new DistinctCheck(n);
int output = processing.getSum();
System.out.println(output);
}
}
class DistinctCheck
{
//private int year = 0;
private boolean hasDistinctDidgets;
private int b = 0;
DistinctCheck(int temp)
{
hasDistinctDidgets(temp);
}
private void yearAdd(int b)
{
while(!hasDistinctDidgets(b)) //if there is repeating digits
{
b++;// next year
}
}
private boolean hasDistinctDidgets(int year)
{
String s = String.valueOf(year); // converts the year from int to String
int[] numbers = new int[10]; // index position represents number, element value represents occurrence of that number
for (int i = 0; i < s.length(); i++)
{
int x = Integer.parseInt(s.substring(i, i + 1)); // integer at this part in the string
numbers[x]++; //increase occurrence of this integer in the array
}
//check if any digit occurred more than once in the array
for (int i = 0; i < numbers.length; i ++)
{
if (numbers[i] > 1) //digit occurred more than once
{
return false; //not distinct
}
}
return true; // hasn't returned false yet, so the integer has distinct digits
}
int getSum()
{
return b;// prints year
}
}
I would start with a method to determine if a given int consists of distinct digits. You could use a Set<Character> and add each character from the String to the Set. You will get false on a duplicate. Like,
static boolean distinctDigits(int i) {
String s = String.valueOf(i);
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray()) {
if (!set.add(c)) {
return false;
}
}
return true;
}
Then your main just needs to invoke that. Like,
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int v = s.nextInt();
while (v < 10000) {
v++;
if (distinctDigits(v)) {
break;
}
}
System.out.println(v);
}
i figured it out:
import java.util.*;
public class Apple
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int num = input.nextInt();
Distinct findDistinct = new Distinct(num); // objecct
String output = findDistinct.getDistinctYear();
System.out.println(output);
}
} // end of main
class Distinct
{
private int ctr = 0;
private String yearStr = "";
private String distinctYear = "";
private int year = 0;
Distinct(int n)
{
year = n;
makeDistinct();
}
private void makeDistinct()
{
while(ctr == 0)
{
year += 1; // year will keep increasing until it is distinct
yearStr = Integer.toString(year);
if(isDistinct(yearStr) == true) // if the number is distinct
{
distinctYear = yearStr;
ctr++;
}
}
}
private boolean isDistinct(String yearStr)
{
String eachNum[] = yearStr.split(""); // breaks up each number (char) of yearStr
for(int i = 0; i < eachNum.length; i++)
{
for(int j = 0; j < i; j++)
{
if (eachNum[i].equals(eachNum[j])) // not distinct
{
return false;
}
}
}
return true; // is distinct
}
String getDistinctYear()
{
return distinctYear;
}
}

To Reduce a String

I was solving a problem to reduce the form to it's non-reducible form. This was the question.
Shil has a string S , consisting of N lowercase English letters. In one operation, he can delete any pair of adjacent letters with same value. For example, string "aabcc" would become either "aab" or "bcc" after operation.
Shil wants to reduce S as much as possible. To do this, he will repeat the above operation as many times as it can be performed. Help Shil out by finding and printing 's non-reducible form!
If the final string is empty, print Empty String; otherwise, print the final non-reducible string.
Sample Input 0
aaabccddd
Sample Output 0
abd
Sample Input 1
baab
Sample Output 1
Empty String
Sample Input 2
aa
Sample Output 2
Empty String
Explanation
Sample Case 0:
Shil can perform the following sequence of operations to get the final string:
Thus, we print .
Sample Case 1:
Shil can perform the following sequence of operations to get the final string:
aaabccddd -> abccddd
abccddd -> abddd
abddd -> abd
Thus we print abd
Sample case 1:
baab -> bb
bb -> Empty String.
And what I have done till now is try to solve it through StringBuilder in Java.But some of testcases pass while other's don't and I can't find out what's the error?
Here is the code that I have tried so far.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder(scan.nextLine());
for(int i = 0; i < sb.length()-1; i++)
{
if(sb.charAt(i) == sb.charAt(i+1))
sb.delete(i,i+2);
i = 0;
}
if(sb.length() == 0)
System.out.println("Empty String");
else
System.out.println(sb.toString());
}
}
Inputs like aaabccddd
and aa pass.But when the input is baab it fails.
You have to use a while loop. Problem with your code is that it just iterate through the code just one time. In first iteration though your input "baab" becomes "bb", then it checks 2nd b and try to find a "b" in i+1 (which does not exist). change your for loop to a while loop as below.
import java.util.Scanner;
public class Solution{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder(scan.nextLine());
int c=0;
while(c< sb.length()-1){
if(sb.charAt(c) == sb.charAt(c+1)){
sb.delete(c,c+2);
c=0;
}
else{
c+=1;
}
}
if(sb.length() == 0)
System.out.println("Empty String");
else
System.out.println(sb.toString());
}
}
The problem is you just run loop through the string for one time.
For example:
String "baab", you just delete "aa" and finish the loop.
Solution: use recursion with a flag isNonReducible, loop until it give empty string or flag isNonReducible = true;
public class Solution {
public static StringBuilder checkReducible(StringBuilder sb) {
boolean isNonReducible = true;
for (int i = 0; i < sb.length() - 1; i++) {
if (sb.charAt(i) == sb.charAt(i + 1)) {
isNonReducible = false;
sb.delete(i, i + 2);
}
}
if (sb.length() == 0) {
return new StringBuilder("Empty String");
}
else {
if(!isNonReducible) {
sb = checkReducible(sb);
}
return sb;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder(scan.nextLine());
System.out.println(checkReducible(sb));
scan.close();
}
}
you can do with the help of lable try this,
public static void main(String[] args) {
boolean canReduce = true;
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder(scan.nextLine());
startPoint: while (sb.length() > 0 && canReduce) {
for (int i = 0; i < sb.length() - 1; i++) {
if (sb.charAt(i) == sb.charAt(i + 1)) {
sb.delete(i, i + 2);
canReduce=true;
continue startPoint;
}else{
canReduce=false;
}
}
}
if (sb.length() == 0) {
System.out.println("Empty String");
} else {
System.out.println(sb.toString());
}
}
Try this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder sb =new StringBuilder(in.nextLine());
for (int i=0; i<sb.length()-1; i++){
if(sb.charAt(i)==sb.charAt(i+1)){
sb.delete(i, i+2);
i=-1;
}
}
if(sb.length()==0){
System.out.println("Empty String");
}else{
System.out.println(sb);
}
}

Simple Java TextEditor program not working

So for I was suppose to create a text-editor program where editing is done via a command which, in turn, is executed by the corresponding method. I was suppose to utilize this text-editor program with two files a) A novel .txt file such as tale of two cities and b) a file that contains a list of properly spelled words as for reference for the spellcheck methods
I was to utilized the following:
boolean Find (String x) // Looks for a word "x" in the file and returns true if found or false otherwise.
boolean FindReplace (String x, String y) // looks for the first occurrence of word "x" in the file and replaces it with word "y" if found returning true, false otherwise.
boolean FindInsert (String x, String y) // looks for the first occurrence of word "x" in the file and then insert "y" right after "x", if x is found, returning true, false otherwise.
boolean Delete (String x) // looks for the first occurrence of word "x" in the file and deletes it from the file, returning true if x is found, returning false otherwise.
String spellCheck () // finds the first occurrence of spelling error and returns the misspelled word. If no word is misspelled returns "Spell Check Passed".
void spellCheckAll() // find all misspelled words and output them to the screen.
void save() // saves file with the changes made.
void print() // saves file with the changes and outputs the contents of the file to the screen.
void quit() should save() the file and exit.
10. boolean FindReplaceAll (String x, String y) // looks for all occurrences of word "x" in the file and replace each with word "y" if found returning true, false otherwise.
This is the code I have so far. It compiles. However, when I try to object test any of methods they won't work. For example, my method to find the occurrence of a string within a .txt file will always return false I'm assuming there are some issues either a) my boolean method loops b) there's some issues with the creation of my linked list that I used for my file that's being read in and/or my hash map that I used for my dictionary/reference file. I'm stumped.
package FileEditor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
public class FileEditor {
static LinkedList<String> list = new LinkedList<String>();
public FileEditor() {
super();
}
public static void readNovelFile() {
String content = new String();
File file = new File("2city10.txt");
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNextLine()) {
content = sc.nextLine();
list.add(content);
}
sc.close();
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
}
public static boolean findText(String x) {
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
return true;
} else {
return false;
}
}
return false;
}
public static void findAndReplace(String x, String y) {
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
text = text.replaceAll(x, y);
list.remove(i);
list.add(i, text);
}
}
}
public static void findAndInsert(String x, String y) {
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
if (flag == false)
text = text.replace(x, x + " " + y);
list.remove(i);
list.add(i, text);
}
flag = true;
}
}
public static void delete(String x) {
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x) || text.equals(x)) {
if (flag == false)
text = text.replace(x, "");
list.remove(i);
list.add(i, text);
}
flag = true;
}
}
public static HashSet<String> readWords(String filename) throws FileNotFoundException {
HashSet<String> words = new HashSet<String>();
Scanner in = new Scanner(new File(filename));
// Use any characters other than a-z or A-Z as delimiters
in.useDelimiter("[^a-zA-Z]+");
while (in.hasNext()) {
words.add(in.next().toLowerCase());
}
return words;
}
public static void spellCheck() {
// Read the dictionary and the document
Set<String> dictionaryWords = null;
Set<String> documentWords = null;
boolean flag = false;
try {
dictionaryWords = readWords("EnglishWordList.txt");
documentWords = readWords("2city10.txt");
} catch (FileNotFoundException e) {
}
// Print all words that are in the document but not the dictionary
for (String word : documentWords) {
if (!dictionaryWords.contains(word) && flag == false) {
System.out.println(word);
flag = true;
}
}
}
public static void spellCheckAll() {
// Read the dictionary and the document
Set<String> dictionaryWords = null;
Set<String> documentWords = null;
try {
dictionaryWords = readWords("EnglishWordList.txt");
documentWords = readWords("2city10.txt");
} catch (FileNotFoundException e) {
}
// Print all words that are in the document but not the dictionary
for (String word : documentWords) {
if (!dictionaryWords.contains(word)) {
System.out.println("Misspelled words :" + word);
}
}
}
public static void saveFile() {
BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter("2city10.txt"));
for (int i = 0; i < list.size(); i++) {
out.write(list.get(i).toString());
out.write('\n'); // add a new line
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void printFile() {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
//out.write('\n'); // add a new line
}
}
public static void menuList() {
System.out.println("\n Enter the Choice ...");
System.out.println("\n Enter 1 to Find ");
System.out.println("\n Enter 2 to FindReplace ");
System.out.println("\n Enter 3 to FindInsert ");
System.out.println("\n Enter 4 to Delete ");
System.out.println("\n Enter 5 to spellCheck ");
System.out.println("\n Enter 6 to spellCheckAll ");
System.out.println("\n Enter 7 to save ");
System.out.println("\n Enter 8 to print ");
System.out.println("\n Enter 9 to quit ");
}
public static void main(String[] args) throws IOException {
readNovelFile();
int choice = 0;
menuList();
Scanner scanner = new Scanner(System.in);
choice = scanner.nextInt();
while (true) {
switch (choice) {
case 1:
{
String input = "";
System.out.println("\nEnter the string to Find ...");
Scanner textscan = new Scanner(System.in);
input = textscan.nextLine();
System.out.println("The String entered exists :" +
findText(input));
menuList();
choice = scanner.nextInt();
break;
}
case 2:
String find = "";
String replace = "";
System.out.println("\nEnter the string to Find ...");
Scanner findScan = new Scanner(System.in);
find = findScan.nextLine();
System.out.println("\nEnter the string to Replace ...");
Scanner replaceScan = new Scanner(System.in);
replace = replaceScan.nextLine();
findAndReplace(find, replace);
menuList();
choice = scanner.nextInt();
break;
case 3:
String findStr = "";
String insStr = "";
System.out.println("\nEnter the string to Find ...");
Scanner findStrScan = new Scanner(System.in);
findStr = findStrScan.nextLine();
System.out.println("\nEnter the string to Insert ...");
Scanner InsertStrScan = new Scanner(System.in);
insStr = InsertStrScan.nextLine();
findAndInsert(findStr, insStr);
menuList();
choice = scanner.nextInt();
break;
case 4:
String delete = "";
System.out.println("\nEnter the string to Delete ...");
Scanner deleteScan = new Scanner(System.in);
delete = deleteScan.nextLine();
delete(delete);
menuList();
choice = scanner.nextInt();
break;
case 5:
System.out.println("\nSpell checking for first occurence ....");
spellCheck();
menuList();
choice = scanner.nextInt();
break;
case 6:
System.out.println("\nSpell checking for All occurences ....");
spellCheckAll();
menuList();
choice = scanner.nextInt();
break;
case 7:
System.out.println("\nSaving the File ....");
saveFile();
menuList();
choice = scanner.nextInt();
break;
case 8:
System.out.println("\nSaving and Printing the File ....");
saveFile();
printFile();
menuList();
choice = scanner.nextInt();
break;
case 9:
System.out.println("\nEXIT MENU ....");
System.exit(0);
break;
}
}
}
}
For your findText always returning false....
public static boolean findText(String x) {
boolean found;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
if (text.contains(x)) { // You also dont need that "equals()" here
found=true;
break; // Break loop if text found
} else {
found=false;
}
}
return found;
}
But as mentioned in comments, better way is this:
public static boolean findText(String x) {
boolean found;
for (int i = 0; i < list.size(); i++) {
if(list.get(i).indexOf(x) >= 0){
found = true;
break;
}
else
found = false
}
return found;
}
Another way to do this without manually involving loops is:
public static boolean findText(String x) {
int index = Collections.binarySearch(list, x);
if(index >= 0)
return true;
return false;
}

Displaying multiple variables on single line?

So I'm doing a program which reads in a message up to 80 characters long and displays it back to the user using the push, pop, and isempty methods. The only problem is, a single variable will be printed on the line, so the backwards message goes vertically down the screen one letter at a time. The code is below, can someone tell me the correct command or what needs to be fixed?
public class StackUser
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int length;
int I = 0;
char currChar;
int Max = 80;
myStack Placeholder;
Placeholder = new myStack();
System.out.println("Please enter any word or sentence up to a maximum of 80 characters long.");
String userEnter = keyboard.nextLine();
length = userEnter.length();
if (length < Max)
{
while (I < length)
{
char res = userEnter.charAt(I);
Placeholder.PushChar(res);
I = I + 1;
}
}
if (Max < length)
{
while (I < Max)
{
char res = userEnter.charAt(I);
Placeholder.PushChar(res);
I = I + 1;
}
}
while (Placeholder.IsEmpty() != true)
{
currChar = Placeholder.PopChar();
System.out.println("Here is your message backwards:" + currChar);
}
}
}
Just use print instead of println:
System.out.print("Here is your message backwards: ");
while (Placeholder.IsEmpty() != true) {
currChar = Placeholder.PopChar();
System.out.print(currChar);
}
System.out.println();

java beginner programming, putting strings in char array to filter input

in this code I've been trying to filter the characters in the array with a for-loop to a second array, but am unable to. Could anyone tell me what is exactly wrong with my code?
public class Deel1 {
public static void main(String[] args) {
String zinInvoer = getInput();
String zinUitvoer = filterZin(zinInvoer);
}
static String getInput() {
Scanner scan = new Scanner(System.in);
String zinInvoer = "";
System.out.println("Voer een zin in: ");
if (scan.hasNextLine()) {
zinInvoer = scan.nextLine().trim();
}
if (zinInvoer.equals("")) {
System.out.println("Geen invoer!");
System.exit(0);
}
return zinInvoer;
}
static String filterZin(String zinInvoer) {
String zinUitvoer = "";
char ongefilterd[] = zinInvoer.toCharArray();
String nogFilteren = new String(ongefilterd);
char a = nogFilteren.charAt(97);
for (a = 97; a <= 122; a++) {
a = a += 32;
char gefilterd[] = //second array to be printed
}
System.out.println("Gefilterd: " + zinUitvoer);
return zinInvoer;
}
}
Sorry if it annoys you but I had to translate your variables into english in order to figure out what their purposes were.
First of all, it will always throw an exception when the string is less than 98 letters long because it looks for the 97th letter.
Second, the for loop in "filterZin" will only filter letter # 98, which I am guessing was not your intention.
Also, geFilterd should probably be created outside of the for loop, and in the for loop you (i guess) would want to do
geFilterd[a]=a+32;
a+=32;
Because I couldn't figure out what your overall goal was for this program, I made a version of it that does what I think you were trying to do, but again, I do not know.
import java.util.Scanner;
public class Deel1 {
public static void main(String[] args) {
String phraseInput = getInput();
filterPhrase(phraseInput);
}
static String getInput() {
Scanner scan = new Scanner(System.in);
String phraseInput = "";
System.out.println("Voer een zin in: ");
if (scan.hasNextLine()) {
phraseInput = scan.nextLine().trim();
}
if (phraseInput.equals("")) {
System.out.println("Geen invoer!");
System.exit(0);
}
return phraseInput;
}
static String filterPhrase(String phraseInput) {
String phraseOutput = "";
char onFiltered[] = phraseInput.toCharArray();
String currentFilter = new String(onFiltered);
// for (a = 97; a <= 122; a++) {
// a = a += 32;
// //char filtered[] = //second array to be printed
// }
char[] filtered = new char[26];
for(int i=97;i<=122;i++){
char a = currentFilter.charAt(i);
filtered[i-97]= (char) (a+32);
}
System.out.println("filtered: " + filtered.toString());
return phraseInput;
}
}

Categories

Resources