The problem code is below, if you need the entire main method to help me, please ask. The code complies but does not run as expected. I am trying to make the code report back an exclamation mark if the number is out of bounds/larger than the last position of the source text, which is a string the user inputs, so the length cannot be predefined. Exception is 'StringIndexOutOfBoundsException'
TDLR num is an int, sourcetext is a string, both are inputs. Exception: when code should output an '!' instead.
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner sc;
int result, num= 0, end = -2, temp, infolost, count;
String word, sourcetext, answer, space= " ";
String sourcetext2, temp2;
char input, result2, chalost;
sc = new Scanner(System.in);
System.out.println("please enter sourcetext");
sourcetext = sc.nextLine(); // user inputs source text
sourcetext = sourcetext.toLowerCase(); // converts sourcetext into lowercase
System.out.print("Would you like to 1 encrypt, or 2 decrypt?");
answer = sc.next(); // user inputs choice
if (answer.equals("1")||(answer.equals("encrypt"))) {
System.out.println("Please enter at least one word to encrypt");
word = sc.next(); // user inputs one word
for (int i= 0; i < word.length(); i++) {
temp = sourcetext.indexOf(word.charAt(i)); // uses index to convert char positions int num
System.out.print(space + temp + space);
}
System.out.print(space + end);
}
else if (answer.equals("2")||(answer.equals("decrypt"))) {
System.out.println("Please enter digits, with one space between each. End with -2");
while (num > -2) {
num = sc.nextInt(); // num to decrypt
if (num > -2) {
result2 = sourcetext.charAt(num); // num converted into characters
System.out.print(result2);
} else if (num > sourcetext.length()) {
System.out.print("!");
} else if (num<0) {
System.out.print("end");
}
}
}
}
}
Try it like this:
int stringLength = sourcetext.length();
if (num > stringLength) {
System.out.print("!");
}
else if (num<0) {
System.out.print("end");
}
This could lead to an IndexOutOfBoundsException - since -1 is greater than -2 - but still out of bounds...
if (num > -2){
result2 = sourcetext.charAt(num); // num converted into characters
System.out.print(sourcetext.indexOf(num));
}
Edit: Unless the users input is -2 - the first if-Statement will always run... You probably need to re-work the logic there.
Edit2: If num is -1 sourcetext.charAt(num); leads to an IndexOutOfBounds. Do something like
if(num == -2) {
System.out.print("end");
} else if (num >= 0 && num < sourcetext.lenght()) {
// index ok
result2 = sourcetext.charAt(num); // num converted into characters
System.out.print(result2);
} else {
// index out of bounds
System.out.print("!");
}
Related
Below are the code. To fix the error, I simply rewrote the code in getFraction() method to den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/")+1, fracValue.length())) , by adding +1. I have never seen or learn this during my courses and I just encounter this while doing project. I want to understand what the code did in num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/"))) and den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length())), we are converting the numerator to int and the num is all the values before the / and the den is all the values after the /. Am I right ? My second question is that why do we need to add +1 after the indexOf("/") ? Is it so we are taking the values after the /?
import java.util.Scanner;
public class FractionCalculator {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
intro();
while (true) {
String operation = getOperation();
Fraction frac1 = getFraction();
Fraction frac2 = getFraction();
Fraction result = new Fraction(1,1);
String result2 = "";
if (operation.equals("=")) {
System.out.println(frac1+" "+operation+" "+frac2+" is "+frac1.equals(frac2));
} else {
if (operation.equals("+")) {
result=frac1.add(frac2);
} else if (operation.equals("-")) {
result=frac1.subtract(frac2);
} else if (operation.equals("/")) {
if(frac2.getNumerator()==0) {
result2="Undefined";
} else {
result=frac1.divide(frac2);
}
} else if (operation.equals("*")) {
if(frac2.getNumerator()==0) {
result2 = "Undefined";
} else {
result=frac1.multiply(frac2);
}
}
//print results
} if (result2!="") {// division and multiplication by zero is undefined
System.out.println(frac1+" "+operation+" "+"0"+" = "+result2);
} else if (result.getNumerator()%result.getDenominator() == 0) {
System.out.println(frac1+" "+operation+" "+frac2+" = "+(result.getNumerator()/ result.getDenominator()));
} else {
System.out.println(frac1+" "+operation+" "+frac2+" = "+result.toString());
}
}
}
public static void intro() {
System.out.println("\nThis program is a fraction calculator");
System.out.println("It will add, subtract, multiply and divide fractions until you type Q to quit.");
System.out.println("Please enter your fraction in the form a/b, where a and b are integers.");
for (int i=0; i<80; i++) {
System.out.print("-");
}
}
public static String getOperation() {
System.out.println("\nPlease enter an operation (+, -, /, *, = or \"Q\" to quit): ");
Scanner input = new Scanner(System.in);
String operation = input.nextLine();
int x = 0;
while (x == 0) {
if (operation.equals("+") || operation.equals("-") || operation.equals("/") || operation.equals("*") || operation.equals("=")) {
x++;
} else if (operation.equalsIgnoreCase("q")) {
System.exit(0);
} else {
System.out.println("Invalid input, enter valid operation (+, -, /, *, = or \"Q\" to quit)");
operation = input.nextLine();
}
}
return operation;
}
public static boolean validFraction(String input) {
boolean valid;
if (input.startsWith("-")) {
input = input.replaceFirst("-",""); // or use 'input.substring("1", input.length())';
}
if (input.contains("-") || input.charAt(input.indexOf("/")+1)==('0') || input.contains(" ")) {
valid = false;
} else if (input.contains("/")) {
input = input.replace("/", "");
}
if (input.matches("^[0-9]+$") && input.length() > 0) {
valid = true;
} else {
valid = false;
}
return valid;
}
public static Fraction getFraction() {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
String fracValue = input.nextLine();
//validate input
while (!validFraction(fracValue)) {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
fracValue = input.nextLine();
}
//convert to numerator, denominator
int num = 0;
int den = 0;
if (fracValue.contains("/")) {
num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/")));
den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length()));
} else {
num = Integer.parseInt(fracValue);
den = 1;
}
// return fraction
Fraction fracConv = new Fraction(num, den);
return fracConv;
}
}
substring(int start, int end) includes the character at start and excludes the character at end. Since the integer cannot be parsed with a / in it, you need to do fracValue.indexOf("/")+1 to get just the numerical part of the denominator.
Firstly you have to understand your input
if your input is String a = "6+7", it means your string length is 3, and alloted indexes are 0,1 and 2
where 0 index is '6', 1 index is '+' and 2 index is '7'
So, when you use a.substring(0, a.indexOf("+")) it means you are saying
a.indexOf("+") = 1 index
you should get a string including '0' index but not 1 index, because substring works with inclusive first param and exclusive second param.
In case of this a.substring(a.indexOf("+")+1, a.length())
you don't want to include '+' in your denominator, So you should not include 1 index, that's why you are adding (+1) with indexof.
So, by adding +1 you are saying only pick value from a.indexOf("+") +1, i.e. 2 index,
and length of string is 3, which means you will get string inclusive of 2 index, i.e 7
if your input is "6 + 7" in that case you should use 'trim()' before using Integer.parseInt.
I've been working on a palindrome and it won't support an even number of words. I'm not the best at coding. It supports words like "racecar" or "tacocat", but it won't let me use a word/name like "Hannah". I'm new at this coding stuff so anything would really be appreciated.
import java.util.Scanner;
public class Palindrome
{
public static void main(String args [])
{
System.out.printf("\f");
Scanner input = new Scanner(System.in);
System.out.println("enter a word");
String word = input.nextLine();
int size = word.length();
int correct = 0;
int incorrect = 0;
for (int count = 1; count < size; count++)
{
int start = (word.charAt(count));//starting
int end = (word.charAt(size-count));//ending
if (start == end)
correct++;
else
incorrect++;
}
if (correct == 0)
System.out.printf("%s is a palindrome", word);
else
System.out.printf("%s is not a palindrome", word);
}
}
Your code has many problems:
You are comparing characters of wrong indices. For example, you compare the second character (whose index is 1) to the last character (whose index is size - 1). count should be initialized to 0, and end should be word.charAt(size-count-1).
You report the String to be a palindrome when correct == 0, when it should be incorrect == 0 (BTW you don't need a counter, just a boolean).
If you want the check to be case insensitive, you can convert the String to lower case prior to running your loop.
This should work:
public static void main(String args [])
{
System.out.printf("\f");
Scanner input = new Scanner(System.in);
System.out.println("enter a word");
String word = input.nextLine().toLowerCase();
int size = word.length();
boolean isPalindrome = true;
for (int count = 0; count < size; count++)
{
int start = (word.charAt(count));//starting
int end = (word.charAt(size-count-1));//ending
if (start != end) {
isPalindrome = false;
break;
}
}
if (isPalindrome)
System.out.printf("%s is a palindrome", word);
else
System.out.printf("%s is not a palindrome", word);
}
There are several mistakes in your code
You should convert everything to lowercase if you are planning to ignore capital letter in the checking, since it is identified differently in ASCII
For starting, you should start from index 0 instead of 1, to start from the first letter
For ending, you should start from index size-count-1 instead of size-count, to start from the last letter
You should check for incorrect == 0 instead of correct == 0 to determine if it is a palindrome
public static void main(String args[]) {
System.out.printf("\f");
Scanner input = new Scanner(System.in);
System.out.println("enter a word");
String word = input.nextLine().toLowerCase();
int size = word.length();
int correct = 0;
int incorrect = 0;
for (int count = 0; count < size; count++)
{
int start = (word.charAt(count)); //starting
int end = (word.charAt(size-count-1)); //ending
if (start == end)
correct++;
else
incorrect++;
System.out.println(start + " " + end);
}
if (incorrect == 0)
System.out.printf("%s is a palindrome", word);
else
System.out.printf("%s is not a palindrome", word);
}
Bonus: You could check for just half of the word instead of looping through the whole word
First of all you should know that array in java start at 0, not one. so set your count from 0 not one.
Then, word.charAt(count) is a char so better have char variable instead of int.
It's seem that the algorithm you use to decide whether a word is a palindrome or not is by matching first char with last char, second char with second last char, and so on.
If that the case, you will only need to loop halfway for (int count = 1; count < size / 2; count++).
The last one is, you only need one variable to hold the status of palindrome, if your matching process ever find a false then break the loop and just set the isPalindrome status into false.
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
System.out.println ("enter a word");
String word = input.nextLine ();
int size = word.length ();
boolean isPalindrome = true;
int maxIndex = size - 1;
for (int count = 0; count < size / 2; count++)
{
char start = word.charAt (count);
char end = word.charAt (maxIndex - count);
if (start != end)
{
isPalindrome = false;
break;
}
}
if (isPalindrome)
System.out.printf ("%s is a palindrome", word);
else
System.out.printf ("%s is not a palindrome", word);
}
And bear in mind that java's String is case sensitive, so "Tiger" is different than "tiger". Hence, Hannah will not be treated as palindrome. If you want it to be case insensitive, just lowercase all the char in the word like this word = word.toLowerCase() before doing the macthing process.
Check palindrome function is very simple:
public boolean isPalindrome(String str) {
if(str == null)
return false;
str = str.toLowerCase();
for(int i = 0, j = str.length() - 1; i < j; i++, j--)
if(str.charAt(i) != str.charAt(j))
return false;
return true;
}
you may use Stringbuilder to do palindrome check as below
public class test {
public static void main(String args [])
{
System.out.print("\f");
Scanner input = new Scanner(System.in);
System.out.println("enter a word");
String word = input.nextLine();
StringBuilder originalStr = new StringBuilder(word);
String revString = originalStr.reverse().toString();
if(word.equalsIgnoreCase(revString))
System.out.print( word +" is a palindrome");
else
System.out.print( word +" is not a palindrome");
}
}
i tried to make a simple program,which check if the input number from the user is a binary number and that number is in correct binary format -> without leading zeros. That below is my code,but it doesn't work. I would appreciate if someone could help.
public class CheckNumberBinary {
public static void main(String args[]) {
int r = 0, c = 0, num, b;
Scanner sl = new Scanner(System.in);
num = sl.nextInt();
int firstDigit = Integer.parseInt(Integer.toString(num).substring(0, 1));// i want to get the first digit from the input
if (firstDigit>0||firstDigit==1 ){
while (num > 0) {
if ((num % 10 == 0) || (num % 10 == 1))
c++;
r++;
num = num / 10;
}
if (c == r) {
System.out.println(true);
} else
System.out.println(false);
} else System.out.printf("WARNING: The number starts with 0");
}
}
There are a better solution, you can check if your input contain only 0 and 1 and the input great then 0 then valide number, so instead you can use String for example :
String num;
Scanner sl = new Scanner(System.in);
num = sl.next();
if (num.matches("[01]+") && !num.startsWith("0")) {
System.out.println("Correct number :" + num);
}else{
System.out.println("Not Correct number!");
}
num.matches("[01]+") will check if your input contain only 0 and 1.
!num.startsWith("0") this to answer this part without leading zeros
Test:
10010 -> Correct number :10010
00001 -> Not Correct number!
11101 -> Correct number :01101
98888 -> Not Correct number!
You can try something like this:
public static void main(String args[]) {
boolean binary=true; // boolean for final decision
String input;
int counter=0; // to count how many leading zeros there are in the input
int target = 5; // specify how many leading zeros allowed!!
Scanner in = new Scanner(System.in);
input = in.nextLine(); // take the entire line as a String
//first loop through the whole input to check for any illegal entry (i.e. non digits)
for(char digit : input.toCharArray()){
if(!Character.isDigit(digit)){ // catch any non-digit !
System.out.println("Illegal Input Found!"); // inform user and exit
System.exit(0);
}
if(digit!='0' && digit!='1'){ // check if it's not 1 and not 0
binary = false;
}
}
// now if there are no illegal inputs, check if it starts with leading zeros
if(input.charAt(0)=='0'){ // potential leading zeros, check the rest
while(input.charAt(counter)=='0'){ // while there are followed zeros
counter++;
if(counter>target && binary){ // leading zeros only in case it's a binary
System.out.println("Illegal Leading Zeros!");
System.exit(0);
}
}
}
// now if your program reach this point that means the input is valid and doesn't contain leading zeros in case it's a binary
if(binary){
System.out.println("It is a binary number");
}
else{
System.out.println("It is NOT a binary number");
}
}
Test:
01010101 -> It is a binary number
01010105 -> It is NOT a binary number
0000001 -> Illegal Leading Zeros!
0000005 -> It is NOT a binary number
000000A -> Illegal Input Found!
Why not simply use the standard library methods?
static boolean isValidBinary(final int input) {
final String binary = String.valueOf(input);
return binary.replaceAll("[01]", "").isEmpty() && !binary.startsWith("0");
}
you should not use sl.nextInt(); it will transfer '011' to 11, so when user input '011', the variable 'num' get the int value 11.
You should simply use sl.next() to get the input of user.
I think you need to check your "if" condition before the while, because you don't want that the number starts with 0, right? so... just ask for it, I have tryied and worded fine to me:
public class CheckNumberBinary {
public static void main(String args[]) {
int r = 0, c = 0, num, b;
Scanner sl = new Scanner(System.in);
String input = sl.next();
num = Integer.parseInt(input);
String firstDigit = (input.length() > 0 ? input.substring(0, 1) : "" );
if (firstDigit.equals("0")) {
System.out.printf("WARNING: The number starts with 0");
} else {
while (num > 0) {
if ((num % 10 == 0) || (num % 10 == 1))
c++;
r++;
num = num / 10;
}
if (c == r) {
System.out.println(true);
} else
System.out.println(false);
}
}
}
The rest of your code Fulfills its mission! It tells you if the number is binary or not, and now plus tells you if your code begins with useless zeros
import java.util.*;
public class BinaryTest {
public static void main(String [] args){
Scanner input=new Scanner(System.in);
int count=0;
boolean check=true;
System.out.print("Enter a number: ");
int num=input.nextInt();
for(int i=0; i<=num; i++){
count=num%10;
if(count>1) {
check=false;
break;
}
else {
check=true;
}
num=num/10;
}
if(check)
System.out.println("Binary");
else
System.out.println("Not Binary");
}
}
I'm creating a program for my gui number converter. I want my program to ask user a binary string and if he does not enter a binary number, the program will show him error message and will ask him to enter again. The problem is that I can add restriction to alphabets but when it comes to numbers then it fails or it keeps showing the error message.
import java.util.*;
public class test {
Scanner key = new Scanner(System.in);
String in;
int b;
public test(){
do{
System.out.println("Enter the binary string of 5 numbers");
in = key.nextLine();
int i,j;
char ch;
for (i=0 ; i<=5 ; i++){
b = 0;
ch = in.charAt(i);
j = ch;
if (Character.isDigit(ch) && ch<=0 && ch>=1)
b = 1;
else
System.out.println("Please enter a binary number (1 , 0)");
break;
//b = 1;
}
}while(b != 1);
int c;
c = Integer.parseInt(in);
System.out.println("your number is: " + c );
}
public static void main (String args[]){
test obj = new test();
}
}
ch<=0 && ch>=1 does not do what you think it does. The character codes for "0" and "1" are 48 and 49, so you should check for those instead. Also, your >= comparators are backwards, but that's not really the clearest way to write this code. And since you're comparing for just those two values, Character.isDigit(ch) is redundant.
Try one of these:
ch == 48 || ch == 49
ch == '0' || ch == '1'
Scanner has an overloaded nextInt method that uses a radix
int binaryNumber = scanner.nextInt(2);
1) First logic error here for (i=0 ; i<=5 ; i++) replace i<=5 to i<5
2) change the if else condition like below
if (Character.isDigit(ch) && (ch == '0' || ch == '1'))
{
b = 1;
}
else
{
System.out.println("Please enter a binary number (1 , 0)");
break;
}
//Input Number
System.out.print("Enter a number from 1-10: ");
words = input.next();
//Processing String
if(!words.equals(FLAG)){
while(!words.equals("1") && !words.equals("2") && !words.equals("3") &&
!words.equals("4") && !words.equals("5") && !words.equals("6") &&
!words.equals("7") && !words.equals("8") && !words.equals("9") &&
!words.equals("10")){
System.out.print("Please enter a number in integer form from 1-10: ");
words = input.next();
}
}
//Close Scanner
input.close();
//String to Integer
num = Integer.parseInt(words);
//Output Number
if(num >=1 || num <=10){
System.out.println("\nYour number is: " + num);
}
How could i change the while loop? What if the number range was from 1-100? IS there any way to shorten the processing string segment? The program needs to be able to handle string and integers.
Parse it to an Integer
int number = Integer.parseInt(words);
and loop :
while( !(number > 0 && number <= 10) ){ // loop the loop }
N.B: Because you will be using parseInt(), you will need to learn to use try-catch blocks. Do look it up.
You can also directly use nextInt() method of the Scanner class for an input of Integer data type.
So you can do,
num = input.nextInt();
and then directly,
//Output Number
if(num >=1 || num <=10){
System.out.println("\nYour number is: " + num);
}
Make a min int and a max int.
int min =1;
int max =10; // change to 100 for if you wanted it bigger
Then when you get their value simply parse it(Parse turns it into an Interger)
int value= Integer.parseInt(words);
The final while loop would look like this:
while( !(number > min && number <= max) ){
//Your code
}
The easiest way to 'shorten' it would be to parse it before the while loop. You should be throwing NumberFormatException and putting your code in a try/catch block if you're going to be parsing strings to ints.
public void f() throws NumberFormatException{
try{
// other code
num = Integer.parseInt(words);
while( !(num>0 && num<=10) ){
// magic
}
// other code
}catch(NumberFormatException e){
// handle it!
}
}
Use a do while loop so that you don't have to ask the user for input twice, just let the loop do the work for you.
int number;
final int MIN = 0;
final int MAX = 100;
do
{
System.out.print("Please enter a number in integer form from 1-10: ");
words = input.next();
number = Integer.parseInt(words);
}while(!(number > MIN && number <= MAX));
Parse the input into an Integer before check validation.
But do remember to handle exception properly.
int number;
while(true){
try{
number = Integer.parseInt(words);
if(number <= 10 && number >= 1) break;
else
System.out.print("Your number is out of range (1, 10):");
}catch(NumberFormatException e){
System.out.print("Please input valid number from 1 to 10:");
}
words = input.next();
}
System.out.println("Your number is " + number);