Java Assignment Help.
#1

I have Assignment that must be submitted by tomorrow, i was able to solve 4 of the 5 questions.
Well, i am stuck in this

Код:
Write a program that reads in from the user an integer (num) between 1000 and 9999. Then it prompts
the user to enter an integer (d) between 0 and 9 and a character (ch). Your program should replace the 
second and the last digit in num with d and it should display the character that precedes (ch) followed by 
the number after the change and then the character that comes after (ch). Use the division and modulus 
operators to extract the digits from num.

Sample run:

Enter an integer between 1000 and 9999: 2134
Enter a digit (between 0 and 9): 6
Enter a character: b
Number was 2134. Result: a2636c.
and this what i did for now

Код:
package question2;
import java.util.Scanner;

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

    Scanner input = new Scanner(System.in);
    System.out.println("Enter an integer between 1000 and 9999: ");
    int num = input.nextInt();
    System.out.println("Enter a digit (between 0 and 9): ");
    int d = input.nextInt();
    System.out.println("Enter a character: ");
    double ch = input.nextDouble();
        
   }
    
}
Anyone knows how to solve it?
Reply
#2

Take a look at how strings work. When modifying certain digits of something this is the easiest way to go.
If you read a bit about how to use them and what you can do with them youll probably find the answer quickly.

About your code: in the last line youre trying to get a double from the input. A double is just like a float but with higher precision. Its certainly not a String or (char)acter and so cant hold the character you entered, so you have to use a different datatype there.
Reply
#3

Quote:
Originally Posted by Mauzen
Посмотреть сообщение
Take a look at how strings work. When modifying certain digits of something this is the easiest way to go.
If you read a bit about how to use them and what you can do with them youll probably find the answer quickly.

About your code: in the last line youre trying to get a double from the input. A double is just like a float but with higher precision. Its certainly not a String or (char)acter and so cant hold the character you entered, so you have to use a different datatype there.
so at the last line i have to change it to
Char ch = input.nextChar();

Still i need to figure out how to replace 2nd and 4th in num and replace it with integer d.
As well display the precedent and the letter that comes after ch before&after the number.
Reply
#4

As here https://buckysroom.org/forum/
Someone will answer you, trust me
Reply
#5

Assuming you have to use only modulus and division, you can do something like this:

Код:
num = num - ( ( num / 100 % 10 ) * 100 ) - ( num % 10 ) + d * 100 + d;
cout << (char)(ch-1) << num << (char)(ch+1);
I wrote the code in C syntax.
Reply
#6

Quote:
Originally Posted by ColonelBurton
Посмотреть сообщение
so at the last line i have to change it to
Char ch = input.nextChar();

Still i need to figure out how to replace 2nd and 4th in num and replace it with integer d.
As well display the precedent and the letter that comes after ch before&after the number.
Yep, a char would work for that.

Incase its not too late yet, take a look at the Java API for Strings : http://docs.oracle.com/javase/6/docs...ng/String.html

The examples at the top already point into exactly the direction you need.
You cant actually replace the digits in the string without extra efforts, but you can cut out the parts you need from the number, and then recombine the string, inserting the d integer and the chars where you need them.
For that you should take a closer look at the example, the substring, and the length method. Im pretty sure this the expected solution, as other ways to do it are more complicated.

I dont like just giving the solutions and explaining them for assignments. Its much more helpful if you work it out yourself, with just having hints where to look


@RaZVaN ^ xD
This would be an alternative solution. Though I dont think they expect a solution like that for the assignment, as its more complicated, and rather focuses on math than on the java language (and it wouldnt work if the number wasnt 4 digits long without extra effort )
Reply
#7

I program in java so here is solution -
Код:
package question2;
import java.util.Scanner;

public class Question2 {
   
  void main() {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter an integer between 1000 and 9999: ");
    int num = input.nextInt();
    System.out.println("Enter a digit (between 0 and 9): ");
    int d = input.nextInt();
    System.out.println("Enter a character: ");
    char ch = input.nextLine.charAt(0);//thats how u enter a char
    String result = (ch-1);//it stores the final result
    int r,c = 1,f=num;
    while(num != 0)
    {
       r = num % 10;
       num = num/10;
      if(c==2 || c==4)result+=d;
      else result+=r;
      c++;
    }
    result+= (ch+1);
    System.out.println("Number was "+f+". Result : "+result);
 
        
   }
    
}
didnt tested it caz currently i dont have bluej on my pc
But should work...
Reply
#8

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
I program in java so here is solution -
Код:
package question2;
import java.util.Scanner;

public class Question2 {
   
  void main() {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter an integer between 1000 and 9999: ");
    int num = input.nextInt();
    System.out.println("Enter a digit (between 0 and 9): ");
    int d = input.nextInt();
    System.out.println("Enter a character: ");
    char ch = input.nextLine.charAt(0);//thats how u enter a char
    String result = (ch-1);//it stores the final result
    int r,c = 1,f=num;
    while(num != 0)
    {
       r = num % 10;
       num = num/10;
      if(c==2 || c==4)result+=d;
      else result+=r;
      c++;
    }
    result+= (ch+1);
    System.out.println("Number was "+f+". Result : "+result);
 
        
   }
    
}
didnt tested it caz currently i dont have bluej on my pc
But should work...
First of all, handing him a clear cut solution is NOT helping. He will learn nothing from it and, depending on how strict his school is, could even get in trouble for handing in code that isn't his.

Furthermore, it doesn't work...


@OP:
In Java, a String cannot be modified. However, you can convert the string to a array of characters (******, in which you can modify each individual character.
Reply
#9

There must be some little error but it must work i am sure...
And string cannot be modified but can be concated
So i have made a string which is concated...

And for giving him direct solution I am sorry for dat..
Reply
#10

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
There must be some little error but it must work i am sure...
And string cannot be modified but can be concated
So i have made a string which is concated...

And for giving him direct solution I am sorry for dat..
You didn't follow the assignment, at all. So no, it is wrong. Or can you explain why you're using modulo and such for an assignment such as this one?

Edit : I get it, and it is needlessly complicated. Like I said, Strings may be immutable but character arrays are not, which seem to be the only logical goal of the assignment...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)